|
| 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 | + "testing" |
| 14 | + |
| 15 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 16 | + |
| 17 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +/* |
| 22 | + * Test the behaviour of sending a message under a transaction. |
| 23 | + * |
| 24 | + * - put without transaction, immediately available |
| 25 | + * - put multiple msgs with transaction, not immediately available |
| 26 | + * - available after commit |
| 27 | + * - put multiple msgs under transaction then rollback - not available |
| 28 | + * - put message under transaction, close connection - should not be available |
| 29 | + */ |
| 30 | +func TestPutTransaction(t *testing.T) { |
| 31 | + |
| 32 | + // Create a ConnectionFactory using some property files |
| 33 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 34 | + assert.Nil(t, cfErr) |
| 35 | + |
| 36 | + // Creates a connection to the queue manager. |
| 37 | + untransactedContext, errCtx := cf.CreateContext() |
| 38 | + assert.Nil(t, errCtx) |
| 39 | + if untransactedContext != nil { |
| 40 | + defer untransactedContext.Close() |
| 41 | + } |
| 42 | + |
| 43 | + transactedContext, errCtx := cf.CreateContextWithSessionMode(jms20subset.JMSContextSESSIONTRANSACTED) |
| 44 | + assert.Nil(t, errCtx) |
| 45 | + if transactedContext != nil { |
| 46 | + defer transactedContext.Close() |
| 47 | + } |
| 48 | + |
| 49 | + // Create queue objects that points at an IBM MQ queue |
| 50 | + queueName := "DEV.QUEUE.1" |
| 51 | + unQueue := untransactedContext.CreateQueue(queueName) |
| 52 | + trQueue := transactedContext.CreateQueue(queueName) |
| 53 | + |
| 54 | + // Create an untransacted consumer |
| 55 | + untransactedConsumer, errCons := untransactedContext.CreateConsumer(unQueue) |
| 56 | + assert.Nil(t, errCons) |
| 57 | + if untransactedConsumer != nil { |
| 58 | + defer untransactedConsumer.Close() |
| 59 | + } |
| 60 | + |
| 61 | + // Send an untransacted message and check it is immediately available |
| 62 | + untransactedProducer := untransactedContext.CreateProducer().SetTimeToLive(20000) |
| 63 | + bodyTxt := "untransacted-put" |
| 64 | + errSend := untransactedProducer.SendString(unQueue, bodyTxt) |
| 65 | + assert.Nil(t, errSend) |
| 66 | + rcvBody, errRcv := untransactedConsumer.ReceiveStringBodyNoWait() |
| 67 | + assert.Nil(t, errRcv) |
| 68 | + assert.Equal(t, bodyTxt, *rcvBody) |
| 69 | + |
| 70 | + // put multiple msgs with transaction, not immediately available |
| 71 | + transactedProducer := transactedContext.CreateProducer().SetTimeToLive(20000) |
| 72 | + bodyTxt1 := "transacted-put-1" |
| 73 | + bodyTxt2 := "transacted-put-2" |
| 74 | + errSend = transactedProducer.SendString(trQueue, bodyTxt1) |
| 75 | + assert.Nil(t, errSend) |
| 76 | + errSend = transactedProducer.SendString(trQueue, bodyTxt2) |
| 77 | + assert.Nil(t, errSend) |
| 78 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 79 | + assert.Nil(t, errRcv) |
| 80 | + assert.Nil(t, rcvBody) // Expect nil here - message should not have been received. |
| 81 | + |
| 82 | + // Commit and messages should now be available (in order) |
| 83 | + transactedContext.Commit() |
| 84 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 85 | + assert.Nil(t, errRcv) |
| 86 | + assert.Equal(t, bodyTxt1, *rcvBody) |
| 87 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 88 | + assert.Nil(t, errRcv) |
| 89 | + assert.Equal(t, bodyTxt2, *rcvBody) |
| 90 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 91 | + assert.Nil(t, errRcv) |
| 92 | + assert.Nil(t, rcvBody) // Only expected two messages |
| 93 | + |
| 94 | + // put multiple msgs under transaction then rollback - not available |
| 95 | + bodyTxt1 = "transacted-put-rollback-1" |
| 96 | + bodyTxt2 = "transacted-put-rollback-2" |
| 97 | + errSend = transactedProducer.SendString(trQueue, bodyTxt1) |
| 98 | + assert.Nil(t, errSend) |
| 99 | + errSend = transactedProducer.SendString(trQueue, bodyTxt2) |
| 100 | + assert.Nil(t, errSend) |
| 101 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 102 | + assert.Nil(t, errRcv) |
| 103 | + assert.Nil(t, rcvBody) |
| 104 | + transactedContext.Rollback() // Undo the messages |
| 105 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 106 | + assert.Nil(t, errRcv) |
| 107 | + assert.Nil(t, rcvBody) |
| 108 | + transactedContext.Commit() // Should no longer be under the transaction |
| 109 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 110 | + assert.Nil(t, errRcv) |
| 111 | + assert.Nil(t, rcvBody) |
| 112 | + |
| 113 | + // put message under transaction, close connection - should not be available |
| 114 | + errSend = transactedProducer.SendString(trQueue, "orphan1") |
| 115 | + assert.Nil(t, errSend) |
| 116 | + errSend = transactedProducer.SendString(trQueue, "orphan2") |
| 117 | + assert.Nil(t, errSend) |
| 118 | + transactedContext.Close() |
| 119 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 120 | + assert.Nil(t, errRcv) |
| 121 | + assert.Nil(t, rcvBody) |
| 122 | + rcvBody, errRcv = untransactedConsumer.ReceiveStringBodyNoWait() |
| 123 | + assert.Nil(t, errRcv) |
| 124 | + assert.Nil(t, rcvBody) |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +// get-transaction |
| 129 | +// - get without transaction, immediately disappears |
| 130 | +// - get multiple with transaction, immedatiately disappears |
| 131 | +// - rollback, multiple reappear |
| 132 | +// - get multiple with transaction, commit, not available |
| 133 | + |
| 134 | +// get-put transaction |
| 135 | +// - place initial message on queue |
| 136 | +// - get message under transaction, put reply message to different queue |
| 137 | +// - neither request nor reply message is available |
| 138 | +// - rollback; request message is available, reply message is not |
| 139 | +// - (again) get message under transaction, put reply message to different queue |
| 140 | +// - commit; request message is gone, and reply message is available |
0 commit comments