go.etcd.io/etcd@v3.3.27+incompatible/clientv3/integration/mirror_test.go (about) 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 integration 16 17 import ( 18 "context" 19 "fmt" 20 "reflect" 21 "sync" 22 "testing" 23 "time" 24 25 "github.com/coreos/etcd/clientv3/mirror" 26 "github.com/coreos/etcd/integration" 27 "github.com/coreos/etcd/mvcc/mvccpb" 28 "github.com/coreos/etcd/pkg/testutil" 29 ) 30 31 func TestMirrorSync(t *testing.T) { 32 defer testutil.AfterTest(t) 33 34 clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) 35 defer clus.Terminate(t) 36 37 c := clus.Client(0) 38 _, err := c.KV.Put(context.TODO(), "foo", "bar") 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 syncer := mirror.NewSyncer(c, "", 0) 44 gch, ech := syncer.SyncBase(context.TODO()) 45 wkvs := []*mvccpb.KeyValue{{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}} 46 47 for g := range gch { 48 if !reflect.DeepEqual(g.Kvs, wkvs) { 49 t.Fatalf("kv = %v, want %v", g.Kvs, wkvs) 50 } 51 } 52 53 for e := range ech { 54 t.Fatalf("unexpected error %v", e) 55 } 56 57 wch := syncer.SyncUpdates(context.TODO()) 58 59 _, err = c.KV.Put(context.TODO(), "foo", "bar") 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 select { 65 case r := <-wch: 66 wkv := &mvccpb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 3, Version: 2} 67 if !reflect.DeepEqual(r.Events[0].Kv, wkv) { 68 t.Fatalf("kv = %v, want %v", r.Events[0].Kv, wkv) 69 } 70 case <-time.After(time.Second): 71 t.Fatal("failed to receive update in one second") 72 } 73 } 74 75 func TestMirrorSyncBase(t *testing.T) { 76 cluster := integration.NewClusterV3(nil, &integration.ClusterConfig{Size: 1}) 77 defer cluster.Terminate(nil) 78 79 cli := cluster.Client(0) 80 ctx := context.TODO() 81 82 keyCh := make(chan string) 83 var wg sync.WaitGroup 84 85 for i := 0; i < 50; i++ { 86 wg.Add(1) 87 88 go func() { 89 defer wg.Done() 90 91 for key := range keyCh { 92 if _, err := cli.Put(ctx, key, "test"); err != nil { 93 t.Fatal(err) 94 } 95 } 96 }() 97 } 98 99 for i := 0; i < 2000; i++ { 100 keyCh <- fmt.Sprintf("test%d", i) 101 } 102 103 close(keyCh) 104 wg.Wait() 105 106 syncer := mirror.NewSyncer(cli, "test", 0) 107 respCh, errCh := syncer.SyncBase(ctx) 108 109 count := 0 110 111 for resp := range respCh { 112 count = count + len(resp.Kvs) 113 if !resp.More { 114 break 115 } 116 } 117 118 for err := range errCh { 119 t.Fatalf("unexpected error %v", err) 120 } 121 122 if count != 2000 { 123 t.Errorf("unexpected kv count: %d", count) 124 } 125 }