|
| 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 | + |
| 15 | +package naming |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | + |
| 22 | + etcd "go.etcd.io/etcd/clientv3" |
| 23 | + "go.etcd.io/etcd/clientv3/naming/endpoints" |
| 24 | + |
| 25 | + "go.etcd.io/etcd/integration" |
| 26 | + "go.etcd.io/etcd/pkg/testutil" |
| 27 | +) |
| 28 | + |
| 29 | +func TestEndpointManager(t *testing.T) { |
| 30 | + t.Skip("Not implemented yet") |
| 31 | + |
| 32 | + defer testutil.AfterTest(t) |
| 33 | + |
| 34 | + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) |
| 35 | + defer clus.Terminate(t) |
| 36 | + |
| 37 | + em, err := endpoints.NewManager(clus.RandClient(), "foo") |
| 38 | + if err != nil { |
| 39 | + t.Fatal("failed to create EndpointManager", err) |
| 40 | + } |
| 41 | + ctx, watchCancel := context.WithCancel(context.Background()) |
| 42 | + defer watchCancel() |
| 43 | + w, err := em.NewWatchChannel(ctx) |
| 44 | + if err != nil { |
| 45 | + t.Fatal("failed to establish watch", err) |
| 46 | + } |
| 47 | + |
| 48 | + e1 := endpoints.Endpoint{Addr: "127.0.0.1", Metadata: "metadata"} |
| 49 | + err = em.AddEndpoint(context.TODO(), "foo/a1", e1) |
| 50 | + if err != nil { |
| 51 | + t.Fatal("failed to add foo", err) |
| 52 | + } |
| 53 | + |
| 54 | + us := <-w |
| 55 | + |
| 56 | + if us == nil { |
| 57 | + t.Fatal("failed to get update", err) |
| 58 | + } |
| 59 | + |
| 60 | + wu := endpoints.Update{ |
| 61 | + Op: endpoints.Add, |
| 62 | + Key: "foo/a1", |
| 63 | + Endpoint: e1, |
| 64 | + } |
| 65 | + |
| 66 | + if !reflect.DeepEqual(us[0], wu) { |
| 67 | + t.Fatalf("up = %#v, want %#v", us[0], wu) |
| 68 | + } |
| 69 | + |
| 70 | + err = em.DeleteEndpoint(context.TODO(), "foo/a1") |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("failed to udpate %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + us = <-w |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to get udpate %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + wu = endpoints.Update{ |
| 81 | + Op: endpoints.Delete, |
| 82 | + Key: "foo/a1", |
| 83 | + } |
| 84 | + |
| 85 | + if !reflect.DeepEqual(us, wu) { |
| 86 | + t.Fatalf("up = %#v, want %#v", us[1], wu) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestEndpointManagerAtomicity ensures the resolver will initialize |
| 91 | +// correctly with multiple hosts and correctly receive multiple |
| 92 | +// updates in a single revision. |
| 93 | +func TestEndpointManagerAtomicity(t *testing.T) { |
| 94 | + t.Skip("Not implemented yet") |
| 95 | + |
| 96 | + defer testutil.AfterTest(t) |
| 97 | + |
| 98 | + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) |
| 99 | + defer clus.Terminate(t) |
| 100 | + |
| 101 | + c := clus.RandClient() |
| 102 | + em, err := endpoints.NewManager(c, "foo") |
| 103 | + if err != nil { |
| 104 | + t.Fatal("failed to create EndpointManager", err) |
| 105 | + } |
| 106 | + |
| 107 | + err = em.Update(context.TODO(), []*endpoints.UpdateWithOpts{ |
| 108 | + endpoints.NewAddUpdateOpts("foo/host", endpoints.Endpoint{Addr: "127.0.0.1:2000"}), |
| 109 | + endpoints.NewAddUpdateOpts("foo/host2", endpoints.Endpoint{Addr: "127.0.0.1:2001"})}) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + ctx, watchCancel := context.WithCancel(context.Background()) |
| 115 | + defer watchCancel() |
| 116 | + w, err := em.NewWatchChannel(ctx) |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + updates := <-w |
| 122 | + if len(updates) != 2 { |
| 123 | + t.Fatalf("expected two updates, got %+v", updates) |
| 124 | + } |
| 125 | + |
| 126 | + _, err = c.Txn(context.TODO()).Then(etcd.OpDelete("foo/host"), etcd.OpDelete("foo/host2")).Commit() |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + updates = <-w |
| 132 | + if len(updates) != 2 || (updates[0].Op != endpoints.Delete && updates[1].Op != endpoints.Delete) { |
| 133 | + t.Fatalf("expected two delete updates, got %+v", updates) |
| 134 | + } |
| 135 | +} |
0 commit comments