Skip to content

Commit d7bae5d

Browse files
committed
fix: 解决测试用例data race问题
1 parent eb89dc6 commit d7bae5d

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

memory/priority/cache.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
// Copyright 2023 ecodeclub
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+
115
package priority
216

317
import (
418
"context"
19+
"sync"
20+
"time"
21+
522
"github.com/ecodeclub/ecache"
623
"github.com/ecodeclub/ecache/internal/errs"
724
"github.com/ecodeclub/ekit"
825
"github.com/ecodeclub/ekit/queue"
9-
"sync"
10-
"time"
1126
)
1227

1328
type Option func(c *Cache)

memory/priority/cache_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
// Copyright 2023 ecodeclub
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+
115
package priority
216

317
import (
418
"context"
19+
"testing"
20+
"time"
21+
522
"github.com/ecodeclub/ecache"
623
"github.com/ecodeclub/ecache/internal/errs"
724
"github.com/stretchr/testify/assert"
8-
"testing"
9-
"time"
1025
)
1126

1227
func TestCache_Set(t *testing.T) {
@@ -337,19 +352,23 @@ func TestCache_Get(t *testing.T) {
337352
tc.before(tc.cache)
338353

339354
for k, v := range tc.beforeGetIndex {
340-
assert.Equal(t, v.Val, tc.cache.(*Cache).index[k].Val)
355+
node := tc.cache.(*Cache).getNode(k)
356+
357+
assert.Equal(t, v.Val, node.Val)
341358

342-
assert.InDelta(t, v.Dl.Unix(), tc.cache.(*Cache).index[k].Dl.Unix(), 2)
359+
assert.InDelta(t, v.Dl.Unix(), node.Dl.Unix(), 2)
343360
}
344361

345362
res := tc.cache.Get(ctx, tc.key)
346363

347364
assert.Equal(t, len(tc.wantIndex), len(tc.cache.(*Cache).index))
348365

349366
for k, v := range tc.wantIndex {
350-
assert.Equal(t, v.Val, tc.cache.(*Cache).index[k].Val)
367+
node := tc.cache.(*Cache).getNode(k)
351368

352-
assert.InDelta(t, v.Dl.Unix(), tc.cache.(*Cache).index[k].Dl.Unix(), 2)
369+
assert.Equal(t, v.Val, node.Val)
370+
371+
assert.InDelta(t, v.Dl.Unix(), node.Dl.Unix(), 2)
353372
}
354373

355374
assert.Equal(t, tc.wantErr, res.Err)
@@ -363,6 +382,12 @@ func TestCache_Get(t *testing.T) {
363382
}
364383
}
365384

385+
func (c *Cache) getNode(key string) *Node {
386+
c.mu.Lock()
387+
defer c.mu.Unlock()
388+
return c.index[key]
389+
}
390+
366391
func TestCache_GetSet(t *testing.T) {
367392
ctx := context.TODO()
368393

0 commit comments

Comments
 (0)