make it easier to test the timer in the zeroRTTQueue

This commit is contained in:
Marten Seemann 2020-08-28 18:03:45 +07:00
parent 7c204d4d9e
commit b21822ffc7
2 changed files with 13 additions and 5 deletions

View file

@ -13,12 +13,16 @@ type zeroRTTQueueEntry struct {
}
type zeroRTTQueue struct {
mutex sync.Mutex
queue map[string]*zeroRTTQueueEntry
mutex sync.Mutex
queue map[string]*zeroRTTQueueEntry
queueDuration time.Duration // so we can set it in tests
}
func newZeroRTTQueue() *zeroRTTQueue {
return &zeroRTTQueue{queue: make(map[string]*zeroRTTQueueEntry)}
return &zeroRTTQueue{
queue: make(map[string]*zeroRTTQueueEntry),
queueDuration: protocol.Max0RTTQueueingDuration,
}
}
func (h *zeroRTTQueue) Enqueue(connID protocol.ConnectionID, p *receivedPacket) {
@ -30,7 +34,9 @@ func (h *zeroRTTQueue) Enqueue(connID protocol.ConnectionID, p *receivedPacket)
if len(h.queue) >= protocol.Max0RTTQueues {
return
}
h.queue[cid] = &zeroRTTQueueEntry{timer: time.AfterFunc(protocol.Max0RTTQueueingDuration, func() { h.deleteQueue(connID) })}
h.queue[cid] = &zeroRTTQueueEntry{timer: time.AfterFunc(h.queueDuration, func() {
h.deleteQueue(connID)
})}
}
entry := h.queue[cid]
if len(entry.packets) >= protocol.Max0RTTQueueLen {

View file

@ -12,9 +12,11 @@ import (
var _ = Describe("0-RTT queue", func() {
var q *zeroRTTQueue
queueDuration := scaleDuration(20 * time.Millisecond)
BeforeEach(func() {
q = newZeroRTTQueue()
q.queueDuration = queueDuration
})
AfterEach(func() {
@ -107,7 +109,7 @@ var _ = Describe("0-RTT queue", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}
p := &receivedPacket{data: []byte("foobar"), buffer: getPacketBuffer()}
q.Enqueue(connID, p)
time.Sleep(protocol.Max0RTTQueueingDuration * 3 / 2)
time.Sleep(queueDuration * 3 / 2)
Expect(q.Dequeue(connID)).To(BeNil())
})
})