register_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package grpcproxy
  15. import (
  16. "testing"
  17. "time"
  18. "go.etcd.io/etcd/clientv3"
  19. "go.etcd.io/etcd/clientv3/naming"
  20. "go.etcd.io/etcd/integration"
  21. "go.etcd.io/etcd/pkg/testutil"
  22. gnaming "google.golang.org/grpc/naming"
  23. )
  24. func TestRegister(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  27. defer clus.Terminate(t)
  28. cli := clus.Client(0)
  29. paddr := clus.Members[0].GRPCAddr()
  30. testPrefix := "test-name"
  31. wa := createWatcher(t, cli, testPrefix)
  32. ups, err := wa.Next()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if len(ups) != 0 {
  37. t.Fatalf("len(ups) expected 0, got %d (%v)", len(ups), ups)
  38. }
  39. donec := Register(cli, testPrefix, paddr, 5)
  40. ups, err = wa.Next()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if len(ups) != 1 {
  45. t.Fatalf("len(ups) expected 1, got %d (%v)", len(ups), ups)
  46. }
  47. if ups[0].Addr != paddr {
  48. t.Fatalf("ups[0].Addr expected %q, got %q", paddr, ups[0].Addr)
  49. }
  50. cli.Close()
  51. clus.TakeClient(0)
  52. select {
  53. case <-donec:
  54. case <-time.After(5 * time.Second):
  55. t.Fatal("donec 'register' did not return in time")
  56. }
  57. }
  58. func createWatcher(t *testing.T, c *clientv3.Client, prefix string) gnaming.Watcher {
  59. gr := &naming.GRPCResolver{Client: c}
  60. watcher, err := gr.Resolve(prefix)
  61. if err != nil {
  62. t.Fatalf("failed to resolve %q (%v)", prefix, err)
  63. }
  64. return watcher
  65. }