Skip to content

Commit b8e29de

Browse files
committed
Check for cascaded close
1 parent fe06a09 commit b8e29de

File tree

3 files changed

+135
-10
lines changed

3 files changed

+135
-10
lines changed

cascade_close_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) IBM Corporation 2020
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package main
11+
12+
import (
13+
"testing"
14+
15+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
/*
20+
* Test the behaviour of the cascading close.
21+
*
22+
* - Create three consumers
23+
* - Receive with timeout, get no message
24+
* - Close second consumer, try to receive again, get error, check 1 + 3 still ok
25+
* - Ctx.close, try to receive on the other two, get error
26+
*/
27+
func TestCascadeClose(t *testing.T) {
28+
29+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
30+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
31+
assert.Nil(t, cfErr)
32+
33+
// Creates a connection to the queue manager, using defer to close it automatically
34+
// at the end of the function (if it was created successfully)
35+
context, ctxErr := cf.CreateContext()
36+
assert.Nil(t, ctxErr)
37+
// We are testing Close behaviour here, but auto-cleanup just in case.
38+
if context != nil {
39+
defer context.Close()
40+
}
41+
42+
// Equivalent to a JNDI lookup or other declarative definition
43+
queue := context.CreateQueue("DEV.QUEUE.1")
44+
45+
// Set up the consumers 1-3 ready to receive messages.
46+
consumer1, conErr := context.CreateConsumer(queue)
47+
assert.Nil(t, conErr)
48+
// We are testing Close behaviour here, but auto-cleanup just in case.
49+
if consumer1 != nil {
50+
defer consumer1.Close()
51+
}
52+
53+
consumer2, conErr := context.CreateConsumer(queue)
54+
assert.Nil(t, conErr)
55+
// We are testing Close behaviour here, but auto-cleanup just in case.
56+
if consumer2 != nil {
57+
defer consumer2.Close()
58+
}
59+
60+
consumer3, conErr := context.CreateConsumer(queue)
61+
assert.Nil(t, conErr)
62+
// We are testing Close behaviour here, but auto-cleanup just in case.
63+
if consumer3 != nil {
64+
defer consumer3.Close()
65+
}
66+
67+
// Check no message on the queue to start with
68+
testMsg, err := consumer1.ReceiveNoWait()
69+
assert.Nil(t, err)
70+
assert.Nil(t, testMsg)
71+
72+
testMsg, err = consumer2.ReceiveNoWait()
73+
assert.Nil(t, err)
74+
assert.Nil(t, testMsg)
75+
76+
testMsg, err = consumer3.ReceiveNoWait()
77+
assert.Nil(t, err)
78+
assert.Nil(t, testMsg)
79+
80+
// Close second consumer, try to receive again, get error, check 1 + 3 still ok
81+
consumer2.Close()
82+
83+
testMsg, err = consumer1.ReceiveNoWait()
84+
assert.Nil(t, err)
85+
assert.Nil(t, testMsg)
86+
87+
testMsg, err = consumer2.ReceiveNoWait()
88+
assert.NotNil(t, err)
89+
assert.Equal(t, "2019", err.GetErrorCode())
90+
assert.Equal(t, "MQRC_HOBJ_ERROR", err.GetReason())
91+
assert.Nil(t, testMsg)
92+
93+
testMsg, err = consumer3.ReceiveNoWait()
94+
assert.Nil(t, err)
95+
assert.Nil(t, testMsg)
96+
97+
// Close a closed thing to check it doesn't complain.
98+
consumer2.Close()
99+
100+
// Ctx.close, try to receive on the other two, get error
101+
context.Close()
102+
103+
testMsg, err = consumer1.ReceiveNoWait()
104+
assert.NotNil(t, err)
105+
assert.Equal(t, "2018", err.GetErrorCode())
106+
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
107+
assert.Nil(t, testMsg)
108+
109+
testMsg, err = consumer2.ReceiveNoWait()
110+
assert.NotNil(t, err)
111+
assert.Equal(t, "2018", err.GetErrorCode())
112+
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
113+
assert.Nil(t, testMsg)
114+
115+
testMsg, err = consumer3.ReceiveNoWait()
116+
assert.NotNil(t, err)
117+
assert.Equal(t, "2018", err.GetErrorCode())
118+
assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason())
119+
assert.Nil(t, testMsg)
120+
121+
}

