-
Notifications
You must be signed in to change notification settings - Fork 19
/
deliverymode_test.go
111 lines (92 loc) · 3.72 KB
/
deliverymode_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
/*
* Copyright (c) IBM Corporation 2019
*
* 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 (
"fmt"
"testing"
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
/*
* Test the ability to send both Persistent and Non-Persistent messages.
*/
func TestDeliveryMode(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)
if context != nil {
defer context.Close()
}
// Equivalent to a JNDI lookup or other declarative definition
queue := context.CreateQueue("DEV.QUEUE.1")
// First, check the queue is empty
consumer, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
if consumer != nil {
defer consumer.Close()
}
expectedNilMsg, rcvErr := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, rcvErr)
assert.Nil(t, expectedNilMsg)
if expectedNilMsg != nil {
fmt.Println("Unexpected message with body: " + *expectedNilMsg)
}
// Check the default behaviour if you don't set anything, which is that
// messages are send as Persistent.
tmpProducer := context.CreateProducer()
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, tmpProducer.GetDeliveryMode())
msgBody := "DeliveryMode test default"
errDef := tmpProducer.SendString(queue, msgBody)
assert.Nil(t, errDef)
defMsg, errDef2 := consumer.ReceiveNoWait()
assert.Nil(t, errDef2)
assert.NotNil(t, defMsg)
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, defMsg.GetJMSDeliveryMode())
// We have a "Message" object and can use a switch to safely test it is our expected TextMessage
switch msg := defMsg.(type) {
case jms20subset.TextMessage:
assert.Equal(t, msgBody, *msg.GetText())
default:
assert.Fail(t, "Got something other than a text message")
}
// Check the getter/setter behaviour on the Producer
tmpProducer = context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT)
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, tmpProducer.GetDeliveryMode())
tmpProducer.SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT)
assert.Equal(t, jms20subset.DeliveryMode_NON_PERSISTENT, tmpProducer.GetDeliveryMode())
// Create a Producer and set the delivery mode to Persistent
msgBody = "DeliveryMode test P"
err := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT).SendString(queue, msgBody)
assert.Nil(t, err)
pMsg, err2 := consumer.ReceiveNoWait()
assert.Nil(t, err2)
assert.NotNil(t, pMsg)
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, pMsg.GetJMSDeliveryMode())
// Create a Producer and set the delivery mode to NonPersistent
msgBody = "DeliveryMode test NP"
err3 := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT).SendString(queue, msgBody)
assert.Nil(t, err3)
npMsg, err4 := consumer.ReceiveNoWait()
assert.Nil(t, err4)
assert.NotNil(t, npMsg)
assert.Equal(t, jms20subset.DeliveryMode_NON_PERSISTENT, npMsg.GetJMSDeliveryMode())
// We have a "Message" object and can use a switch to safely test it is our expected TextMessage
switch msg := npMsg.(type) {
case jms20subset.TextMessage:
assert.Equal(t, msgBody, *msg.GetText())
default:
assert.Fail(t, "Got something other than a text message")
}
}