You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package remotedb_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tmlibs/grpcdb"
  7. "github.com/tendermint/tmlibs/remotedb"
  8. )
  9. func TestRemoteDB(t *testing.T) {
  10. cert := "::.crt"
  11. key := "::.key"
  12. ln, err := net.Listen("tcp", "0.0.0.0:0")
  13. require.Nil(t, err, "expecting a port to have been assigned on which we can listen")
  14. srv, err := grpcdb.NewServer(cert, key)
  15. require.Nil(t, err)
  16. defer srv.Stop()
  17. go func() {
  18. if err := srv.Serve(ln); err != nil {
  19. t.Fatalf("BindServer: %v", err)
  20. }
  21. }()
  22. client, err := remotedb.NewRemoteDB(ln.Addr().String(), cert)
  23. require.Nil(t, err, "expecting a successful client creation")
  24. require.Nil(t, client.InitRemote(&remotedb.Init{Name: "test-remote-db", Type: "leveldb"}))
  25. k1 := []byte("key-1")
  26. v1 := client.Get(k1)
  27. require.Equal(t, 0, len(v1), "expecting no key1 to have been stored")
  28. vv1 := []byte("value-1")
  29. client.Set(k1, vv1)
  30. gv1 := client.Get(k1)
  31. require.Equal(t, gv1, vv1)
  32. // Simple iteration
  33. itr := client.Iterator(nil, nil)
  34. itr.Next()
  35. require.Equal(t, itr.Key(), []byte("key-1"))
  36. require.Equal(t, itr.Value(), []byte("value-1"))
  37. require.Panics(t, itr.Next)
  38. itr.Close()
  39. // Set some more keys
  40. k2 := []byte("key-2")
  41. v2 := []byte("value-2")
  42. client.SetSync(k2, v2)
  43. has := client.Has(k2)
  44. require.True(t, has)
  45. gv2 := client.Get(k2)
  46. require.Equal(t, gv2, v2)
  47. // More iteration
  48. itr = client.Iterator(nil, nil)
  49. itr.Next()
  50. require.Equal(t, itr.Key(), []byte("key-1"))
  51. require.Equal(t, itr.Value(), []byte("value-1"))
  52. itr.Next()
  53. require.Equal(t, itr.Key(), []byte("key-2"))
  54. require.Equal(t, itr.Value(), []byte("value-2"))
  55. require.Panics(t, itr.Next)
  56. itr.Close()
  57. // Deletion
  58. client.Delete(k1)
  59. client.DeleteSync(k2)
  60. gv1 = client.Get(k1)
  61. gv2 = client.Get(k2)
  62. require.Equal(t, len(gv2), 0, "after deletion, not expecting the key to exist anymore")
  63. require.Equal(t, len(gv1), 0, "after deletion, not expecting the key to exist anymore")
  64. // Batch tests - set
  65. k3 := []byte("key-3")
  66. k4 := []byte("key-4")
  67. k5 := []byte("key-5")
  68. v3 := []byte("value-3")
  69. v4 := []byte("value-4")
  70. v5 := []byte("value-5")
  71. bat := client.NewBatch()
  72. bat.Set(k3, v3)
  73. bat.Set(k4, v4)
  74. rv3 := client.Get(k3)
  75. require.Equal(t, 0, len(rv3), "expecting no k3 to have been stored")
  76. rv4 := client.Get(k4)
  77. require.Equal(t, 0, len(rv4), "expecting no k4 to have been stored")
  78. bat.Write()
  79. rv3 = client.Get(k3)
  80. require.Equal(t, rv3, v3, "expecting k3 to have been stored")
  81. rv4 = client.Get(k4)
  82. require.Equal(t, rv4, v4, "expecting k4 to have been stored")
  83. // Batch tests - deletion
  84. bat = client.NewBatch()
  85. bat.Delete(k4)
  86. bat.Delete(k3)
  87. bat.WriteSync()
  88. rv3 = client.Get(k3)
  89. require.Equal(t, 0, len(rv3), "expecting k3 to have been deleted")
  90. rv4 = client.Get(k4)
  91. require.Equal(t, 0, len(rv4), "expecting k4 to have been deleted")
  92. // Batch tests - set and delete
  93. bat = client.NewBatch()
  94. bat.Set(k4, v4)
  95. bat.Set(k5, v5)
  96. bat.Delete(k4)
  97. bat.WriteSync()
  98. rv4 = client.Get(k4)
  99. require.Equal(t, 0, len(rv4), "expecting k4 to have been deleted")
  100. rv5 := client.Get(k5)
  101. require.Equal(t, rv5, v5, "expecting k5 to have been stored")
  102. }