Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
pull-requests: read
jobs:
golangci:
name: lint
Expand All @@ -32,7 +32,7 @@ jobs:
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ jobs:
with:
go-version: ${{ matrix.go_version }}

- name: build
run: go build ./...

- name: vet
run: go vet ./...

- name: run tests
run: go test ./...
run: go test -race ./...
52 changes: 23 additions & 29 deletions pkg/cache/ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,45 +96,39 @@ func TestMaxItemsEvictsOldest(t *testing.T) {
func TestCallbacks(t *testing.T) {
instance := NewTTLMap(10, "", "")

evictedCallback := false
// Callbacks fire in their own goroutines, so synchronise on channels
// rather than reading shared state after a sleep.
evicted := make(chan [2]string, 1)
added := make(chan [2]string, 1)

instance.OnItemDeleted(func(key string, value interface{}, expiresAt time.Time) {
if key != "key4" {
t.Fatalf("Expected key to be key4, got %s", key)
}

if value != "value4" {
t.Fatalf("Expected value to be value4, got %s", value)
}

evictedCallback = true
str, _ := value.(string)
evicted <- [2]string{key, str}
})

addedCallback := false

instance.OnItemAdded(func(key string, value interface{}, expiresAt time.Time) {
if key != "key4" {
t.Fatalf("Expected key to be key4, got %s", key)
}

if value != "value4" {
t.Fatalf("Expected value to be value4, got %s", value)
}

addedCallback = true
str, _ := value.(string)
added <- [2]string{key, str}
})

instance.Add("key4", "value4", time.Now().Add(time.Hour), false)
instance.Delete("key4")

time.Sleep(time.Second * 1)

if !evictedCallback {
t.Fatalf("Expected evicted callback to have been called")
}

if !addedCallback {
t.Fatalf("Expected added callback to have been called")
for _, tc := range []struct {
name string
ch chan [2]string
}{
{"added", added},
{"evicted", evicted},
} {
select {
case got := <-tc.ch:
if got[0] != "key4" || got[1] != "value4" {
t.Fatalf("%s callback: expected key4/value4, got %s/%s", tc.name, got[0], got[1])
}
case <-time.After(time.Second * 5):
t.Fatalf("Expected %s callback to have been called", tc.name)
}
}
}

Expand Down
Loading