forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_store_test.go
108 lines (95 loc) · 3.17 KB
/
token_store_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
105
106
107
108
package quic
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Token Cache", func() {
var s TokenStore
BeforeEach(func() {
s = NewLRUTokenStore(3, 4)
})
mockToken := func(num int) *ClientToken {
return &ClientToken{data: []byte(fmt.Sprintf("%d", num))}
}
Context("for a single origin", func() {
const origin = "localhost"
It("adds and gets tokens", func() {
s.Put(origin, mockToken(1))
s.Put(origin, mockToken(2))
Expect(s.Pop(origin)).To(Equal(mockToken(2)))
Expect(s.Pop(origin)).To(Equal(mockToken(1)))
Expect(s.Pop(origin)).To(BeNil())
})
It("overwrites old tokens", func() {
s.Put(origin, mockToken(1))
s.Put(origin, mockToken(2))
s.Put(origin, mockToken(3))
s.Put(origin, mockToken(4))
s.Put(origin, mockToken(5))
Expect(s.Pop(origin)).To(Equal(mockToken(5)))
Expect(s.Pop(origin)).To(Equal(mockToken(4)))
Expect(s.Pop(origin)).To(Equal(mockToken(3)))
Expect(s.Pop(origin)).To(Equal(mockToken(2)))
Expect(s.Pop(origin)).To(BeNil())
})
It("continues after getting a token", func() {
s.Put(origin, mockToken(1))
s.Put(origin, mockToken(2))
s.Put(origin, mockToken(3))
Expect(s.Pop(origin)).To(Equal(mockToken(3)))
s.Put(origin, mockToken(4))
s.Put(origin, mockToken(5))
Expect(s.Pop(origin)).To(Equal(mockToken(5)))
Expect(s.Pop(origin)).To(Equal(mockToken(4)))
Expect(s.Pop(origin)).To(Equal(mockToken(2)))
Expect(s.Pop(origin)).To(Equal(mockToken(1)))
Expect(s.Pop(origin)).To(BeNil())
})
})
Context("for multiple origins", func() {
It("adds and gets tokens", func() {
s.Put("host1", mockToken(1))
s.Put("host2", mockToken(2))
Expect(s.Pop("host1")).To(Equal(mockToken(1)))
Expect(s.Pop("host1")).To(BeNil())
Expect(s.Pop("host2")).To(Equal(mockToken(2)))
Expect(s.Pop("host2")).To(BeNil())
})
It("evicts old entries", func() {
s.Put("host1", mockToken(1))
s.Put("host2", mockToken(2))
s.Put("host3", mockToken(3))
s.Put("host4", mockToken(4))
Expect(s.Pop("host1")).To(BeNil())
Expect(s.Pop("host2")).To(Equal(mockToken(2)))
Expect(s.Pop("host3")).To(Equal(mockToken(3)))
Expect(s.Pop("host4")).To(Equal(mockToken(4)))
})
It("moves old entries to the front, when new tokens are added", func() {
s.Put("host1", mockToken(1))
s.Put("host2", mockToken(2))
s.Put("host3", mockToken(3))
s.Put("host1", mockToken(11))
// make sure one is evicted
s.Put("host4", mockToken(4))
Expect(s.Pop("host2")).To(BeNil())
Expect(s.Pop("host1")).To(Equal(mockToken(11)))
Expect(s.Pop("host1")).To(Equal(mockToken(1)))
Expect(s.Pop("host3")).To(Equal(mockToken(3)))
Expect(s.Pop("host4")).To(Equal(mockToken(4)))
})
It("deletes hosts that are empty", func() {
s.Put("host1", mockToken(1))
s.Put("host2", mockToken(2))
s.Put("host3", mockToken(3))
Expect(s.Pop("host2")).To(Equal(mockToken(2)))
Expect(s.Pop("host2")).To(BeNil())
// host2 is now empty and should have been deleted, making space for host4
s.Put("host4", mockToken(4))
Expect(s.Pop("host1")).To(Equal(mockToken(1)))
Expect(s.Pop("host3")).To(Equal(mockToken(3)))
Expect(s.Pop("host4")).To(Equal(mockToken(4)))
})
})
})