-
Notifications
You must be signed in to change notification settings - Fork 0
/
timap_test.go
174 lines (162 loc) · 4.42 KB
/
timap_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package timap
import (
"testing"
"time"
)
// the existence of this private method makes sense for testing only.
func (tm *timap) watchers() (total int) {
tm.timers.Range(func(key interface{}, value interface{}) bool {
total++
return true
})
return
}
func Test_Timap(t *testing.T) {
// create new Timap with unlimited life time for key-value pair by default
m := New(0)
t.Run("Add temporrary key-value pair that has to be deleted", func(t *testing.T) {
m.Store("delete me", "as soon as possible", 10*time.Millisecond)
if total := m.(*timap).watchers(); total != 1 {
t.Errorf(`should have one watcher but has %d`, total)
}
d, ok := m.Load("delete me")
if !ok {
t.Error(`should already have key "delete me"`)
}
if d != "as soon as possible" {
t.Error(`wrong value for key "delete me"`)
}
})
t.Run("Delete temporrary key-value pair and its watcher", func(t *testing.T) {
m.Delete("delete me")
if total := m.(*timap).watchers(); total != 0 {
t.Errorf(`should not have any watchers but has %d`, total)
}
d, ok := m.Load("delete me")
if ok {
t.Error(`value with key "delete me" should have been deleted`)
}
if d != nil {
t.Errorf(`shoul be nil but has value "%v"`, d)
}
})
t.Run("Add first temporrary key-value pair", func(t *testing.T) {
m.Store("a", 42, 10*time.Millisecond)
if total := m.(*timap).watchers(); total != 1 {
t.Errorf(`should have one watcher but has %d`, total)
}
a, ok := m.Load("a")
if !ok {
t.Error(`should already have key "a"`)
}
if a != 42 {
t.Error(`wrong value for "a"`)
}
})
t.Run("Add second temporrary key-value pair", func(t *testing.T) {
m.Store("b", "second", 80*time.Millisecond)
if total := m.(*timap).watchers(); total != 2 {
t.Errorf(`should have two watchers but has %d`, total)
}
b, ok := m.Load("b")
if !ok {
t.Error(`should already have key "b"`)
}
if b != "second" {
t.Error(`wrong value for "b"`)
}
})
t.Run("Add third persistent key-value pair", func(t *testing.T) {
m.Store("c", [42]int{})
if m.(*timap).watchers() != 2 {
t.Error(`should not add any watchers`)
}
c, ok := m.Load("c")
if !ok {
t.Error(`should already have key "c"`)
}
if c != [42]int{} {
t.Error(`wrong value for "c"`)
}
})
t.Run("Replace first key-value pair with another tepmorrary pair", func(t *testing.T) {
m.Store("a", "new A", 50*time.Millisecond)
if total := m.(*timap).watchers(); total != 2 {
t.Errorf(`should still have two values but has %d`, total)
}
d, ok := m.Load("a")
if !ok {
t.Error(`should already have key "a"`)
}
if d != "new A" {
t.Error(`wrong new value for "a"`)
}
})
t.Run("Check values after 30 milliseconds", func(t *testing.T) {
time.Sleep(30 * time.Millisecond)
if total := m.(*timap).watchers(); total != 2 {
t.Errorf("should still have two watchers but has %d", total)
}
a, ok := m.Load("a")
if !ok {
t.Error(`should still have key "a" because its timer was redefined`)
}
if a != "new A" {
t.Errorf(`"a" should have new value but has value "%v"`, a)
}
b, ok := m.Load("b")
if !ok {
t.Error(`should still have key "b"`)
}
if b != "second" {
t.Error(`value of "b" should not be changed`)
}
})
t.Run("Check values after 70 milliseconds", func(t *testing.T) {
time.Sleep(40 * time.Millisecond)
if total := m.(*timap).watchers(); total != 1 {
t.Errorf("should have exactly one watcher but has %d", total)
}
a, ok := m.Load("a")
if ok {
t.Error(`"a" should have been expired and deleted`)
}
if a != nil {
t.Errorf(`"a" should be nil but has value "%v"`, a)
}
b, ok := m.Load("b")
if !ok {
t.Error(`should still have key "b"`)
}
if b != "second" {
t.Error(`value of "b" should not be changed`)
}
})
t.Run("Check values after 100 milliseconds", func(t *testing.T) {
time.Sleep(30 * time.Millisecond)
if total := m.(*timap).watchers(); total != 0 {
t.Errorf("should not have any watchers but has %d", total)
}
a, ok := m.Load("a")
if ok {
t.Error(`"a" should have never been expired or deleted`)
}
if a != nil {
t.Errorf(`"a" should be nil but has value "%v"`, a)
}
b, ok := m.Load("b")
if ok {
t.Error(`"b" should have been expired and deleted`)
}
if b != nil {
t.Errorf(`"b" should be nil but has value "%v"`, b)
}
c, ok := m.Load("c")
if !ok {
t.Error(`"c" should have never been expired`)
}
if c != [42]int{} {
t.Error(`value of "c" should not be changed`)
}
})
}