mirror_test.go 2.7 KB

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