forked from benthosdev/benthos
/
cache_test_definitions.go
124 lines (103 loc) · 3.08 KB
/
cache_test_definitions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package integration
import (
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/benthosdev/benthos/v4/internal/component"
)
// CacheTestOpenClose checks that the cache can be started, an item added, and
// then stopped.
func CacheTestOpenClose() CacheTestDefinition {
return namedCacheTest(
"can open and close",
func(t *testing.T, env *cacheTestEnvironment) {
cache := initCache(t, env)
t.Cleanup(func() {
closeCache(t, cache)
})
require.NoError(t, cache.Add(env.ctx, "foo", []byte("bar"), nil))
res, err := cache.Get(env.ctx, "foo")
require.NoError(t, err)
assert.Equal(t, "bar", string(res))
},
)
}
// CacheTestMissingKey checks that we get an error on missing key.
func CacheTestMissingKey() CacheTestDefinition {
return namedCacheTest(
"return consistent error on missing key",
func(t *testing.T, env *cacheTestEnvironment) {
cache := initCache(t, env)
t.Cleanup(func() {
closeCache(t, cache)
})
_, err := cache.Get(env.ctx, "missingkey")
assert.EqualError(t, err, "key does not exist")
},
)
}
// CacheTestDoubleAdd ensures that a double add returns an error.
func CacheTestDoubleAdd() CacheTestDefinition {
return namedCacheTest(
"add with duplicate key fails",
func(t *testing.T, env *cacheTestEnvironment) {
cache := initCache(t, env)
t.Cleanup(func() {
closeCache(t, cache)
})
require.NoError(t, cache.Add(env.ctx, "addkey", []byte("first"), nil))
assert.Eventually(t, func() bool {
return errors.Is(cache.Add(env.ctx, "addkey", []byte("second"), nil), component.ErrKeyAlreadyExists)
}, time.Minute, time.Second)
res, err := cache.Get(env.ctx, "addkey")
require.NoError(t, err)
assert.Equal(t, "first", string(res))
},
)
}
// CacheTestDelete checks that deletes work.
func CacheTestDelete() CacheTestDefinition {
return namedCacheTest(
"can set and delete keys",
func(t *testing.T, env *cacheTestEnvironment) {
cache := initCache(t, env)
t.Cleanup(func() {
closeCache(t, cache)
})
require.NoError(t, cache.Add(env.ctx, "addkey", []byte("first"), nil))
res, err := cache.Get(env.ctx, "addkey")
require.NoError(t, err)
assert.Equal(t, "first", string(res))
require.NoError(t, cache.Delete(env.ctx, "addkey"))
_, err = cache.Get(env.ctx, "addkey")
require.EqualError(t, err, "key does not exist")
},
)
}
// CacheTestGetAndSet checks that we can set and then get n items.
func CacheTestGetAndSet(n int) CacheTestDefinition {
return namedCacheTest(
"can get and set",
func(t *testing.T, env *cacheTestEnvironment) {
cache := initCache(t, env)
t.Cleanup(func() {
closeCache(t, cache)
})
for i := 0; i < n; i++ {
key := fmt.Sprintf("key:%v", i)
value := fmt.Sprintf("value:%v", i)
require.NoError(t, cache.Set(env.ctx, key, []byte(value), nil))
}
for i := 0; i < n; i++ {
key := fmt.Sprintf("key:%v", i)
value := fmt.Sprintf("value:%v", i)
res, err := cache.Get(env.ctx, key)
require.NoError(t, err)
assert.Equal(t, value, string(res))
}
},
)
}