Skip to content

Commit

Permalink
fix:data race between goroutine in test
Browse files Browse the repository at this point in the history
  • Loading branch information
XZ0730 committed Nov 26, 2023
1 parent 8928455 commit 442b66d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions apollo/apollo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func (fa *fakeApollo) change(cfg configParamKey, data string) {
fa.Lock()
defer fa.Unlock()
klog.Infof("change data : %s", data)
for idx := 0; idx < fa.len; idx++ {

if fa.len != 0 {
fa.resp <- &agollo.ApolloResponse{
NewValue: agollo.Configurations{cfg.Key: data},
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestRegisterAndDeregister(t *testing.T) {
}()
wg.Wait()
// wait the goroutine init
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
// first change
fake.change(configParamKey{
Key: "k1",
Expand All @@ -155,8 +156,9 @@ func TestRegisterAndDeregister(t *testing.T) {
}, "first change")

// wait goroutine deal
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)

gotlock.Lock()
assert.Equal(t, map[configParamKey]map[int64]string{
{
Key: "k1",
Expand All @@ -167,7 +169,7 @@ func TestRegisterAndDeregister(t *testing.T) {
id2: "first change",
},
}, gots)

gotlock.Unlock()
cli.DeregisterConfig(ConfigParam{
Key: "k1",
nameSpace: "n1",
Expand All @@ -185,7 +187,7 @@ func TestRegisterAndDeregister(t *testing.T) {
}, "second change")

// wait goroutine deal
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)

cli.DeregisterConfig(ConfigParam{
Key: "k1",
Expand All @@ -197,6 +199,7 @@ func TestRegisterAndDeregister(t *testing.T) {
fake.len--
fake.Unlock()

gotlock.Lock()
assert.Equal(t, map[configParamKey]map[int64]string{
{
Key: "k1",
Expand All @@ -207,13 +210,18 @@ func TestRegisterAndDeregister(t *testing.T) {
id2: "first change",
},
}, gots)
gotlock.Unlock()

fake.change(configParamKey{
Key: "k1",
NameSpace: "n1",
Cluster: "c1",
}, "third change")
time.Sleep(2 * time.Second)

// wait goroutine deal
time.Sleep(1 * time.Second)

gotlock.Lock()
assert.Equal(t, map[configParamKey]map[int64]string{
{
Key: "k1",
Expand All @@ -224,4 +232,5 @@ func TestRegisterAndDeregister(t *testing.T) {
id2: "first change",
},
}, gots)
gotlock.Unlock()
}

0 comments on commit 442b66d

Please sign in to comment.