-
Notifications
You must be signed in to change notification settings - Fork 19
/
cascade_close_test.go
121 lines (99 loc) · 3.41 KB
/
cascade_close_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) IBM Corporation 2020
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package main
import (
"testing"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
/*
* Test the behaviour of the cascading close.
*
* - Create three consumers
* - Receive with timeout, get no message
* - Close second consumer, try to receive again, get error, check 1 + 3 still ok
* - Ctx.close, try to receive on the other two, get error
*/
func TestCascadeClose(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
// We are testing Close behaviour here, but auto-cleanup just in case.
if context != nil {
defer context.Close()
}
// Equivalent to a JNDI lookup or other declarative definition
queue := context.CreateQueue("DEV.QUEUE.1")
// Set up the consumers 1-3 ready to receive messages.
consumer1, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
// We are testing Close behaviour here, but auto-cleanup just in case.
if consumer1 != nil {
defer consumer1.Close()
}
consumer2, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
// We are testing Close behaviour here, but auto-cleanup just in case.
if consumer2 != nil {
defer consumer2.Close()
}
consumer3, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
// We are testing Close behaviour here, but auto-cleanup just in case.
if consumer3 != nil {
defer consumer3.Close()
}
// Check no message on the queue to start with
testMsg, err := consumer1.ReceiveNoWait()
assert.Nil(t, err)
assert.Nil(t, testMsg)
testMsg, err = consumer2.ReceiveNoWait()
assert.Nil(t, err)
assert.Nil(t, testMsg)
testMsg, err = consumer3.ReceiveNoWait()
assert.Nil(t, err)
assert.Nil(t, testMsg)
// Close second consumer, try to receive again, get error, check 1 + 3 still ok
consumer2.Close()
testMsg, err = consumer1.ReceiveNoWait()
assert.Nil(t, err)
assert.Nil(t, testMsg)
testMsg, err = consumer2.ReceiveNoWait()
assert.NotNil(t, err)
assert.Equal(t, "2019", err.GetErrorCode())
assert.Equal(t, "MQRC_HOBJ_ERROR", err.GetReason())
assert.Nil(t, testMsg)
testMsg, err = consumer3.ReceiveNoWait()
assert.Nil(t, err)
assert.Nil(t, testMsg)
// Close a closed thing to check it doesn't complain.
consumer2.Close()
// Ctx.close, try to receive on the other two, get error
context.Close()
testMsg, err = consumer1.ReceiveNoWait()
assert.NotNil(t, err)
assert.Equal(t, "2018", err.GetErrorCode())
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
assert.Nil(t, testMsg)
testMsg, err = consumer2.ReceiveNoWait()
assert.NotNil(t, err)
assert.Equal(t, "2018", err.GetErrorCode())
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
assert.Nil(t, testMsg)
testMsg, err = consumer3.ReceiveNoWait()
assert.NotNil(t, err)
assert.Equal(t, "2018", err.GetErrorCode())
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
assert.Nil(t, testMsg)
}