|
@@ -5,6 +5,7 @@ package history
|
5
|
5
|
|
6
|
6
|
import (
|
7
|
7
|
"reflect"
|
|
8
|
+ "strconv"
|
8
|
9
|
"testing"
|
9
|
10
|
"time"
|
10
|
11
|
)
|
|
@@ -16,7 +17,7 @@ const (
|
16
|
17
|
func TestEmptyBuffer(t *testing.T) {
|
17
|
18
|
pastTime := easyParse(timeFormat)
|
18
|
19
|
|
19
|
|
- buf := NewHistoryBuffer(0)
|
|
20
|
+ buf := NewHistoryBuffer(0, 0)
|
20
|
21
|
if buf.Enabled() {
|
21
|
22
|
t.Error("the buffer of size 0 must be considered disabled")
|
22
|
23
|
}
|
|
@@ -33,7 +34,7 @@ func TestEmptyBuffer(t *testing.T) {
|
33
|
34
|
t.Error("the empty/disabled buffer should report results as incomplete")
|
34
|
35
|
}
|
35
|
36
|
|
36
|
|
- buf.Resize(1)
|
|
37
|
+ buf.Resize(1, 0)
|
37
|
38
|
if !buf.Enabled() {
|
38
|
39
|
t.Error("the buffer of size 1 must be considered enabled")
|
39
|
40
|
}
|
|
@@ -102,7 +103,7 @@ func assertEqual(supplied, expected interface{}, t *testing.T) {
|
102
|
103
|
func TestBuffer(t *testing.T) {
|
103
|
104
|
start := easyParse("2006-01-01 00:00:00Z")
|
104
|
105
|
|
105
|
|
- buf := NewHistoryBuffer(3)
|
|
106
|
+ buf := NewHistoryBuffer(3, 0)
|
106
|
107
|
buf.Add(easyItem("testnick0", "2006-01-01 15:04:05Z"))
|
107
|
108
|
|
108
|
109
|
buf.Add(easyItem("testnick1", "2006-01-02 15:04:05Z"))
|
|
@@ -128,12 +129,12 @@ func TestBuffer(t *testing.T) {
|
128
|
129
|
assertEqual(toNicks(since), []string{"testnick1"}, t)
|
129
|
130
|
|
130
|
131
|
// shrink the buffer, cutting off testnick1
|
131
|
|
- buf.Resize(2)
|
|
132
|
+ buf.Resize(2, 0)
|
132
|
133
|
since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), time.Now(), false, 0)
|
133
|
134
|
assertEqual(complete, false, t)
|
134
|
135
|
assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
|
135
|
136
|
|
136
|
|
- buf.Resize(5)
|
|
137
|
+ buf.Resize(5, 0)
|
137
|
138
|
buf.Add(easyItem("testnick4", "2006-01-05 15:04:05Z"))
|
138
|
139
|
buf.Add(easyItem("testnick5", "2006-01-06 15:04:05Z"))
|
139
|
140
|
buf.Add(easyItem("testnick6", "2006-01-07 15:04:05Z"))
|
|
@@ -145,3 +146,80 @@ func TestBuffer(t *testing.T) {
|
145
|
146
|
since, _ = buf.Between(easyParse("2006-01-03 00:00:00Z"), time.Now(), true, 2)
|
146
|
147
|
assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
|
147
|
148
|
}
|
|
149
|
+
|
|
150
|
+func autoItem(id int, t time.Time) (result Item) {
|
|
151
|
+ result.Message.Time = t
|
|
152
|
+ result.Nick = strconv.Itoa(id)
|
|
153
|
+ return
|
|
154
|
+}
|
|
155
|
+
|
|
156
|
+func atoi(s string) int {
|
|
157
|
+ result, err := strconv.Atoi(s)
|
|
158
|
+ if err != nil {
|
|
159
|
+ panic(err)
|
|
160
|
+ }
|
|
161
|
+ return result
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+func TestAutoresize(t *testing.T) {
|
|
165
|
+ now := easyParse("2006-01-01 00:00:00Z")
|
|
166
|
+ nowFunc := func() time.Time {
|
|
167
|
+ return now
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ buf := NewHistoryBuffer(128, time.Hour)
|
|
171
|
+ buf.nowFunc = nowFunc
|
|
172
|
+
|
|
173
|
+ // add items slowly (one every 10 minutes): the buffer should not expand
|
|
174
|
+ // beyond initialAutoSize
|
|
175
|
+ id := 0
|
|
176
|
+ for i := 0; i < 72; i += 1 {
|
|
177
|
+ buf.Add(autoItem(id, now))
|
|
178
|
+ if initialAutoSize < buf.length() {
|
|
179
|
+ t.Errorf("buffer incorrectly resized above %d to %d", initialAutoSize, buf.length())
|
|
180
|
+ }
|
|
181
|
+ now = now.Add(time.Minute * 10)
|
|
182
|
+ id += 1
|
|
183
|
+ }
|
|
184
|
+ items := buf.Latest(0)
|
|
185
|
+ assertEqual(len(items), initialAutoSize, t)
|
|
186
|
+ assertEqual(atoi(items[0].Nick), 40, t)
|
|
187
|
+ assertEqual(atoi(items[len(items)-1].Nick), 71, t)
|
|
188
|
+
|
|
189
|
+ // dump 100 items in very fast:
|
|
190
|
+ for i := 0; i < 100; i += 1 {
|
|
191
|
+ buf.Add(autoItem(id, now))
|
|
192
|
+ now = now.Add(time.Second)
|
|
193
|
+ id += 1
|
|
194
|
+ }
|
|
195
|
+ // ok, 5 items from the first batch are still in the 1-hour window;
|
|
196
|
+ // we should overwrite until only those 5 are left, then start expanding
|
|
197
|
+ // the buffer so that it retains those 5 and the 100 new items
|
|
198
|
+ items = buf.Latest(0)
|
|
199
|
+ assertEqual(len(items), 105, t)
|
|
200
|
+ assertEqual(atoi(items[0].Nick), 67, t)
|
|
201
|
+ assertEqual(atoi(items[len(items)-1].Nick), 171, t)
|
|
202
|
+
|
|
203
|
+ // another 100 items very fast:
|
|
204
|
+ for i := 0; i < 100; i += 1 {
|
|
205
|
+ buf.Add(autoItem(id, now))
|
|
206
|
+ now = now.Add(time.Second)
|
|
207
|
+ id += 1
|
|
208
|
+ }
|
|
209
|
+ // should fill up to the maximum size of 128 and start overwriting
|
|
210
|
+ items = buf.Latest(0)
|
|
211
|
+ assertEqual(len(items), 128, t)
|
|
212
|
+ assertEqual(atoi(items[0].Nick), 144, t)
|
|
213
|
+ assertEqual(atoi(items[len(items)-1].Nick), 271, t)
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+func TestRoundUp(t *testing.T) {
|
|
217
|
+ assertEqual(roundUpToPowerOfTwo(2), 2, t)
|
|
218
|
+ assertEqual(roundUpToPowerOfTwo(3), 4, t)
|
|
219
|
+ assertEqual(roundUpToPowerOfTwo(64), 64, t)
|
|
220
|
+ assertEqual(roundUpToPowerOfTwo(65), 128, t)
|
|
221
|
+ assertEqual(roundUpToPowerOfTwo(100), 128, t)
|
|
222
|
+ assertEqual(roundUpToPowerOfTwo(1000), 1024, t)
|
|
223
|
+ assertEqual(roundUpToPowerOfTwo(1025), 2048, t)
|
|
224
|
+ assertEqual(roundUpToPowerOfTwo(269435457), 536870912, t)
|
|
225
|
+}
|