This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtimer_queue_test.go
165 lines (138 loc) · 3.78 KB
/
timer_queue_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
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
// timer_queue_test.go - Time delayed queue tests
// Copyright (C) 2018 Masala, David Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"io"
mrand "math/rand"
"testing"
"time"
"github.com/katzenpost/core/crypto/rand"
"github.com/stretchr/testify/assert"
)
func TestNewTimerQueue(t *testing.T) {
// create a Queue for rescheduled messages
q := new(Queue)
a := NewTimerQueue(q)
a.Halt()
}
func TestTimerQueuePush(t *testing.T) {
assert := assert.New(t)
// create a queue for rescheduled messages
q := new(Queue)
a := NewTimerQueue(q)
// enqueue 10 messages
for i := 0; i < 10; i++ {
m := &Message{}
m.ID = new([16]byte)
m.SentAt = time.Now()
m.ReplyETA = 200 * time.Millisecond
_, err := io.ReadFull(rand.Reader, m.ID[:])
assert.NoError(err)
a.Push(m)
<-time.After(1 * time.Millisecond)
}
t.Logf("Sent 10 messages")
// wait for all of the timers to expire and each message to be enqueued in q
<-time.After(1 * time.Second)
j := 0
for {
_, err := q.Pop()
if err == ErrQueueEmpty {
break
}
j++
}
t.Logf("Pop() %d messages", j)
// Verify that all messages were placed into q
assert.Equal(10, j)
a.Halt()
}
func TestTimerQueueRemove(t *testing.T) {
assert := assert.New(t)
// create a Queue for forwarded messages
q := new(Queue)
a := NewTimerQueue(q)
// enqueue 10 messages, and call TimerQueue.Remove() on half of them before their timers expire
for i := 0; i < 10; i++ {
m := &Message{}
m.ID = new([16]byte)
m.SentAt = time.Now()
m.ReplyETA = 100 * time.Millisecond
m.QueuePriority = uint64(m.SentAt.Add(m.ReplyETA).UnixNano())
_, err := io.ReadFull(rand.Reader, m.ID[:])
assert.NoError(err)
a.Push(m)
<-time.After(20 * time.Millisecond)
if i%2 == 0 {
err := a.Remove(m)
assert.NoError(err)
}
<-time.After(80 * time.Millisecond)
}
t.Logf("Sent 10 messages")
<-time.After(2 * time.Second)
j := 0
var last uint64
for {
n, err := q.Pop()
if err == ErrQueueEmpty {
break
}
assert.True(n.(*Message).QueuePriority > last)
last = n.(*Message).QueuePriority
j++
}
t.Logf("Popped %d messages", j)
// verify that half of the messages were sent to q
assert.Equal(5, j)
a.Halt()
}
func TestTimerQueueOrder(t *testing.T) {
assert := assert.New(t)
// create a Queue for forwarded messages
q := new(Queue)
a := NewTimerQueue(q)
r := mrand.New(mrand.NewSource(0))
// enqueue 10 messages, and call TimerQueue.Remove() on half of them before their timers expire
for i := 0; i < 10; i++ {
m := &Message{}
m.ID = new([16]byte)
m.SentAt = time.Now()
m.ReplyETA = time.Duration(int(time.Millisecond) * r.Intn(100))
m.QueuePriority = uint64((m.SentAt.Add(m.ReplyETA)).UnixNano())
m.ID[0] = uint8(i)
t.Logf("Inserting: %x : %d", m.ID[0], m.QueuePriority)
a.Push(m)
<-time.After(10 * time.Millisecond)
}
t.Logf("\n")
<-time.After(1 * time.Second)
j := 0
var last uint64
last = 0xEFFFFFFF
for {
n, err := q.Pop()
if err == ErrQueueEmpty {
break
}
t.Logf("Popping: %x : %d", n.(*Message).ID[0], n.(*Message).QueuePriority)
assert.True(n.(*Message).QueuePriority > last)
last = n.(*Message).QueuePriority
j++
}
t.Logf("Popped %d messages", j)
a.Halt()
}