Skip to content

Commit

Permalink
renamed GetOrAdd to GetOrAddFunc, return error if constructor fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dinghram committed Jan 19, 2024
1 parent 48bb232 commit d25bb3b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions expirable/expirable_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ func TestLRUConcurrency(t *testing.T) {
}
}

func TestLRUGetOrAddConcurrency(t *testing.T) {
func TestLRUGetOrAddFuncConcurrency(t *testing.T) {
lc := NewLRU[string, string](0, nil, 0)
wg := sync.WaitGroup{}
wg.Add(1000)
var evictedCount int32
var addedCount int32
for i := 0; i < 1000; i++ {
go func(i int) {
_, added, evicted := lc.GetOrAdd(fmt.Sprintf("key-%d", i/10), func() string { return fmt.Sprintf("val-%d", i/10) })
_, added, evicted, _ := lc.GetOrAddFunc(fmt.Sprintf("key-%d", i/10), func() (string, error) { return fmt.Sprintf("val-%d", i/10), nil })
if evicted {
atomic.AddInt32(&evictedCount, 1)
}
Expand All @@ -359,10 +359,10 @@ func TestLRUGetOrAddConcurrency(t *testing.T) {
t.Fatalf("length differs from expected")
}
if addedCount != 100 {
t.Fatalf("GetOrAdd: unexpected added count %d", addedCount)
t.Fatalf("GetOrAddFunc: unexpected added count %d", addedCount)
}
if evictedCount > 0 {
t.Fatalf("GetOrAdd: unexpected evicted count %d", evictedCount)
t.Fatalf("GetOrAddFunc: unexpected evicted count %d", evictedCount)
}
}

Expand Down

0 comments on commit d25bb3b

Please sign in to comment.