File: item_test.go

package info (click to toggle)
golang-github-renekroon-ttlcache 2.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 140 kB
  • sloc: makefile: 3
file content (34 lines) | stat: -rw-r--r-- 1,124 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package ttlcache

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestItemExpired(t *testing.T) {
	item := newItem("key", "value", (time.Duration(100) * time.Millisecond))
	assert.Equal(t, item.expired(), false, "Expected item to not be expired")
	<-time.After(200 * time.Millisecond)
	assert.Equal(t, item.expired(), true, "Expected item to be expired once time has passed")
}

func TestItemTouch(t *testing.T) {
	item := newItem("key", "value", (time.Duration(100) * time.Millisecond))
	oldExpireAt := item.expireAt
	<-time.After(50 * time.Millisecond)
	item.touch()
	assert.NotEqual(t, oldExpireAt, item.expireAt, "Expected dates to be different")
	<-time.After(150 * time.Millisecond)
	assert.Equal(t, item.expired(), true, "Expected item to be expired")
	item.touch()
	<-time.After(50 * time.Millisecond)
	assert.Equal(t, item.expired(), false, "Expected item to not be expired")
}

func TestItemWithoutExpiration(t *testing.T) {
	item := newItem("key", "value", ItemNotExpire)
	<-time.After(50 * time.Millisecond)
	assert.Equal(t, item.expired(), false, "Expected item to not be expired")
}