|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2023 |
| 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 | + "fmt" |
| 14 | + "runtime" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 19 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +/* |
| 24 | + * Test for memory leak when there is no message to be received. |
| 25 | + * |
| 26 | + * This test is not included in the normal bucket as it sends an enormous number of |
| 27 | + * messages, and requires human observation of the total process size to establish whether |
| 28 | + * it passes or not, so can only be run under human supervision |
| 29 | + */ |
| 30 | +func DONT_RUNTestLeakOnEmptyGet(t *testing.T) { |
| 31 | + |
| 32 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 33 | + //cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 34 | + //assert.Nil(t, cfErr) |
| 35 | + |
| 36 | + // Initialise the attributes of the CF in whatever way you like |
| 37 | + cf := mqjms.ConnectionFactoryImpl{ |
| 38 | + QMName: "QM1", |
| 39 | + Hostname: "localhost", |
| 40 | + PortNumber: 1414, |
| 41 | + ChannelName: "DEV.APP.SVRCONN", |
| 42 | + UserName: "app", |
| 43 | + Password: "passw0rd", |
| 44 | + } |
| 45 | + |
| 46 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 47 | + // at the end of the function (if it was created successfully) |
| 48 | + context, ctxErr := cf.CreateContext() |
| 49 | + assert.Nil(t, ctxErr) |
| 50 | + if context != nil { |
| 51 | + defer context.Close() |
| 52 | + } |
| 53 | + |
| 54 | + // Now send the message and get it back again, to check that it roundtripped. |
| 55 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 56 | + |
| 57 | + consumer, errCons := context.CreateConsumer(queue) |
| 58 | + if consumer != nil { |
| 59 | + defer consumer.Close() |
| 60 | + } |
| 61 | + assert.Nil(t, errCons) |
| 62 | + |
| 63 | + for i := 1; i < 35000; i++ { |
| 64 | + |
| 65 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 66 | + assert.Nil(t, errRvc) |
| 67 | + assert.Nil(t, rcvMsg) |
| 68 | + |
| 69 | + if i%1000 == 0 { |
| 70 | + fmt.Println("Messages:", i) |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + fmt.Println("Finished receive calls - waiting for cooldown.") |
| 76 | + runtime.GC() |
| 77 | + |
| 78 | + time.Sleep(30 * time.Second) |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +/* |
| 83 | + * Test for memory leak when sending and receiving messages |
| 84 | + * |
| 85 | + * This test is not included in the normal bucket as it sends an enormous number of |
| 86 | + * messages, and requires human observation of the total process size to establish whether |
| 87 | + * it passes or not, so can only be run under human supervision |
| 88 | + */ |
| 89 | +func DONTRUN_TestLeakOnPutGet(t *testing.T) { |
| 90 | + |
| 91 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 92 | + //cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 93 | + //assert.Nil(t, cfErr) |
| 94 | + |
| 95 | + // Initialise the attributes of the CF in whatever way you like |
| 96 | + cf := mqjms.ConnectionFactoryImpl{ |
| 97 | + QMName: "QM1", |
| 98 | + Hostname: "localhost", |
| 99 | + PortNumber: 1414, |
| 100 | + ChannelName: "DEV.APP.SVRCONN", |
| 101 | + UserName: "app", |
| 102 | + Password: "passw0rd", |
| 103 | + } |
| 104 | + |
| 105 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 106 | + // at the end of the function (if it was created successfully) |
| 107 | + context, ctxErr := cf.CreateContext() |
| 108 | + assert.Nil(t, ctxErr) |
| 109 | + if context != nil { |
| 110 | + defer context.Close() |
| 111 | + } |
| 112 | + |
| 113 | + // Now send the message and get it back again, to check that it roundtripped. |
| 114 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 115 | + |
| 116 | + consumer, errCons := context.CreateConsumer(queue) |
| 117 | + if consumer != nil { |
| 118 | + defer consumer.Close() |
| 119 | + } |
| 120 | + assert.Nil(t, errCons) |
| 121 | + |
| 122 | + ttlMillis := 20000 |
| 123 | + producer := context.CreateProducer().SetTimeToLive(ttlMillis) |
| 124 | + |
| 125 | + for i := 1; i < 25000; i++ { |
| 126 | + |
| 127 | + // Create a TextMessage and check that we can populate it |
| 128 | + msgBody := "Message " + fmt.Sprint(i) |
| 129 | + txtMsg := context.CreateTextMessage() |
| 130 | + txtMsg.SetText(msgBody) |
| 131 | + txtMsg.SetIntProperty("MessageNumber", i) |
| 132 | + |
| 133 | + errSend := producer.Send(queue, txtMsg) |
| 134 | + assert.Nil(t, errSend) |
| 135 | + |
| 136 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 137 | + assert.Nil(t, errRvc) |
| 138 | + assert.NotNil(t, rcvMsg) |
| 139 | + |
| 140 | + // Check message body. |
| 141 | + switch msg := rcvMsg.(type) { |
| 142 | + case jms20subset.TextMessage: |
| 143 | + assert.Equal(t, msgBody, *msg.GetText()) |
| 144 | + default: |
| 145 | + assert.Fail(t, "Got something other than a text message") |
| 146 | + } |
| 147 | + |
| 148 | + // Check messageID |
| 149 | + assert.Equal(t, txtMsg.GetJMSMessageID(), rcvMsg.GetJMSMessageID()) |
| 150 | + |
| 151 | + // Check int property |
| 152 | + rcvMsgNum, propErr := rcvMsg.GetIntProperty("MessageNumber") |
| 153 | + assert.Nil(t, propErr) |
| 154 | + assert.Equal(t, i, rcvMsgNum) |
| 155 | + |
| 156 | + if i%1000 == 0 { |
| 157 | + fmt.Println("Messages:", i) |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + fmt.Println("Finished receive calls - waiting for cooldown.") |
| 163 | + runtime.GC() |
| 164 | + |
| 165 | + time.Sleep(30 * time.Second) |
| 166 | + |
| 167 | +} |
0 commit comments