forked from rabbitmq/amqp091-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator_test.go
121 lines (97 loc) · 2.83 KB
/
allocator_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
// Copyright (c) 2021 VMware, Inc. or its affiliates. All Rights Reserved.
// Copyright (c) 2012-2021, Sean Treadway, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package amqp091
import (
"math/rand"
"testing"
)
func TestAllocatorFirstShouldBeTheLow(t *testing.T) {
n, ok := newAllocator(1, 2).next()
if !ok {
t.Fatalf("expected to allocate between 1 and 2")
}
if want, got := 1, n; want != got {
t.Fatalf("expected to first allocation to be 1")
}
}
func TestAllocatorShouldBeBoundByHigh(t *testing.T) {
a := newAllocator(1, 2)
if n, ok := a.next(); n != 1 || !ok {
t.Fatalf("expected to allocate between 1 and 2, got %d, %v", n, ok)
}
if n, ok := a.next(); n != 2 || !ok {
t.Fatalf("expected to allocate between 1 and 2, got %d, %v", n, ok)
}
if _, ok := a.next(); ok {
t.Fatalf("expected not to allocate outside of 1 and 2")
}
}
func TestAllocatorStringShouldIncludeAllocatedRanges(t *testing.T) {
a := newAllocator(1, 10)
a.reserve(1)
a.reserve(2)
a.reserve(3)
a.reserve(5)
a.reserve(6)
a.reserve(8)
a.reserve(10)
if want, got := "allocator[1..10] 1..3 5..6 8 10", a.String(); want != got {
t.Fatalf("expected String of %q, got %q", want, got)
}
}
func TestAllocatorShouldReuseReleased(t *testing.T) {
a := newAllocator(1, 2)
first, _ := a.next()
if want, got := 1, first; want != got {
t.Fatalf("expected allocation to be %d, got: %d", want, got)
}
second, _ := a.next()
if want, got := 2, second; want != got {
t.Fatalf("expected allocation to be %d, got: %d", want, got)
}
a.release(first)
third, _ := a.next()
if want, got := first, third; want != got {
t.Fatalf("expected third allocation to be %d, got: %d", want, got)
}
_, ok := a.next()
if want, got := false, ok; want != got {
t.Fatalf("expected fourth allocation to saturate the pool")
}
}
func TestAllocatorShouldNotReuseEarly(t *testing.T) {
a := newAllocator(1, 2)
first, _ := a.next()
if want, got := 1, first; want != got {
t.Fatalf("expected allocation to be %d, got: %d", want, got)
}
a.release(first)
second, _ := a.next()
if want, got := 2, second; want != got {
t.Fatalf("expected second allocation to be %d, got: %d", want, got)
}
third, _ := a.next()
if want, got := first, third; want != got {
t.Fatalf("expected third allocation to be %d, got: %d", want, got)
}
}
func TestAllocatorReleasesKeepUpWithAllocationsForAllSizes(t *testing.T) {
if testing.Short() {
t.Skip()
}
const runs = 5
const max = 13
for lim := 1; lim < 2<<max; lim <<= 1 {
a := newAllocator(0, lim)
for i := 0; i < runs*lim; i++ {
if i >= lim { // fills the allocator
a.release(int(rand.Int63n(int64(lim))))
}
if _, ok := a.next(); !ok {
t.Fatalf("expected %d runs of random release of size %d not to fail on allocation %d", runs, lim, i)
}
}
}
}