forked from raystack/raccoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_test.go
More file actions
173 lines (141 loc) · 7.88 KB
/
Copy pathtable_test.go
File metadata and controls
173 lines (141 loc) · 7.88 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package connection
import (
"fmt"
"testing"
"github.com/goto/raccoon/identification"
"github.com/stretchr/testify/assert"
)
func TestStore(t *testing.T) {
t.Run("Should store new connection", func(t *testing.T) {
table := NewTable(10)
err := table.Store(identification.Identifier{ID: "user1", Group: ""})
assert.NoError(t, err)
assert.True(t, table.Exists(identification.Identifier{ID: "user1"}))
})
t.Run("Should return max connection reached error when connection is maxed", func(t *testing.T) {
table := NewTable(0)
err := table.Store(identification.Identifier{ID: "user1", Group: ""})
assert.Error(t, err, errMaxConnectionReached)
})
t.Run("Should return duplicated error when connection already exists", func(t *testing.T) {
table := NewTable(2)
err := table.Store(identification.Identifier{ID: "user1", Group: ""})
assert.NoError(t, err)
err = table.Store(identification.Identifier{ID: "user1", Group: ""})
assert.Error(t, err, errConnDuplicated)
})
t.Run("Should remove connection when identifier match", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.Remove(identification.Identifier{ID: "user1", Group: ""})
assert.False(t, table.Exists(identification.Identifier{ID: "user1", Group: ""}))
})
}
func TestStoreBatch(t *testing.T) {
t.Run("Should store new event for a connection", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
})
t.Run("Should not store new event if the connection is not active", func(t *testing.T) {
table := NewTable(10)
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
assert.False(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
})
t.Run("Should store multiple unique events for a connetion", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2")
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2"))
})
t.Run("Should store multiple unique events for multiple connetion", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.Store(identification.Identifier{ID: "user2", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2")
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2"))
})
t.Run("Should remove all the events if connetion is removed or not active", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.Store(identification.Identifier{ID: "user2", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2")
table.Remove(identification.Identifier{ID: "user1", Group: ""})
table.Remove(identification.Identifier{ID: "user2", Group: ""})
assert.False(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
assert.False(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2"))
assert.False(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1"))
assert.False(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2"))
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
})
t.Run("Should be able to remove the batch ", func(t *testing.T) {
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: ""})
table.Store(identification.Identifier{ID: "user2", Group: ""})
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1")
table.StoreBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2")
table.RemoveBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1")
table.RemoveBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1")
assert.False(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-1"))
assert.False(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-1"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2"))
assert.True(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2"))
table.RemoveBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2")
table.RemoveBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2")
assert.False(t, table.HasBatch(identification.Identifier{ID: "user1", Group: ""}, "request-id-2"))
assert.False(t, table.HasBatch(identification.Identifier{ID: "user2", Group: ""}, "request-id-2"))
table.RemoveBatch(identification.Identifier{ID: "user1", Group: ""}, "")
})
}
func TestRangeConnectionPerGroup(t *testing.T) {
t.Run("Should iterate over all the group on the table with the count", func(t *testing.T) {
//set up the table data
table := NewTable(10)
table.Store(identification.Identifier{ID: "user1", Group: "group1"})
table.Store(identification.Identifier{ID: "user2", Group: "group1"})
table.Store(identification.Identifier{ID: "user3", Group: "group1"})
table.Store(identification.Identifier{ID: "user1", Group: "group2"})
table.Store(identification.Identifier{ID: "user2", Group: "group2"})
// Track callback calls
called := make(map[string]int)
table.RangeConnectionPerGroup(func(k string, v int) {
called[k] = v
})
// Verify all keys are present
assert.Equal(t, len(table.counter), len(called))
// Verify values match
for k, v := range table.counter {
assert.Equal(t, v, called[k])
}
})
}
func BenchmarkStoreBatch(b *testing.B) {
table := NewTable(b.N)
for i := 0; i < b.N; i++ {
go func(x int) {
userId := fmt.Sprintf("%s-%d", "user", x)
batchId := fmt.Sprintf("%s-%d", "equest-id-", x)
table.Store(identification.Identifier{ID: userId, Group: ""})
table.StoreBatch(identification.Identifier{ID: userId, Group: ""}, batchId)
assert.True(b, table.HasBatch(identification.Identifier{ID: userId, Group: ""}, batchId))
table.RemoveBatch(identification.Identifier{ID: userId, Group: ""}, batchId)
assert.False(b, table.HasBatch(identification.Identifier{ID: userId, Group: ""}, batchId))
}(i)
}
}