-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub_test.go
More file actions
124 lines (99 loc) · 2.54 KB
/
pubsub_test.go
File metadata and controls
124 lines (99 loc) · 2.54 KB
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
package cachegrid
import (
"sync/atomic"
"testing"
"time"
)
func TestSubscribeReceivesEvents(t *testing.T) {
c := newTestCache(t)
sub := c.Subscribe("user:*")
defer sub.Close()
c.Set("user:1", "alice", time.Minute)
select {
case evt := <-sub.Events():
if evt.Key != "user:1" {
t.Fatalf("expected key user:1, got %s", evt.Key)
}
if evt.Type != EventSet {
t.Fatalf("expected EventSet, got %s", evt.Type)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
}
func TestSubscribeFiltersByPattern(t *testing.T) {
c := newTestCache(t)
sub := c.Subscribe("order:*")
defer sub.Close()
// This should NOT match the pattern
c.Set("user:1", "alice", time.Minute)
// This should match
c.Set("order:1", "pizza", time.Minute)
select {
case evt := <-sub.Events():
if evt.Key != "order:1" {
t.Fatalf("expected order:1, got %s", evt.Key)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for matching event")
}
}
func TestSubscribeDeleteEvent(t *testing.T) {
c := newTestCache(t)
sub := c.Subscribe("*")
defer sub.Close()
c.Set("temp", "val", time.Minute)
// drain set event
<-sub.Events()
c.Delete("temp")
select {
case evt := <-sub.Events():
if evt.Type != EventDelete {
t.Fatalf("expected EventDelete, got %s", evt.Type)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for delete event")
}
}
func TestOnHitMissCallbacks(t *testing.T) {
c := newTestCache(t)
var hits, misses atomic.Int64
c.OnHit(func(key string) { hits.Add(1) })
c.OnMiss(func(key string) { misses.Add(1) })
c.Set("exists", "val", time.Minute)
var dest string
c.Get("exists", &dest) // hit
c.Get("missing", &dest) // miss
time.Sleep(10 * time.Millisecond)
if hits.Load() != 1 {
t.Fatalf("expected 1 hit, got %d", hits.Load())
}
if misses.Load() != 1 {
t.Fatalf("expected 1 miss, got %d", misses.Load())
}
}
func TestOnSetDeleteCallbacks(t *testing.T) {
c := newTestCache(t)
var sets, deletes atomic.Int64
c.OnSet(func(key string, value interface{}) { sets.Add(1) })
c.OnDelete(func(key string) { deletes.Add(1) })
c.Set("x", "v", time.Minute)
c.Delete("x")
time.Sleep(10 * time.Millisecond)
if sets.Load() != 1 {
t.Fatalf("expected 1 set, got %d", sets.Load())
}
if deletes.Load() != 1 {
t.Fatalf("expected 1 delete, got %d", deletes.Load())
}
}
func TestSubscriptionClose(t *testing.T) {
c := newTestCache(t)
sub := c.Subscribe("*")
sub.Close()
// Events channel should be closed
_, ok := <-sub.Events()
if ok {
t.Fatal("expected channel to be closed after Close()")
}
}