grpc_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 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 naming
  15. import (
  16. "context"
  17. "encoding/json"
  18. "reflect"
  19. "testing"
  20. etcd "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "google.golang.org/grpc/naming"
  24. )
  25. func TestGRPCResolver(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  28. defer clus.Terminate(t)
  29. r := GRPCResolver{
  30. Client: clus.RandClient(),
  31. }
  32. w, err := r.Resolve("foo")
  33. if err != nil {
  34. t.Fatal("failed to resolve foo", err)
  35. }
  36. defer w.Close()
  37. addOp := naming.Update{Op: naming.Add, Addr: "127.0.0.1", Metadata: "metadata"}
  38. err = r.Update(context.TODO(), "foo", addOp)
  39. if err != nil {
  40. t.Fatal("failed to add foo", err)
  41. }
  42. us, err := w.Next()
  43. if err != nil {
  44. t.Fatal("failed to get udpate", err)
  45. }
  46. wu := &naming.Update{
  47. Op: naming.Add,
  48. Addr: "127.0.0.1",
  49. Metadata: "metadata",
  50. }
  51. if !reflect.DeepEqual(us[0], wu) {
  52. t.Fatalf("up = %#v, want %#v", us[0], wu)
  53. }
  54. delOp := naming.Update{Op: naming.Delete, Addr: "127.0.0.1"}
  55. err = r.Update(context.TODO(), "foo", delOp)
  56. if err != nil {
  57. t.Fatalf("failed to udpate %v", err)
  58. }
  59. us, err = w.Next()
  60. if err != nil {
  61. t.Fatalf("failed to get udpate %v", err)
  62. }
  63. wu = &naming.Update{
  64. Op: naming.Delete,
  65. Addr: "127.0.0.1",
  66. Metadata: "metadata",
  67. }
  68. if !reflect.DeepEqual(us[0], wu) {
  69. t.Fatalf("up = %#v, want %#v", us[0], wu)
  70. }
  71. }
  72. // TestGRPCResolverMulti ensures the resolver will initialize
  73. // correctly with multiple hosts and correctly receive multiple
  74. // updates in a single revision.
  75. func TestGRPCResolverMulti(t *testing.T) {
  76. defer testutil.AfterTest(t)
  77. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  78. defer clus.Terminate(t)
  79. c := clus.RandClient()
  80. v, verr := json.Marshal(naming.Update{Addr: "127.0.0.1", Metadata: "md"})
  81. if verr != nil {
  82. t.Fatal(verr)
  83. }
  84. if _, err := c.Put(context.TODO(), "foo/host", string(v)); err != nil {
  85. t.Fatal(err)
  86. }
  87. if _, err := c.Put(context.TODO(), "foo/host2", string(v)); err != nil {
  88. t.Fatal(err)
  89. }
  90. r := GRPCResolver{c}
  91. w, err := r.Resolve("foo")
  92. if err != nil {
  93. t.Fatal("failed to resolve foo", err)
  94. }
  95. defer w.Close()
  96. updates, nerr := w.Next()
  97. if nerr != nil {
  98. t.Fatal(nerr)
  99. }
  100. if len(updates) != 2 {
  101. t.Fatalf("expected two updates, got %+v", updates)
  102. }
  103. _, err = c.Txn(context.TODO()).Then(etcd.OpDelete("foo/host"), etcd.OpDelete("foo/host2")).Commit()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. updates, nerr = w.Next()
  108. if nerr != nil {
  109. t.Fatal(nerr)
  110. }
  111. if len(updates) != 2 || (updates[0].Op != naming.Delete && updates[1].Op != naming.Delete) {
  112. t.Fatalf("expected two updates, got %+v", updates)
  113. }
  114. }