-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwindowed_map_test.go
96 lines (86 loc) · 2.68 KB
/
windowed_map_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
// Copyright 2014 The zephyr-go authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zephyr
import (
"testing"
"time"
"github.com/zephyr-im/zephyr-go/zephyrtest"
)
func TestWindowedMap(t *testing.T) {
clock := zephyrtest.NewMockClock()
uid1 := MakeUID(clientAddr.IP, time.Unix(1, 0))
uid2 := MakeUID(clientAddr.IP, time.Unix(2, 0))
w := NewWindowedMapFull(5*time.Second, clock)
// Initially empty.
if w.Len() != 0 {
t.Errorf("w.Len() = %d; want 0", w.Len())
}
if val, ok := w.Lookup(uid1); ok {
t.Errorf("w.Lookup(uid1) returned %v, true; want false", val)
}
if val, ok := w.Remove(uid1); ok {
t.Errorf("w.Remove(uid1) returned %v, true; want false", val)
}
// Insert something and remove it.
w.Put(uid1, 1)
if val, ok := w.Lookup(uid1); !ok || val.(int) != 1 {
t.Errorf("w.Lookup(uid1) returned %v, %v; want 1", val, ok)
}
if w.Len() != 1 {
t.Errorf("w.Len() = %d; want 1", w.Len())
}
if val, ok := w.Remove(uid1); !ok || val.(int) != 1 {
t.Errorf("w.Remove(uid1) returned %v, %v; want 1", val, ok)
}
// Insert something and let it expire.
w.Put(uid1, 1)
clock.Advance(5 * time.Second)
if val, ok := w.Lookup(uid1); ok {
t.Errorf("w.Lookup(uid1) returned %v, true; want false", val)
}
if w.Len() != 0 {
t.Errorf("w.Len() = %d; want 0", w.Len())
}
// Looking up a value updates the expiration time.
w.Put(uid1, 1)
w.Put(uid2, 2)
clock.Advance(2 * time.Second)
w.Lookup(uid1)
clock.Advance(3 * time.Second)
if val, ok := w.Lookup(uid2); ok {
t.Errorf("w.Lookup(uid2) returned %v, true; want false", val)
}
if val, ok := w.Lookup(uid1); !ok || val.(int) != 1 {
t.Errorf("w.Lookup(uid1) returned %v, %v; want 1", val, ok)
}
if w.Len() != 1 {
t.Errorf("w.Len() = %d; want 1", w.Len())
}
clock.Advance(5 * time.Second)
if val, ok := w.Lookup(uid1); ok {
t.Errorf("w.Lookup(uid1) returned %v, true; want false", val)
}
if w.Len() != 0 {
t.Errorf("w.Len() = %d; want 0", w.Len())
}
// Overriding values works.
w.Put(uid1, 1)
w.Put(uid1, 2)
if val, ok := w.Lookup(uid1); !ok || val.(int) != 2 {
t.Errorf("w.Lookup(uid1) returned %v, %v; want 2", val, ok)
}
if w.Len() != 1 {
t.Errorf("w.Len() = %d; want 1", w.Len())
}
}