-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
55 lines (44 loc) · 1.13 KB
/
scanner_test.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package filecache_test
import (
"context"
"testing"
"time"
"github.com/kukymbr/filecache/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScanner(t *testing.T) {
fc, err := filecache.New(getTarget(t, "scanner"))
require.NoError(t, err)
// Prepare cache
{
_, err = fc.WriteData(context.Background(), "test1", []byte("value1"))
require.NoError(t, err)
_, err = fc.WriteData(
context.Background(),
"test2",
[]byte("value2"),
filecache.ItemOptions{TTL: time.Hour},
)
require.NoError(t, err)
_, err = fc.WriteData(
context.Background(),
"test3",
[]byte("value3"),
filecache.ItemOptions{TTL: time.Millisecond},
)
require.NoError(t, err)
}
time.Sleep(2 * time.Millisecond)
scanner := filecache.NewScanner(fc.GetPath())
scannedKeys := make([]string, 0)
err = scanner.Scan(func(entry filecache.ScanEntry) error {
scannedKeys = append(scannedKeys, entry.Key)
return nil
})
assert.NoError(t, err)
assert.Len(t, scannedKeys, 2)
assert.Contains(t, scannedKeys, "test1")
assert.Contains(t, scannedKeys, "test2")
assert.NotContains(t, scannedKeys, "test3")
}