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.

123 lines
3.3 KiB

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