Skip to content

Commit

Permalink
fix test again
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed May 6, 2023
1 parent cb5815b commit 82efaae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ type LoopWatcher struct {
loadBatchSize int64
// watchChangeRetryInterval is used to set the retry interval for watching etcd change.
watchChangeRetryInterval time.Duration
// updateClientCh is used to update the etcd client.
// It's only used for testing.
updateClientCh chan *clientv3.Client
}

// NewLoopWatcher creates a new LoopWatcher.
Expand All @@ -407,6 +410,7 @@ func NewLoopWatcher(ctx context.Context, wg *sync.WaitGroup, client *clientv3.Cl
wg: wg,
forceLoadCh: make(chan struct{}, 1),
isLoadedCh: make(chan error, 1),
updateClientCh: make(chan *clientv3.Client, 1),
putFn: putFn,
deleteFn: deleteFn,
postEventFn: postEventFn,
Expand Down Expand Up @@ -445,6 +449,9 @@ func (lw *LoopWatcher) StartWatchLoop() {
zap.Error(err))
watchStartRevision = nextRevision
time.Sleep(lw.watchChangeRetryInterval)
failpoint.Inject("updateClient", func() {
lw.client = <-lw.updateClientCh
})
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/utils/etcdutil/etcdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ func TestEtcdScaleInAndOutWithoutMultiPoint(t *testing.T) {

// Add a new member and check members
etcd2 := checkAddEtcdMember(t, cfg1, client1)
defer func() {
etcd2.Close()
}()
checkMembers(re, client2, []*embed.Etcd{etcd1, etcd2})

// scale in etcd1
Expand Down Expand Up @@ -671,6 +674,9 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
suite.NoError(err)
checkCache("")

// we use close client and update client in failpoint to simulate the network error and recover
failpoint.Enable("github.com/tikv/pd/pkg/utils/etcdutil/updateClient", "return(true)")

// Case1: restart the etcd server
suite.etcd.Close()
suite.startEtcd()
Expand All @@ -681,7 +687,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
suite.client.Close()
suite.client, err = createEtcdClient(nil, suite.config.LCUrls[0])
suite.NoError(err)
watcher.client = suite.client
watcher.updateClientCh <- suite.client
suite.put("TestWatcherBreak", "2")
checkCache("2")

Expand All @@ -690,7 +696,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
suite.client, err = createEtcdClient(nil, suite.config.LCUrls[0])
suite.NoError(err)
suite.put("TestWatcherBreak", "3")
watcher.client = suite.client
watcher.updateClientCh <- suite.client
checkCache("3")

// Case4: close the etcd client and put a new value with compact
Expand All @@ -704,7 +710,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
resp2, err := suite.etcd.Server.Compact(suite.ctx, &etcdserverpb.CompactionRequest{Revision: revision})
suite.NoError(err)
suite.Equal(revision, resp2.Header.Revision)
watcher.client = suite.client
watcher.updateClientCh <- suite.client
checkCache("4")

// Case5: there is an error data in cache
Expand All @@ -713,6 +719,8 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
cache.Unlock()
watcher.ForceLoad()
checkCache("4")

failpoint.Disable("github.com/tikv/pd/pkg/utils/etcdutil/updateClient")
}

func (suite *loopWatcherTestSuite) startEtcd() {
Expand Down

0 comments on commit 82efaae

Please sign in to comment.