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