Skip to content

Commit 2c02b82

Browse files
groupcache: add go.mod, update for Go 1.24 (#175)
Add a go.mod file As of Go 1.24 rand.Seed is a no-op. Change TestPeers to use an explicit rand.Rand instead.
1 parent 41bb18b commit 2c02b82

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/golang/groupcache
2+
3+
go 1.20
4+
5+
require github.com/golang/protobuf v1.5.4
6+
7+
require google.golang.org/protobuf v1.33.0 // indirect

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2+
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
3+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
4+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
5+
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
6+
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

groupcache.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ type Group struct {
171171

172172
// Stats are statistics on the group.
173173
Stats Stats
174+
175+
// rand is only non-nil when testing,
176+
// to get predictable results in TestPeers.
177+
rand *rand.Rand
174178
}
175179

176180
// flightGroup is defined as an interface which flightgroup.Group
@@ -315,7 +319,13 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (
315319
// TODO(bradfitz): use res.MinuteQps or something smart to
316320
// conditionally populate hotCache. For now just do it some
317321
// percentage of the time.
318-
if rand.Intn(10) == 0 {
322+
var pop bool
323+
if g.rand != nil {
324+
pop = g.rand.Intn(10) == 0
325+
} else {
326+
pop = rand.Intn(10) == 0
327+
}
328+
if pop {
319329
g.populateCache(key, value, &g.hotCache)
320330
}
321331
return value, nil

groupcache_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ func (p fakePeers) PickPeer(key string) (peer ProtoGetter, ok bool) {
253253
// TestPeers tests that peers (virtual, in-process) are hit, and how much.
254254
func TestPeers(t *testing.T) {
255255
once.Do(testSetup)
256-
rand.Seed(123)
257256
peer0 := &fakePeer{}
258257
peer1 := &fakePeer{}
259258
peer2 := &fakePeer{}
@@ -265,6 +264,7 @@ func TestPeers(t *testing.T) {
265264
return dest.SetString("got:" + key)
266265
}
267266
testGroup := newGroup("TestPeers-group", cacheSize, GetterFunc(getter), peerList)
267+
testGroup.rand = rand.New(rand.NewSource(123))
268268
run := func(name string, n int, wantSummary string) {
269269
// Reset counters
270270
localHits = 0

0 commit comments

Comments
 (0)