-
Notifications
You must be signed in to change notification settings - Fork 19
/
receivewithwait_test.go
145 lines (116 loc) · 4.22 KB
/
receivewithwait_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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 (
"testing"
"time"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
/*
* Test the behaviour of the receive-with-wait methods.
*/
func TestReceiveWait(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")
// Set up the consumer ready to receive messages.
consumer, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
if consumer != nil {
defer consumer.Close()
}
// Check no message on the queue to start with
testMsg, err1 := consumer.ReceiveNoWait()
assert.Nil(t, err1)
assert.Nil(t, testMsg)
waitTime := int32(500)
// Since there is no message on the queue, the receiveWait call should
// wait for the requested period of time, before returning nil.
startTime := currentTimeMillis()
testMsg2, err2 := consumer.Receive(waitTime)
endTime := currentTimeMillis()
assert.Nil(t, testMsg2)
assert.Nil(t, err2)
// Within a reasonable margin of the expected wait time.
assert.True(t, (endTime-startTime-int64(waitTime)) > -100)
// Send a message.
err := context.CreateProducer().SendString(queue, "MyMessage")
assert.Nil(t, err)
// With a message on the queue the receiveWait call should return immediately
// with the message.
startTime2 := currentTimeMillis()
testMsg3, err3 := consumer.Receive(waitTime)
endTime2 := currentTimeMillis()
assert.NotNil(t, testMsg3)
assert.Nil(t, err3)
// Should not wait the entire waitTime duration
assert.True(t, (endTime2-startTime2) < 300)
}
func TestReceiveStringBodyWait(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")
// Set up the consumer ready to receive messages.
consumer, conErr := context.CreateConsumer(queue)
assert.Nil(t, conErr)
if consumer != nil {
defer consumer.Close()
}
// Check no message on the queue to start with
msg, err1 := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, err1)
assert.Nil(t, msg)
waitTime := int32(500)
// Since there is no message on the queue, the receiveWait call should
// wait for the requested period of time, before returning nil.
startTime := currentTimeMillis()
msg2, err2 := consumer.ReceiveStringBody(waitTime)
endTime := currentTimeMillis()
assert.Nil(t, msg2)
assert.Nil(t, err2)
// Within a reasonable margin of the expected wait time.
assert.True(t, (endTime-startTime-int64(waitTime)) > -100)
// Send a message.
inputMsg := "MyMessageTest"
err := context.CreateProducer().SendString(queue, inputMsg)
assert.Nil(t, err)
// With a message on the queue the receiveWait call should return immediately
// with the message.
startTime2 := currentTimeMillis()
msg3, err3 := consumer.ReceiveStringBody(waitTime)
endTime2 := currentTimeMillis()
assert.Nil(t, err3)
assert.Equal(t, inputMsg, *msg3)
// Should not wait the entire waitTime duration
assert.True(t, (endTime2-startTime2) < 300)
}
// Return the current time in milliseconds
func currentTimeMillis() int64 {
return int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)
}