/
history_test.go
104 lines (86 loc) 路 2.47 KB
/
history_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
package hub
import (
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yosida95/uritemplate"
bolt "go.etcd.io/bbolt"
)
func TestBoltHistory(t *testing.T) {
db, _ := bolt.Open("test.db", 0600, nil)
defer db.Close()
defer os.Remove("test.db")
h := &boltHistory{db, &Options{}}
assert.Implements(t, (*History)(nil), h)
count := 0
assert.Nil(t, h.FindFor(
NewSubscriber(false, map[string]struct{}{}, []string{}, []string{}, []*uritemplate.Template{}, ""),
func(*Update) bool {
count++
return true
}))
assert.Equal(t, 0, count)
assert.Nil(t, h.Add(&Update{Event: Event{ID: "first"}}))
assert.Nil(t, h.Add(&Update{
Targets: map[string]struct{}{"foo": {}},
Topics: []string{"http://example.com/2"},
Event: Event{ID: "second"},
}))
assert.Nil(t, h.Add(&Update{
Targets: map[string]struct{}{"foo": {}, "bar": {}},
Topics: []string{"http://example.com/3", "http://example.com/alt/3"},
Event: Event{ID: "third", Data: "an update"},
}))
assert.Nil(t, h.Add(&Update{
Event: Event{ID: "fourth"},
Topics: []string{"http://example.com/alt/3"},
Targets: map[string]struct{}{"baz": {}},
}))
assert.Nil(t, h.Add(&Update{
Targets: map[string]struct{}{"foo": {}, "bar": {}},
Topics: []string{"http://example.com/alt/3"},
Event: Event{Data: "stop now"},
}))
assert.Nil(t, h.Add(&Update{
Targets: map[string]struct{}{"foo": {}, "bar": {}},
Topics: []string{"http://example.com/alt/3"},
Event: Event{Data: "should not be called"},
}))
h.FindFor(
NewSubscriber(false, map[string]struct{}{"foo": {}}, []string{"http://example.com/alt/3"}, []string{"http://example.com/alt/3"}, []*uritemplate.Template{}, "first"),
func(u *Update) bool {
count++
switch count {
case 1:
assert.Equal(t, "an update", u.Data)
break
case 2:
assert.Equal(t, "stop now", u.Data)
return false
}
return true
},
)
assert.Equal(t, 2, count)
}
func TestPurgeHistory(t *testing.T) {
db, _ := bolt.Open("test.db", 0600, nil)
defer db.Close()
defer os.Remove("test.db")
o := &Options{HistorySize: 5, HistoryCleanupFrequency: 1}
h := &boltHistory{db, o}
for i := 0; i < 12; i++ {
h.Add(&Update{Event: Event{ID: strconv.Itoa(i)}})
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
assert.Equal(t, 5, b.Stats().KeyN)
return nil
})
}
func TestNoHistory(t *testing.T) {
h := &noHistory{}
assert.Nil(t, h.Add(nil))
assert.Nil(t, h.FindFor(nil, func(*Update) bool { return true }))
}