-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathdelivery_test.go
66 lines (60 loc) · 2.12 KB
/
delivery_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
// 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 (
"errors"
"strings"
"testing"
)
func shouldNotPanic(t *testing.T) {
if err := recover(); err != nil {
t.Fatalf("should not panic, got: %s", err)
}
}
// A closed delivery chan could produce zero value. Ack/Nack/Reject on these
// deliveries can produce a nil pointer panic. Instead, return an error when
// the method can never be successful.
func TestAckZeroValueAcknowledgerDoesNotPanic(t *testing.T) {
defer shouldNotPanic(t)
err := (Delivery{}).Ack(false)
if err == nil {
t.Fatalf("expected Delivery{}.Ack to error")
}
if !errors.Is(err, ErrDeliveryNotInitialized) {
t.Fatalf("expected '%v' got '%v'", ErrDeliveryNotInitialized, err)
}
expectedErrMessage := "delivery not initialized. Channel is probably closed"
if !strings.EqualFold(err.Error(), expectedErrMessage) {
t.Errorf("expected '%s' got '%s'", expectedErrMessage, err)
}
}
func TestNackZeroValueAcknowledgerDoesNotPanic(t *testing.T) {
defer shouldNotPanic(t)
err := (Delivery{}).Nack(false, false)
if err == nil {
t.Fatalf("expected Delivery{}.Nack to error")
}
if !errors.Is(err, ErrDeliveryNotInitialized) {
t.Fatalf("expected '%v' got '%v'", ErrDeliveryNotInitialized, err)
}
expectedErrMessage := "delivery not initialized. Channel is probably closed"
if !strings.EqualFold(err.Error(), expectedErrMessage) {
t.Errorf("expected '%s' got '%s'", expectedErrMessage, err)
}
}
func TestRejectZeroValueAcknowledgerDoesNotPanic(t *testing.T) {
defer shouldNotPanic(t)
err := (Delivery{}).Reject(false)
if err == nil {
t.Fatalf("expected Delivery{}.Reject to error")
}
if !errors.Is(err, ErrDeliveryNotInitialized) {
t.Fatalf("expected '%v' got '%v'", ErrDeliveryNotInitialized, err)
}
expectedErrMessage := "delivery not initialized. Channel is probably closed"
if !strings.EqualFold(err.Error(), expectedErrMessage) {
t.Errorf("expected '%s' got '%s'", expectedErrMessage, err)
}
}