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.

73 lines
1.9 KiB

  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.Set(k2, v2)
  43. gv2 := client.Get(k2)
  44. require.Equal(t, gv2, v2)
  45. // More iteration
  46. itr = client.Iterator(nil, nil)
  47. itr.Next()
  48. require.Equal(t, itr.Key(), []byte("key-1"))
  49. require.Equal(t, itr.Value(), []byte("value-1"))
  50. itr.Next()
  51. require.Equal(t, itr.Key(), []byte("key-2"))
  52. require.Equal(t, itr.Value(), []byte("value-2"))
  53. require.Panics(t, itr.Next)
  54. // Deletion
  55. client.Delete(k1)
  56. client.Delete(k2)
  57. gv1 = client.Get(k1)
  58. gv2 = client.Get(k2)
  59. require.Equal(t, len(gv2), 0, "after deletion, not expecting the key to exist anymore")
  60. require.Equal(t, len(gv1), 0, "after deletion, not expecting the key to exist anymore")
  61. // TODO Batch tests
  62. }