|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2022 |
| 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/jms20subset" |
| 16 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +/* |
| 21 | + * Test the retrieval of special header properties |
| 22 | + */ |
| 23 | +func TestPrioritySetGet(t *testing.T) { |
| 24 | + |
| 25 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 26 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 27 | + assert.Nil(t, cfErr) |
| 28 | + |
| 29 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 30 | + // at the end of the function (if it was created successfully) |
| 31 | + context, ctxErr := cf.CreateContext() |
| 32 | + assert.Nil(t, ctxErr) |
| 33 | + if context != nil { |
| 34 | + defer context.Close() |
| 35 | + } |
| 36 | + |
| 37 | + // Create a TextMessage and check that we can populate it |
| 38 | + msgBody := "PriorityMsg" |
| 39 | + txtMsg := context.CreateTextMessage() |
| 40 | + txtMsg.SetText(msgBody) |
| 41 | + |
| 42 | + // Set up objects for send/receive |
| 43 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 44 | + consumer, errCons := context.CreateConsumer(queue) |
| 45 | + if consumer != nil { |
| 46 | + defer consumer.Close() |
| 47 | + } |
| 48 | + assert.Nil(t, errCons) |
| 49 | + |
| 50 | + // Now send the message and get it back again, to check that it roundtripped. |
| 51 | + ttlMillis := 20000 |
| 52 | + producer := context.CreateProducer().SetTimeToLive(ttlMillis) |
| 53 | + |
| 54 | + // Check the default priority. |
| 55 | + assert.Equal(t, jms20subset.Priority_DEFAULT, producer.GetPriority()) |
| 56 | + |
| 57 | + errSend := producer.Send(queue, txtMsg) |
| 58 | + assert.Nil(t, errSend) |
| 59 | + |
| 60 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 61 | + assert.Nil(t, errRvc) |
| 62 | + assert.NotNil(t, rcvMsg) |
| 63 | + |
| 64 | + switch msg := rcvMsg.(type) { |
| 65 | + case jms20subset.TextMessage: |
| 66 | + assert.Equal(t, msgBody, *msg.GetText()) |
| 67 | + default: |
| 68 | + assert.Fail(t, "Got something other than a text message") |
| 69 | + } |
| 70 | + |
| 71 | + // Check the Priority |
| 72 | + gotPropValue := rcvMsg.GetJMSPriority() |
| 73 | + assert.Equal(t, jms20subset.Priority_DEFAULT, gotPropValue) |
| 74 | + |
| 75 | + // ------- |
| 76 | + // Go again with a different priority. |
| 77 | + newMsgPriority := 2 |
| 78 | + producer = producer.SetPriority(newMsgPriority) |
| 79 | + assert.Equal(t, newMsgPriority, producer.GetPriority()) |
| 80 | + |
| 81 | + errSend = producer.Send(queue, txtMsg) |
| 82 | + assert.Nil(t, errSend) |
| 83 | + |
| 84 | + rcvMsg, errRvc = consumer.ReceiveNoWait() |
| 85 | + assert.Nil(t, errRvc) |
| 86 | + assert.NotNil(t, rcvMsg) |
| 87 | + |
| 88 | + switch msg := rcvMsg.(type) { |
| 89 | + case jms20subset.TextMessage: |
| 90 | + assert.Equal(t, msgBody, *msg.GetText()) |
| 91 | + default: |
| 92 | + assert.Fail(t, "Got something other than a text message") |
| 93 | + } |
| 94 | + |
| 95 | + // Check the Priority |
| 96 | + gotPropValue = rcvMsg.GetJMSPriority() |
| 97 | + assert.Equal(t, newMsgPriority, gotPropValue) |
| 98 | + |
| 99 | + // ------- |
| 100 | + // Go again with a different priority. |
| 101 | + newMsgPriority2 := 7 |
| 102 | + producer = producer.SetPriority(newMsgPriority2) |
| 103 | + assert.Equal(t, newMsgPriority2, producer.GetPriority()) |
| 104 | + |
| 105 | + errSend = producer.Send(queue, txtMsg) |
| 106 | + assert.Nil(t, errSend) |
| 107 | + |
| 108 | + rcvMsg, errRvc = consumer.ReceiveNoWait() |
| 109 | + assert.Nil(t, errRvc) |
| 110 | + assert.NotNil(t, rcvMsg) |
| 111 | + |
| 112 | + switch msg := rcvMsg.(type) { |
| 113 | + case jms20subset.TextMessage: |
| 114 | + assert.Equal(t, msgBody, *msg.GetText()) |
| 115 | + default: |
| 116 | + assert.Fail(t, "Got something other than a text message") |
| 117 | + } |
| 118 | + |
| 119 | + // Check the Priority |
| 120 | + gotPropValue = rcvMsg.GetJMSPriority() |
| 121 | + assert.Equal(t, newMsgPriority2, gotPropValue) |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +/* |
| 126 | + * Test the functional behaviour of messages sent with different priorities, |
| 127 | + * where the expectation is that they should be returned from the queue in priority |
| 128 | + * order (with FIFO within a priority group), and not FIFO across the entire queue. |
| 129 | + */ |
| 130 | +func TestPriorityOrdering(t *testing.T) { |
| 131 | + |
| 132 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 133 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 134 | + assert.Nil(t, cfErr) |
| 135 | + |
| 136 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 137 | + // at the end of the function (if it was created successfully) |
| 138 | + context, ctxErr := cf.CreateContext() |
| 139 | + assert.Nil(t, ctxErr) |
| 140 | + if context != nil { |
| 141 | + defer context.Close() |
| 142 | + } |
| 143 | + |
| 144 | + // Set up objects for send/receive |
| 145 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 146 | + consumer, errCons := context.CreateConsumer(queue) |
| 147 | + if consumer != nil { |
| 148 | + defer consumer.Close() |
| 149 | + } |
| 150 | + assert.Nil(t, errCons) |
| 151 | + |
| 152 | + // Send a sequence of messages with different priorities. |
| 153 | + id_msg1_pri5 := sendMessageWithPriority(t, context, queue, 5) |
| 154 | + id_msg2_pri2 := sendMessageWithPriority(t, context, queue, 2) |
| 155 | + id_msg3_pri8 := sendMessageWithPriority(t, context, queue, 8) |
| 156 | + id_msg4_pri2 := sendMessageWithPriority(t, context, queue, 2) |
| 157 | + id_msg5_pri4 := sendMessageWithPriority(t, context, queue, 4) |
| 158 | + id_msg6_pri5 := sendMessageWithPriority(t, context, queue, 5) |
| 159 | + id_msg7_pri2 := sendMessageWithPriority(t, context, queue, 2) |
| 160 | + id_msg8_pri2 := sendMessageWithPriority(t, context, queue, 2) |
| 161 | + id_msg9_pri4 := sendMessageWithPriority(t, context, queue, 4) |
| 162 | + id_msg10_pri1 := sendMessageWithPriority(t, context, queue, 1) |
| 163 | + |
| 164 | + // We expect them to be returned in priority group order (highest first), |
| 165 | + // with FIFO within the priority group. |
| 166 | + // |
| 167 | + // This behaviour relies on MsgDeliverySequence for the queue |
| 168 | + // being set to MQMDS_PRIORITY (which is the default). |
| 169 | + receiveMessageAndCheck(t, consumer, id_msg3_pri8, 8) |
| 170 | + receiveMessageAndCheck(t, consumer, id_msg1_pri5, 5) |
| 171 | + receiveMessageAndCheck(t, consumer, id_msg6_pri5, 5) |
| 172 | + receiveMessageAndCheck(t, consumer, id_msg5_pri4, 4) |
| 173 | + receiveMessageAndCheck(t, consumer, id_msg9_pri4, 4) |
| 174 | + receiveMessageAndCheck(t, consumer, id_msg2_pri2, 2) |
| 175 | + receiveMessageAndCheck(t, consumer, id_msg4_pri2, 2) |
| 176 | + receiveMessageAndCheck(t, consumer, id_msg7_pri2, 2) |
| 177 | + receiveMessageAndCheck(t, consumer, id_msg8_pri2, 2) |
| 178 | + receiveMessageAndCheck(t, consumer, id_msg10_pri1, 1) |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | +// Send a message with the specified priority. |
| 183 | +// Return the generated MessageID. |
| 184 | +func sendMessageWithPriority(t *testing.T, context jms20subset.JMSContext, queue jms20subset.Queue, priority int) string { |
| 185 | + |
| 186 | + ttlMillis := 20000 |
| 187 | + producer := context.CreateProducer().SetTimeToLive(ttlMillis) |
| 188 | + producer = producer.SetPriority(priority) |
| 189 | + |
| 190 | + // Create a TextMessage and check that we can populate it |
| 191 | + msgBody := "PriorityOrderingMsg" |
| 192 | + txtMsg := context.CreateTextMessage() |
| 193 | + txtMsg.SetText(msgBody) |
| 194 | + |
| 195 | + errSend := producer.Send(queue, txtMsg) |
| 196 | + assert.Nil(t, errSend) |
| 197 | + |
| 198 | + return txtMsg.GetJMSMessageID() |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +// Try to receive a message from the queue and check that it matches |
| 203 | +// the expected attributes |
| 204 | +func receiveMessageAndCheck(t *testing.T, consumer jms20subset.JMSConsumer, expectedMsgID string, expectedPriority int) { |
| 205 | + |
| 206 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 207 | + assert.Nil(t, errRvc) |
| 208 | + assert.NotNil(t, rcvMsg) |
| 209 | + |
| 210 | + assert.Equal(t, expectedPriority, rcvMsg.GetJMSPriority()) |
| 211 | + assert.Equal(t, expectedMsgID, rcvMsg.GetJMSMessageID()) |
| 212 | + |
| 213 | +} |
0 commit comments