You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/lib/ttlcache/eviction_test.go

98 lines
2.4 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package ttlcache
import (
"container/heap"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/hashicorp/consul/sdk/testutil"
)
var _ heap.Interface = (*entryHeap)(nil)
func TestExpiryHeap(t *testing.T) {
h := NewExpiryHeap()
ch := h.NotifyCh
var entry, entry2, entry3 *Entry
// Init, shouldn't trigger anything
testNoMessage(t, ch)
testutil.RunStep(t, "add an entry", func(t *testing.T) {
entry = h.Add("foo", 100*time.Millisecond)
assert.Equal(t, 0, entry.heapIndex)
testMessage(t, ch)
testNoMessage(t, ch) // exactly one asserted above
})
testutil.RunStep(t, "add a second entry in front", func(t *testing.T) {
entry2 = h.Add("bar", 50*time.Millisecond)
assert.Equal(t, 0, entry2.heapIndex)
assert.Equal(t, 1, entry.heapIndex)
testMessage(t, ch)
testNoMessage(t, ch) // exactly one asserted above
})
testutil.RunStep(t, "add a third entry at the end", func(t *testing.T) {
entry3 = h.Add("baz", 1000*time.Millisecond)
assert.Equal(t, 2, entry3.heapIndex)
testNoMessage(t, ch) // no notify cause index 0 stayed the same
})
testutil.RunStep(t, "remove the first entry", func(t *testing.T) {
h.Remove(0)
assert.Equal(t, 0, entry.heapIndex)
assert.Equal(t, 1, entry3.heapIndex)
testMessage(t, ch)
testNoMessage(t, ch)
})
testutil.RunStep(t, "update so that entry3 expires first", func(t *testing.T) {
h.Update(entry.heapIndex, 2000*time.Millisecond)
assert.Equal(t, 1, entry.heapIndex)
assert.Equal(t, 0, entry3.heapIndex)
testMessage(t, ch)
testNoMessage(t, ch)
})
testutil.RunStep(t, "0th element change triggers a notify", func(t *testing.T) {
h.Update(entry3.heapIndex, 1500*time.Millisecond)
assert.Equal(t, 1, entry.heapIndex) // no move
assert.Equal(t, 0, entry3.heapIndex)
testMessage(t, ch)
testNoMessage(t, ch) // one message
})
testutil.RunStep(t, "update can not decrease expiry time", func(t *testing.T) {
h.Update(entry.heapIndex, 100*time.Millisecond)
assert.Equal(t, 1, entry.heapIndex) // no move
assert.Equal(t, 0, entry3.heapIndex)
testNoMessage(t, ch) // no notify, because no change in the heap
})
}
func testNoMessage(t *testing.T, ch <-chan struct{}) {
t.Helper()
select {
case <-ch:
t.Fatal("should not have a message")
default:
}
}
func testMessage(t *testing.T, ch <-chan struct{}) {
t.Helper()
select {
case <-ch:
default:
t.Fatal("should have a message")
}
}