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.

44 lines
1.2 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. // Deletion
  33. client.Delete(k1)
  34. gv2 := client.Get(k1)
  35. require.Equal(t, len(gv2), 0, "after deletion, not expecting the key to exist anymore")
  36. require.NotEqual(t, len(gv1), len(gv2), "after deletion, not expecting the key to exist anymore")
  37. }