|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2020 |
| 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/mqjms" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +/* |
| 20 | + * Test the behaviour of the cascading close. |
| 21 | + * |
| 22 | + * - Create three consumers |
| 23 | + * - Receive with timeout, get no message |
| 24 | + * - Close second consumer, try to receive again, get error, check 1 + 3 still ok |
| 25 | + * - Ctx.close, try to receive on the other two, get error |
| 26 | + */ |
| 27 | +func TestCascadeClose(t *testing.T) { |
| 28 | + |
| 29 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 30 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 31 | + assert.Nil(t, cfErr) |
| 32 | + |
| 33 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 34 | + // at the end of the function (if it was created successfully) |
| 35 | + context, ctxErr := cf.CreateContext() |
| 36 | + assert.Nil(t, ctxErr) |
| 37 | + // We are testing Close behaviour here, but auto-cleanup just in case. |
| 38 | + if context != nil { |
| 39 | + defer context.Close() |
| 40 | + } |
| 41 | + |
| 42 | + // Equivalent to a JNDI lookup or other declarative definition |
| 43 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 44 | + |
| 45 | + // Set up the consumers 1-3 ready to receive messages. |
| 46 | + consumer1, conErr := context.CreateConsumer(queue) |
| 47 | + assert.Nil(t, conErr) |
| 48 | + // We are testing Close behaviour here, but auto-cleanup just in case. |
| 49 | + if consumer1 != nil { |
| 50 | + defer consumer1.Close() |
| 51 | + } |
| 52 | + |
| 53 | + consumer2, conErr := context.CreateConsumer(queue) |
| 54 | + assert.Nil(t, conErr) |
| 55 | + // We are testing Close behaviour here, but auto-cleanup just in case. |
| 56 | + if consumer2 != nil { |
| 57 | + defer consumer2.Close() |
| 58 | + } |
| 59 | + |
| 60 | + consumer3, conErr := context.CreateConsumer(queue) |
| 61 | + assert.Nil(t, conErr) |
| 62 | + // We are testing Close behaviour here, but auto-cleanup just in case. |
| 63 | + if consumer3 != nil { |
| 64 | + defer consumer3.Close() |
| 65 | + } |
| 66 | + |
| 67 | + // Check no message on the queue to start with |
| 68 | + testMsg, err := consumer1.ReceiveNoWait() |
| 69 | + assert.Nil(t, err) |
| 70 | + assert.Nil(t, testMsg) |
| 71 | + |
| 72 | + testMsg, err = consumer2.ReceiveNoWait() |
| 73 | + assert.Nil(t, err) |
| 74 | + assert.Nil(t, testMsg) |
| 75 | + |
| 76 | + testMsg, err = consumer3.ReceiveNoWait() |
| 77 | + assert.Nil(t, err) |
| 78 | + assert.Nil(t, testMsg) |
| 79 | + |
| 80 | + // Close second consumer, try to receive again, get error, check 1 + 3 still ok |
| 81 | + consumer2.Close() |
| 82 | + |
| 83 | + testMsg, err = consumer1.ReceiveNoWait() |
| 84 | + assert.Nil(t, err) |
| 85 | + assert.Nil(t, testMsg) |
| 86 | + |
| 87 | + testMsg, err = consumer2.ReceiveNoWait() |
| 88 | + assert.NotNil(t, err) |
| 89 | + assert.Equal(t, "2019", err.GetErrorCode()) |
| 90 | + assert.Equal(t, "MQRC_HOBJ_ERROR", err.GetReason()) |
| 91 | + assert.Nil(t, testMsg) |
| 92 | + |
| 93 | + testMsg, err = consumer3.ReceiveNoWait() |
| 94 | + assert.Nil(t, err) |
| 95 | + assert.Nil(t, testMsg) |
| 96 | + |
| 97 | + // Close a closed thing to check it doesn't complain. |
| 98 | + consumer2.Close() |
| 99 | + |
| 100 | + // Ctx.close, try to receive on the other two, get error |
| 101 | + context.Close() |
| 102 | + |
| 103 | + testMsg, err = consumer1.ReceiveNoWait() |
| 104 | + assert.NotNil(t, err) |
| 105 | + assert.Equal(t, "2018", err.GetErrorCode()) |
| 106 | + assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason()) |
| 107 | + assert.Nil(t, testMsg) |
| 108 | + |
| 109 | + testMsg, err = consumer2.ReceiveNoWait() |
| 110 | + assert.NotNil(t, err) |
| 111 | + assert.Equal(t, "2018", err.GetErrorCode()) |
| 112 | + assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason()) |
| 113 | + assert.Nil(t, testMsg) |
| 114 | + |
| 115 | + testMsg, err = consumer3.ReceiveNoWait() |
| 116 | + assert.NotNil(t, err) |
| 117 | + assert.Equal(t, "2018", err.GetErrorCode()) |
| 118 | + assert.Equal(t, "MQRC_HCONN_ERROR", err.GetReason()) |
| 119 | + assert.Nil(t, testMsg) |
| 120 | + |
| 121 | +} |
0 commit comments