|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2021 |
| 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 | + "strconv" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 17 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +/* |
| 22 | + * Test the ability to send a message asynchronously, which can give a higher |
| 23 | + * rate of sending non-persistent messages, in exchange for less/no checking for errors. |
| 24 | + */ |
| 25 | +func TestAsyncPut(t *testing.T) { |
| 26 | + |
| 27 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 28 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 29 | + assert.Nil(t, cfErr) |
| 30 | + |
| 31 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 32 | + // at the end of the function (if it was created successfully) |
| 33 | + context, ctxErr := cf.CreateContext() |
| 34 | + assert.Nil(t, ctxErr) |
| 35 | + if context != nil { |
| 36 | + defer context.Close() |
| 37 | + } |
| 38 | + |
| 39 | + // Set up the producer and consumer with the SYNCHRONOUS (not async yet) queue |
| 40 | + syncQueue := context.CreateQueue("DEV.QUEUE.1") |
| 41 | + producer := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT).SetTimeToLive(60000) |
| 42 | + |
| 43 | + consumer, errCons := context.CreateConsumer(syncQueue) |
| 44 | + assert.Nil(t, errCons) |
| 45 | + if consumer != nil { |
| 46 | + defer consumer.Close() |
| 47 | + } |
| 48 | + |
| 49 | + // Create a unique message prefix representing this execution of the test case. |
| 50 | + testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10) |
| 51 | + syncMsgPrefix := "syncput_" + testcasePrefix + "_" |
| 52 | + asyncMsgPrefix := "asyncput_" + testcasePrefix + "_" |
| 53 | + numberMessages := 50 |
| 54 | + |
| 55 | + // First get a baseline for how long it takes us to send the batch of messages |
| 56 | + // WITHOUT async put. |
| 57 | + syncStartTime := currentTimeMillis() |
| 58 | + for i := 0; i < numberMessages; i++ { |
| 59 | + |
| 60 | + // Create a TextMessage and send it. |
| 61 | + msg := context.CreateTextMessageWithString(syncMsgPrefix + strconv.Itoa(i)) |
| 62 | + |
| 63 | + errSend := producer.Send(syncQueue, msg) |
| 64 | + assert.Nil(t, errSend) |
| 65 | + } |
| 66 | + syncEndTime := currentTimeMillis() |
| 67 | + |
| 68 | + syncSendTime := syncEndTime - syncStartTime |
| 69 | + //fmt.Println("Took " + strconv.FormatInt(syncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " synchronous messages.") |
| 70 | + |
| 71 | + // Receive the messages back again |
| 72 | + finishedReceiving := false |
| 73 | + rcvCount := 0 |
| 74 | + |
| 75 | + for !finishedReceiving { |
| 76 | + rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait() |
| 77 | + assert.Nil(t, errRvc) |
| 78 | + |
| 79 | + if rcvTxt != nil { |
| 80 | + // Check the message bod matches what we expect |
| 81 | + assert.Equal(t, syncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt) |
| 82 | + rcvCount++ |
| 83 | + } else { |
| 84 | + finishedReceiving = true |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // -------------------------------------------------------- |
| 89 | + // Now repeat the experiment but with ASYNC message put |
| 90 | + asyncQueue := context.CreateQueue("DEV.QUEUE.1").SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED) |
| 91 | + |
| 92 | + asyncStartTime := currentTimeMillis() |
| 93 | + for i := 0; i < numberMessages; i++ { |
| 94 | + |
| 95 | + // Create a TextMessage and send it. |
| 96 | + msg := context.CreateTextMessageWithString(asyncMsgPrefix + strconv.Itoa(i)) |
| 97 | + |
| 98 | + errSend := producer.Send(asyncQueue, msg) |
| 99 | + assert.Nil(t, errSend) |
| 100 | + } |
| 101 | + asyncEndTime := currentTimeMillis() |
| 102 | + |
| 103 | + asyncSendTime := asyncEndTime - asyncStartTime |
| 104 | + //fmt.Println("Took " + strconv.FormatInt(asyncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " ASYNC messages.") |
| 105 | + |
| 106 | + // Receive the messages back again |
| 107 | + finishedReceiving = false |
| 108 | + rcvCount = 0 |
| 109 | + |
| 110 | + for !finishedReceiving { |
| 111 | + rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait() |
| 112 | + assert.Nil(t, errRvc) |
| 113 | + |
| 114 | + if rcvTxt != nil { |
| 115 | + // Check the message bod matches what we expect |
| 116 | + assert.Equal(t, asyncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt) |
| 117 | + rcvCount++ |
| 118 | + } else { |
| 119 | + finishedReceiving = true |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + assert.Equal(t, numberMessages, rcvCount) |
| 124 | + |
| 125 | + // Expect that async put is at least 10% faster than sync put for non-persistent messages |
| 126 | + // (in testing against a remote queue manager it was actually 30% faster) |
| 127 | + assert.True(t, 100*asyncSendTime < 90*syncSendTime) |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +/* |
| 132 | + * Test the getter/setter functions for controlling async put. |
| 133 | + */ |
| 134 | +func TestAsyncPutGetterSetter(t *testing.T) { |
| 135 | + |
| 136 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 137 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 138 | + assert.Nil(t, cfErr) |
| 139 | + |
| 140 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 141 | + // at the end of the function (if it was created successfully) |
| 142 | + context, ctxErr := cf.CreateContext() |
| 143 | + assert.Nil(t, ctxErr) |
| 144 | + if context != nil { |
| 145 | + defer context.Close() |
| 146 | + } |
| 147 | + |
| 148 | + // Set up the producer and consumer |
| 149 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 150 | + |
| 151 | + // Check the default |
| 152 | + assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST, queue.GetPutAsyncAllowed()) |
| 153 | + |
| 154 | + // Check enabled |
| 155 | + queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED) |
| 156 | + assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED, queue.GetPutAsyncAllowed()) |
| 157 | + |
| 158 | + // Check disabled |
| 159 | + queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_DISABLED) |
| 160 | + assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_DISABLED, queue.GetPutAsyncAllowed()) |
| 161 | + |
| 162 | + // Check as-dest |
| 163 | + queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST) |
| 164 | + assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST, queue.GetPutAsyncAllowed()) |
| 165 | + |
| 166 | +} |
0 commit comments