|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2019 |
| 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 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +/* |
| 20 | + * Test the behaviour of the receive-with-wait methods. |
| 21 | + */ |
| 22 | +func TestReceiveWait(t *testing.T) { |
| 23 | + |
| 24 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 25 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 26 | + assert.Nil(t, cfErr) |
| 27 | + |
| 28 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 29 | + // at the end of the function (if it was created successfully) |
| 30 | + context, ctxErr := cf.CreateContext() |
| 31 | + assert.Nil(t, ctxErr) |
| 32 | + if context != nil { |
| 33 | + defer context.Close() |
| 34 | + } |
| 35 | + |
| 36 | + // Equivalent to a JNDI lookup or other declarative definition |
| 37 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 38 | + |
| 39 | + // Set up the consumer ready to receive messages. |
| 40 | + consumer, conErr := context.CreateConsumer(queue) |
| 41 | + assert.Nil(t, conErr) |
| 42 | + if consumer != nil { |
| 43 | + defer consumer.Close() |
| 44 | + } |
| 45 | + |
| 46 | + // Check no message on the queue to start with |
| 47 | + testMsg, err1 := consumer.ReceiveNoWait() |
| 48 | + assert.Nil(t, err1) |
| 49 | + assert.Nil(t, testMsg) |
| 50 | + |
| 51 | + waitTime := int32(500) |
| 52 | + |
| 53 | + // Since there is no message on the queue, the receiveWait call should |
| 54 | + // wait for the requested period of time, before returning nil. |
| 55 | + startTime := currentTimeMillis() |
| 56 | + testMsg2, err2 := consumer.Receive(waitTime) |
| 57 | + endTime := currentTimeMillis() |
| 58 | + assert.Nil(t, testMsg2) |
| 59 | + assert.Nil(t, err2) |
| 60 | + |
| 61 | + // Within a reasonable margin of the expected wait time. |
| 62 | + assert.True(t, (endTime-startTime-int64(waitTime)) > -100) |
| 63 | + |
| 64 | + // Send a message. |
| 65 | + err := context.CreateProducer().SendString(queue, "MyMessage") |
| 66 | + assert.Nil(t, err) |
| 67 | + |
| 68 | + // With a message on the queue the receiveWait call should return immediately |
| 69 | + // with the message. |
| 70 | + startTime2 := currentTimeMillis() |
| 71 | + testMsg3, err3 := consumer.Receive(waitTime) |
| 72 | + endTime2 := currentTimeMillis() |
| 73 | + assert.NotNil(t, testMsg3) |
| 74 | + assert.Nil(t, err3) |
| 75 | + |
| 76 | + // Should not wait the entire waitTime duration |
| 77 | + assert.True(t, (endTime2-startTime2) < 300) |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +func TestReceiveStringBodyWait(t *testing.T) { |
| 82 | + |
| 83 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 84 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 85 | + assert.Nil(t, cfErr) |
| 86 | + |
| 87 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 88 | + // at the end of the function (if it was created successfully) |
| 89 | + context, ctxErr := cf.CreateContext() |
| 90 | + assert.Nil(t, ctxErr) |
| 91 | + if context != nil { |
| 92 | + defer context.Close() |
| 93 | + } |
| 94 | + |
| 95 | + // Equivalent to a JNDI lookup or other declarative definition |
| 96 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 97 | + |
| 98 | + // Set up the consumer ready to receive messages. |
| 99 | + consumer, conErr := context.CreateConsumer(queue) |
| 100 | + assert.Nil(t, conErr) |
| 101 | + if consumer != nil { |
| 102 | + defer consumer.Close() |
| 103 | + } |
| 104 | + |
| 105 | + // Check no message on the queue to start with |
| 106 | + msg, err1 := consumer.ReceiveStringBodyNoWait() |
| 107 | + assert.Nil(t, err1) |
| 108 | + assert.Nil(t, msg) |
| 109 | + |
| 110 | + waitTime := int32(500) |
| 111 | + |
| 112 | + // Since there is no message on the queue, the receiveWait call should |
| 113 | + // wait for the requested period of time, before returning nil. |
| 114 | + startTime := currentTimeMillis() |
| 115 | + msg2, err2 := consumer.ReceiveStringBody(waitTime) |
| 116 | + endTime := currentTimeMillis() |
| 117 | + assert.Nil(t, msg2) |
| 118 | + assert.Nil(t, err2) |
| 119 | + |
| 120 | + // Within a reasonable margin of the expected wait time. |
| 121 | + assert.True(t, (endTime-startTime-int64(waitTime)) > -100) |
| 122 | + |
| 123 | + // Send a message. |
| 124 | + inputMsg := "MyMessageTest" |
| 125 | + err := context.CreateProducer().SendString(queue, inputMsg) |
| 126 | + assert.Nil(t, err) |
| 127 | + |
| 128 | + // With a message on the queue the receiveWait call should return immediately |
| 129 | + // with the message. |
| 130 | + startTime2 := currentTimeMillis() |
| 131 | + msg3, err3 := consumer.ReceiveStringBody(waitTime) |
| 132 | + endTime2 := currentTimeMillis() |
| 133 | + assert.Nil(t, err3) |
| 134 | + assert.Equal(t, inputMsg, *msg3) |
| 135 | + |
| 136 | + // Should not wait the entire waitTime duration |
| 137 | + assert.True(t, (endTime2-startTime2) < 300) |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +// Return the current time in milliseconds |
| 142 | +func currentTimeMillis() int64 { |
| 143 | + return int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond) |
| 144 | +} |
0 commit comments