Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Aug 1, 2023
1 parent c737e25 commit 16b6f56
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pkg/mcs/discovery/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ func (sr *ServiceRegister) Register() error {
log.Error("put the key failed", zap.String("key", sr.key), zap.Error(err))
continue
}

kresp, err = sr.cli.KeepAlive(sr.ctx, resp.ID)
if err != nil {
log.Error("client keep alive failed", zap.String("key", sr.key), zap.Error(err))
continue
}
t.Stop()
goto KeepaliveLoop
}
Expand Down
38 changes: 27 additions & 11 deletions pkg/mcs/discovery/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ func TestRegister(t *testing.T) {
re := require.New(t)
cfg := etcdutil.NewTestSingleConfig(t)
etcd, err := embed.StartEtcd(cfg)
defer func() {
etcd.Close()
}()
re.NoError(err)

ep := cfg.LCUrls[0].String()
client, err := clientv3.NewFromURL(ep)
re.NoError(err)

<-etcd.Server.ReadyNotify()
// with http prefix

// Test register with http prefix.
sr := NewServiceRegister(context.Background(), client, "12345", "test_service", "http://127.0.0.1:1", "http://127.0.0.1:1", 10)
re.NoError(err)
err = sr.Register()
Expand All @@ -49,20 +45,40 @@ func TestRegister(t *testing.T) {
re.NoError(err)
re.Equal("http://127.0.0.1:1", string(resp.Kvs[0].Value))

// Test deregister.
err = sr.Deregister()
re.NoError(err)
resp, err = client.Get(context.Background(), sr.key)
re.NoError(err)
re.Empty(resp.Kvs)

// Test the case that ctx is canceled.
sr = NewServiceRegister(context.Background(), client, "12345", "test_service", "127.0.0.1:2", "127.0.0.1:2", 1)
re.NoError(err)
err = sr.Register()
re.NoError(err)
sr.cancel()
// ensure that the lease is expired
time.Sleep(3 * time.Second)
resp, err = client.Get(context.Background(), sr.key)
re.Empty(getKeyAfterLeaseExpired(re, client, sr.key))

// Test the case that keepalive is failed when the etcd is restarted.
sr = NewServiceRegister(context.Background(), client, "12345", "test_service", "127.0.0.1:2", "127.0.0.1:2", 1)
err = sr.Register()
re.NoError(err)
re.Empty(resp.Kvs)
re.Equal("127.0.0.1:2", getKeyAfterLeaseExpired(re, client, sr.key))
etcd.Close() // close the etcd to make the keepalive failed
time.Sleep(3 * time.Second) // ensure that the lease is expired
etcd, err = embed.StartEtcd(cfg)
re.NoError(err)
<-etcd.Server.ReadyNotify()
defer etcd.Close()
re.Equal("127.0.0.1:2", getKeyAfterLeaseExpired(re, client, sr.key))
}

func getKeyAfterLeaseExpired(re *require.Assertions, client *clientv3.Client, key string) string {
time.Sleep(3 * time.Second) // ensure that the lease is expired
resp, err := client.Get(context.Background(), key)
re.NoError(err)
if len(resp.Kvs) == 0 {
return ""
}
return string(resp.Kvs[0].Value)
}

0 comments on commit 16b6f56

Please sign in to comment.