Skip to content

Commit

Permalink
test: etcd ut (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang authored Jan 28, 2024
1 parent 53b9a9b commit 2e81d66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 2 additions & 7 deletions pkg/config/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ func (c *storeOperate) Init(options map[string]interface{}) error {

func (c *storeOperate) Save(key config.PathKey, val []byte) error {
_, err := c.client.Put(context.Background(), string(key), string(val))
if err != nil {
return err
}

return nil
return err
}

func (c *storeOperate) Get(key config.PathKey) ([]byte, error) {
Expand All @@ -92,7 +88,7 @@ func (c *storeOperate) Get(key config.PathKey) ([]byte, error) {
}

if len(resp.Kvs) == 0 {
return nil, err
return nil, nil
}

return resp.Kvs[0].Value, nil
Expand Down Expand Up @@ -150,7 +146,6 @@ func (c *storeOperate) Watch(key config.PathKey) (<-chan []byte, error) {
ctx, cancel := context.WithCancel(context.Background())
c.cancelList = append(c.cancelList, cancel)
go w.run(ctx)

}

w := c.receivers[key]
Expand Down
9 changes: 7 additions & 2 deletions pkg/config/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ func Test_storeOpertae(t *testing.T) {

assert.NoError(t, err, "init must success")

assert.Error(t, operate.Init(map[string]interface{}{"endpoints": "fake"}))

doDataMock()
cfg, _ := config.Load(testdata.Path("fake_config.yaml"))

tenantName := cfg.Data.Tenants[0].Name

for k, v := range mockConfData {
err := operate.Save(k, []byte(v))
assert.NoError(t, err, "save must success")
assert.NoError(t, operate.Save(k, []byte(v)), "save must success")
}

for k, v := range mockConfData {
Expand All @@ -111,6 +112,9 @@ func Test_storeOpertae(t *testing.T) {
t.Logf("%s => %s", k, string(ret))
}

_, err = operate.Get("fake")
assert.NoError(t, err)

receiver, err := operate.Watch(mockPath[tenantName].DefaultConfigDataUsersPath)
assert.NoError(t, err, "watch must success")

Expand All @@ -132,4 +136,5 @@ func Test_storeOpertae(t *testing.T) {
t.Logf("acutal val : %s", string(ret))

assert.Equal(t, string(data), string(ret))
assert.NoError(t, operate.Close())
}

0 comments on commit 2e81d66

Please sign in to comment.