-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimed_test.go
48 lines (43 loc) · 1.08 KB
/
timed_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
package genh
import (
"fmt"
"runtime"
"testing"
"time"
)
func TestTimedMap(t *testing.T) {
n := runtime.NumGoroutine()
var tm TimedMap[string, string]
i := 0
tm.SetUpdateExpireFn("key", func() string {
i++
return fmt.Sprintf("val:%d", i)
}, time.Millisecond*110, time.Millisecond*400)
time.Sleep(time.Millisecond * 100)
if v := tm.Get("key"); v != "val:1" {
t.Fatal("expected val:1, got", v)
}
time.Sleep(time.Millisecond * 350)
if v := tm.Get("key"); v != "val:5" {
t.Fatal("expected val:5, got", v)
}
time.Sleep(time.Millisecond * 500)
if v := tm.Get("key"); v != "" {
t.Fatal("expected empty, got", v)
}
tm.Set("key2", "val1", time.Millisecond*100)
time.Sleep(time.Millisecond * 350)
if v := tm.Get("key2"); v != "" {
t.Fatal("expected empty, got", v)
}
tm.Set("key2", "val2", time.Minute)
time.Sleep(time.Millisecond * 350)
tm.Set("key2", "val3", time.Minute)
if v, ok := tm.GetOk("key2"); v != "val3" || !ok {
t.Fatalf("unexpected val3: %v", v)
}
tm.Delete("key2")
if nn := runtime.NumGoroutine(); nn != n {
t.Fatalf("goroutine leak: %d > %d", nn, n)
}
}