next-features.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ project!
55

66
Not currently implemented:
77
--------------------------
8-
- Cascade close from JMSContext to producer/consumer objects
98
- BytesMessage, receiveBytesBody
109
- Local transactions (e.g. allow request/reply under transaction)
1110
- MessageListener
@@ -15,6 +14,10 @@ Not currently implemented:
1514
- Temporary destinations
1615
- Priority
1716

17+
Client capabilities for participating in Uniform Clusters;
18+
- CCDT to allow listing queue managers
19+
- Set APPLTag to name the logical application
20+
- Auto reconnect (any) + MQCNO_RECONNECT_QMGR
1821

1922
Known issues:
2023
-------------

requestreply_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
package main
1111

1212
import (
13+
"testing"
14+
1315
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
1416
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
1517
"github.com/stretchr/testify/assert"
16-
"testing"
1718
)
1819

1920
/*
@@ -70,16 +71,16 @@ func TestRequestReply(t *testing.T) {
7071

7172
// Get the generated MessageID from the sent message, so that we
7273
// can receive the response message using it later
73-
msgId := sentMsg.GetJMSMessageID()
74-
assert.NotEqual(t, "", msgId)
75-
assert.Equal(t, 48, len(msgId))
74+
msgID := sentMsg.GetJMSMessageID()
75+
assert.NotEqual(t, "", msgID)
76+
assert.Equal(t, 48, len(msgID))
7677

7778
// "Another application" will consume the request message and sent
7879
// the reply message
7980
replyToMessage(t, cf, requestQueue)
8081

8182
// Receive the reply message, selecting by CorrelID
82-
replyConsumer, rConErr3 := context.CreateConsumerWithSelector(replyQueue, "JMSCorrelationID = '"+msgId+"'")
83+
replyConsumer, rConErr3 := context.CreateConsumerWithSelector(replyQueue, "JMSCorrelationID = '"+msgID+"'")
8384
assert.Nil(t, rConErr3)
8485
if replyConsumer != nil {
8586
defer replyConsumer.Close()
@@ -92,7 +93,7 @@ func TestRequestReply(t *testing.T) {
9293
switch msg := respMsg.(type) {
9394
case jms20subset.TextMessage:
9495
assert.Equal(t, "ReplyMsg", *msg.GetText())
95-
assert.Equal(t, msgId, msg.GetJMSCorrelationID())
96+
assert.Equal(t, msgID, msg.GetJMSCorrelationID())
9697
default:
9798
assert.Fail(t, "Got something other than a text message")
9899
}
@@ -120,8 +121,8 @@ func replyToMessage(t *testing.T, cf jms20subset.ConnectionFactory, requestQueue
120121
}
121122
reqMsg, err := requestConsumer.ReceiveNoWait()
122123
assert.Nil(t, err)
123-
reqMsgId := reqMsg.GetJMSMessageID()
124-
assert.NotEqual(t, "", reqMsgId)
124+
reqMsgID := reqMsg.GetJMSMessageID()
125+
assert.NotEqual(t, "", reqMsgID)
125126

126127
// Get the replyTo queue and request MsgID
127128
replyDest := reqMsg.GetJMSReplyTo()
@@ -131,7 +132,7 @@ func replyToMessage(t *testing.T, cf jms20subset.ConnectionFactory, requestQueue
131132
replyMsgBody := "ReplyMsg"
132133
replyMsg := rrContext.CreateTextMessageWithString(replyMsgBody)
133134
assert.Equal(t, "", replyMsg.GetJMSCorrelationID())
134-
replyMsg.SetJMSCorrelationID(reqMsgId)
135+
replyMsg.SetJMSCorrelationID(reqMsgID)
135136
assert.NotEqual(t, "", replyMsg.GetJMSCorrelationID())
136137

137138
// Send the reply message back to the original application

0 commit comments

Comments
 (0)