| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package amqp091 |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
func TestConfirmOneResequences(t *testing.T) { |
| 16 |
var ( |
| 17 |
fixtures = []Confirmation{ |
| 18 |
{1, true}, |
| 19 |
{2, false}, |
| 20 |
{3, true}, |
| 21 |
} |
| 22 |
c = newConfirms() |
| 23 |
l = make(chan Confirmation, len(fixtures)) |
| 24 |
) |
| 25 |
|
| 26 |
c.Listen(l) |
| 27 |
|
| 28 |
for i := range fixtures { |
| 29 |
if want, got := uint64(i+1), c.publish(); want != got.DeliveryTag { |
| 30 |
t.Fatalf("expected publish to return the 1 based delivery tag published, want: %d, got: %d", want, got.DeliveryTag) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
c.One(fixtures[1]) |
| 35 |
c.One(fixtures[2]) |
| 36 |
|
| 37 |
select { |
| 38 |
case confirm := <-l: |
| 39 |
t.Fatalf("expected to wait in order to properly resequence results, got: %+v", confirm) |
| 40 |
default: |
| 41 |
} |
| 42 |
|
| 43 |
c.One(fixtures[0]) |
| 44 |
|
| 45 |
for i, fix := range fixtures { |
| 46 |
if want, got := fix, <-l; want != got { |
| 47 |
t.Fatalf("expected to return confirmations in sequence for %d, want: %+v, got: %+v", i, want, got) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestConfirmAndPublishDoNotDeadlock(t *testing.T) { |
| 53 |
var ( |
| 54 |
c = newConfirms() |
| 55 |
l = make(chan Confirmation) |
| 56 |
iterations = 10 |
| 57 |
) |
| 58 |
c.Listen(l) |
| 59 |
|
| 60 |
go func() { |
| 61 |
for i := 0; i < iterations; i++ { |
| 62 |
c.One(Confirmation{uint64(i + 1), true}) |
| 63 |
} |
| 64 |
}() |
| 65 |
|
| 66 |
for i := 0; i < iterations; i++ { |
| 67 |
c.publish() |
| 68 |
<-l |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestConfirmMixedResequences(t *testing.T) { |
| 73 |
var ( |
| 74 |
fixtures = []Confirmation{ |
| 75 |
{1, true}, |
| 76 |
{2, true}, |
| 77 |
{3, true}, |
| 78 |
} |
| 79 |
c = newConfirms() |
| 80 |
l = make(chan Confirmation, len(fixtures)) |
| 81 |
) |
| 82 |
c.Listen(l) |
| 83 |
|
| 84 |
for range fixtures { |
| 85 |
c.publish() |
| 86 |
} |
| 87 |
|
| 88 |
c.One(fixtures[0]) |
| 89 |
c.One(fixtures[2]) |
| 90 |
c.Multiple(fixtures[1]) |
| 91 |
|
| 92 |
for i, fix := range fixtures { |
| 93 |
want := fix |
| 94 |
var got Confirmation |
| 95 |
select { |
| 96 |
case got = <-l: |
| 97 |
case <-time.After(1 * time.Second): |
| 98 |
t.Fatalf("timeout on reading confirmations") |
| 99 |
} |
| 100 |
if want != got { |
| 101 |
t.Fatalf("expected to confirm in sequence for %d, want: %+v, got: %+v", i, want, got) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestConfirmMultipleResequences(t *testing.T) { |
| 107 |
var ( |
| 108 |
fixtures = []Confirmation{ |
| 109 |
{1, true}, |
| 110 |
{2, true}, |
| 111 |
{3, true}, |
| 112 |
{4, true}, |
| 113 |
} |
| 114 |
c = newConfirms() |
| 115 |
l = make(chan Confirmation, len(fixtures)) |
| 116 |
) |
| 117 |
c.Listen(l) |
| 118 |
|
| 119 |
for range fixtures { |
| 120 |
c.publish() |
| 121 |
} |
| 122 |
|
| 123 |
c.Multiple(fixtures[len(fixtures)-1]) |
| 124 |
|
| 125 |
for i, fix := range fixtures { |
| 126 |
if want, got := fix, <-l; want != got { |
| 127 |
t.Fatalf("expected to confirm multiple in sequence for %d, want: %+v, got: %+v", i, want, got) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func BenchmarkSequentialBufferedConfirms(t *testing.B) { |
| 133 |
var ( |
| 134 |
c = newConfirms() |
| 135 |
l = make(chan Confirmation, 10) |
| 136 |
) |
| 137 |
|
| 138 |
c.Listen(l) |
| 139 |
|
| 140 |
for i := 0; i < t.N; i++ { |
| 141 |
if i > cap(l)-1 { |
| 142 |
<-l |
| 143 |
} |
| 144 |
c.One(Confirmation{c.publish().DeliveryTag, true}) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestConfirmsIsThreadSafe(t *testing.T) { |
| 149 |
const count = 1000 |
| 150 |
const timeout = 5 * time.Second |
| 151 |
var ( |
| 152 |
c = newConfirms() |
| 153 |
l = make(chan Confirmation) |
| 154 |
pub = make(chan Confirmation) |
| 155 |
done = make(chan Confirmation) |
| 156 |
late = time.After(timeout) |
| 157 |
) |
| 158 |
|
| 159 |
c.Listen(l) |
| 160 |
|
| 161 |
for i := 0; i < count; i++ { |
| 162 |
go func() { pub <- Confirmation{c.publish().DeliveryTag, true} }() |
| 163 |
} |
| 164 |
|
| 165 |
for i := 0; i < count; i++ { |
| 166 |
go func() { c.One(<-pub) }() |
| 167 |
} |
| 168 |
|
| 169 |
for i := 0; i < count; i++ { |
| 170 |
go func() { done <- <-l }() |
| 171 |
} |
| 172 |
|
| 173 |
for i := 0; i < count; i++ { |
| 174 |
select { |
| 175 |
case <-done: |
| 176 |
case <-late: |
| 177 |
t.Fatalf("expected all publish/confirms to finish after %s", timeout) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestDeferredConfirmationsConfirm(t *testing.T) { |
| 183 |
dcs := newDeferredConfirmations() |
| 184 |
var wg sync.WaitGroup |
| 185 |
for i, ack := range []bool{true, false} { |
| 186 |
var result bool |
| 187 |
deliveryTag := uint64(i + 1) |
| 188 |
dc := dcs.Add(deliveryTag) |
| 189 |
wg.Add(1) |
| 190 |
go func() { |
| 191 |
result = dc.Wait() |
| 192 |
wg.Done() |
| 193 |
}() |
| 194 |
dcs.Confirm(Confirmation{deliveryTag, ack}) |
| 195 |
wg.Wait() |
| 196 |
if result != ack { |
| 197 |
t.Fatalf("expected to receive matching ack got %v", result) |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestDeferredConfirmationsConfirmMultiple(t *testing.T) { |
| 203 |
dcs := newDeferredConfirmations() |
| 204 |
var wg sync.WaitGroup |
| 205 |
var result bool |
| 206 |
dc1 := dcs.Add(1) |
| 207 |
dc2 := dcs.Add(2) |
| 208 |
dc3 := dcs.Add(3) |
| 209 |
wg.Add(1) |
| 210 |
go func() { |
| 211 |
result = dc1.Wait() && dc2.Wait() && dc3.Wait() |
| 212 |
wg.Done() |
| 213 |
}() |
| 214 |
dcs.ConfirmMultiple(Confirmation{4, true}) |
| 215 |
wg.Wait() |
| 216 |
if !result { |
| 217 |
t.Fatal("expected to receive true for result, received false") |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func TestDeferredConfirmationsClose(t *testing.T) { |
| 222 |
dcs := newDeferredConfirmations() |
| 223 |
var wg sync.WaitGroup |
| 224 |
var result bool |
| 225 |
dc1 := dcs.Add(1) |
| 226 |
dc2 := dcs.Add(2) |
| 227 |
dc3 := dcs.Add(3) |
| 228 |
wg.Add(1) |
| 229 |
go func() { |
| 230 |
result = !dc1.Wait() && !dc2.Wait() && !dc3.Wait() |
| 231 |
wg.Done() |
| 232 |
}() |
| 233 |
dcs.Close() |
| 234 |
wg.Wait() |
| 235 |
if !result { |
| 236 |
t.Fatal("expected to receive false for nacked confirmations, received true") |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestDeferredConfirmationsDoneAcked(t *testing.T) { |
| 241 |
dcs := newDeferredConfirmations() |
| 242 |
dc := dcs.Add(1) |
| 243 |
|
| 244 |
if dc.Acked() { |
| 245 |
t.Fatal("expected to receive false for pending confirmations, received true") |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
for i := 0; i < 2; i++ { |
| 250 |
dcs.Confirm(Confirmation{dc.DeliveryTag, true}) |
| 251 |
} |
| 252 |
|
| 253 |
<-dc.Done() |
| 254 |
|
| 255 |
if !dc.Acked() { |
| 256 |
t.Fatal("expected to receive true for acked confirmations, received false") |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func TestDeferredConfirmationsWaitContextNack(t *testing.T) { |
| 261 |
dcs := newDeferredConfirmations() |
| 262 |
dc := dcs.Add(1) |
| 263 |
|
| 264 |
dcs.Confirm(Confirmation{dc.DeliveryTag, false}) |
| 265 |
|
| 266 |
ack, err := dc.WaitContext(context.Background()) |
| 267 |
if err != nil { |
| 268 |
t.Fatalf("expected to receive nil, got %v", err) |
| 269 |
} |
| 270 |
if ack { |
| 271 |
t.Fatal("expected to receive false for nacked confirmations, received true") |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
func TestDeferredConfirmationsWaitContextCancel(t *testing.T) { |
| 276 |
dc := newDeferredConfirmations().Add(1) |
| 277 |
|
| 278 |
ctx, cancel := context.WithCancel(context.Background()) |
| 279 |
cancel() |
| 280 |
|
| 281 |
ack, err := dc.WaitContext(ctx) |
| 282 |
if err == nil { |
| 283 |
t.Fatal("expected to receive context error, got nil") |
| 284 |
} |
| 285 |
if ack { |
| 286 |
t.Fatal("expected to receive false for pending confirmations, received true") |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func TestDeferredConfirmationsConcurrency(t *testing.T) { |
| 291 |
dcs := newDeferredConfirmations() |
| 292 |
var wg sync.WaitGroup |
| 293 |
var result bool |
| 294 |
dc1 := dcs.Add(1) |
| 295 |
dc2 := dcs.Add(2) |
| 296 |
dc3 := dcs.Add(3) |
| 297 |
wg.Add(1) |
| 298 |
|
| 299 |
go func() { |
| 300 |
defer wg.Done() |
| 301 |
result = dc1.Wait() && dc2.Wait() && dc3.Wait() |
| 302 |
}() |
| 303 |
dcs.ConfirmMultiple(Confirmation{4, true}) |
| 304 |
wg.Wait() |
| 305 |
if !result { |
| 306 |
t.Fatal("expected to receive true for concurrent confirmations, received false") |
| 307 |
} |
| 308 |
} |