-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache_test.go
126 lines (99 loc) · 2.92 KB
/
cache_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
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
125
126
package plc
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testTagName = "TEST_TAG"
func newCacheForTesting() (*Cache, FakeReadWriter) {
fakeRW := FakeReadWriter(map[string]interface{}{})
return NewCache(fakeRW), fakeRW
}
func TestNewCache(t *testing.T) {
cache, _ := newCacheForTesting()
assert.NotNil(t, cache, "NewCache should return a non-nil object")
}
func TestCachePassthroughInt(t *testing.T) {
cache, fakeRW := newCacheForTesting()
fakeRW[testTagName] = 7
var actual int
err := cache.ReadTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 7, actual)
}
func TestCachePassthroughTwiceInt(t *testing.T) {
cache, fakeRW := newCacheForTesting()
fakeRW[testTagName] = 7
var unused int
err := cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
fakeRW[testTagName] = 85
var actual int
err = cache.ReadTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 85, actual)
}
func TestCacheInt(t *testing.T) {
cache, fakeRW := newCacheForTesting()
fakeRW[testTagName] = 7
var unused int
err := cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
// Mess up both values to ensure we're not just getting a pointer
unused++
fakeRW[testTagName] = 13
var actual int
err = cache.ReadCachedTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 7, actual)
}
func TestCacheAfterUpdateInt(t *testing.T) {
cache, fakeRW := newCacheForTesting()
fakeRW[testTagName] = 7
// Update the cache with a value we'll never look at
var unused int
err := cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
// Set the value to 85 and read it, expecting this to end up in the cache
fakeRW[testTagName] = 85
err = cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
var actual int
err = cache.ReadCachedTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 85, actual)
}
const secondTestTagName = "secondTag"
func TestCacheSecondTag(t *testing.T) {
cache, fakeRW := newCacheForTesting()
fakeRW[testTagName] = 7
fakeRW[secondTestTagName] = 99
var unused int
err := cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
err = cache.ReadTag(secondTestTagName, &unused)
require.NoError(t, err)
// Ensure BOTH are read correctly
var actual int
err = cache.ReadCachedTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 7, actual)
err = cache.ReadCachedTag(secondTestTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 99, actual)
}
func TestCacheReader(t *testing.T) {
cache, fakeRW := newCacheForTesting()
cacheReader := cache.CacheReader()
fakeRW[testTagName] = 7
var unused int
err := cache.ReadTag(testTagName, &unused)
require.NoError(t, err)
// Mess up both values to ensure we're not just getting a pointer
unused++
fakeRW[testTagName] = 13
var actual int
err = cacheReader.ReadTag(testTagName, &actual)
assert.NoError(t, err)
assert.Equal(t, 7, actual)
}