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.

52 lines
1.2 KiB

  1. package grpcdb_test
  2. import (
  3. "bytes"
  4. "context"
  5. "log"
  6. grpcdb "github.com/tendermint/tmlibs/grpcdb"
  7. protodb "github.com/tendermint/tmlibs/proto"
  8. )
  9. func Example() {
  10. addr := ":8998"
  11. cert := "server.crt"
  12. key := "server.key"
  13. go func() {
  14. if err := grpcdb.ListenAndServe(addr, cert, key); err != nil {
  15. log.Fatalf("BindServer: %v", err)
  16. }
  17. }()
  18. client, err := grpcdb.NewClient(addr, cert)
  19. if err != nil {
  20. log.Fatalf("Failed to create grpcDB client: %v", err)
  21. }
  22. ctx := context.Background()
  23. // 1. Initialize the DB
  24. in := &protodb.Init{
  25. Type: "leveldb",
  26. Name: "grpc-uno-test",
  27. Dir: ".",
  28. }
  29. if _, err := client.Init(ctx, in); err != nil {
  30. log.Fatalf("Init error: %v", err)
  31. }
  32. // 2. Now it can be used!
  33. query1 := &protodb.Entity{Key: []byte("Project"), Value: []byte("Tmlibs-on-gRPC")}
  34. if _, err := client.SetSync(ctx, query1); err != nil {
  35. log.Fatalf("SetSync err: %v", err)
  36. }
  37. query2 := &protodb.Entity{Key: []byte("Project")}
  38. read, err := client.Get(ctx, query2)
  39. if err != nil {
  40. log.Fatalf("Get err: %v", err)
  41. }
  42. if g, w := read.Value, []byte("Tmlibs-on-gRPC"); !bytes.Equal(g, w) {
  43. log.Fatalf("got= (%q ==> % X)\nwant=(%q ==> % X)", g, g, w, w)
  44. }
  45. }