Skip to content

Commit 34efee6

Browse files
committed
Fix handling of CorrelID less than 24 characters - #54
1 parent a1ef887 commit 34efee6

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

getbycorrelid_test.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestCorrelIDParsing(t *testing.T) {
199199

200200
testCorrel = "ThisIsAVeryLongCorrelationIDWhichIsMoreThanTwentyFourCharacters"
201201
msg.SetJMSCorrelationID(testCorrel)
202-
assert.Equal(t, testCorrel[0:24], msg.GetJMSCorrelationID())
202+
assert.Equal(t, testCorrel[0:12], msg.GetJMSCorrelationID())
203203

204204
// MessageID format
205205
testCorrel = "414d5120514d312020202020202020201017155c0255b621"
@@ -212,3 +212,79 @@ func TestCorrelIDParsing(t *testing.T) {
212212
assert.Equal(t, "", msg.GetJMSCorrelationID())
213213

214214
}
215+
216+
// Do a round-trip send and receive of a message in order to check the correlation ID
217+
func checkCorrelIDOnSendReceive(t *testing.T, context jms20subset.JMSContext, queue jms20subset.Queue,
218+
producer jms20subset.JMSProducer, consumer jms20subset.JMSConsumer,
219+
inputCorrelID string, expectedCorrelID string) {
220+
221+
msg := context.CreateTextMessage()
222+
msg.SetJMSCorrelationID(inputCorrelID)
223+
assert.Equal(t, expectedCorrelID, msg.GetJMSCorrelationID())
224+
sendErr := producer.Send(queue, msg)
225+
assert.Nil(t, sendErr)
226+
227+
rcvMsg, rcvErr := consumer.ReceiveNoWait()
228+
assert.Nil(t, rcvErr)
229+
assert.NotNil(t, rcvMsg)
230+
assert.Equal(t, expectedCorrelID, rcvMsg.GetJMSCorrelationID())
231+
232+
}
233+
234+
/*
235+
* Test that we can round trip various correlation IDs via messages sent
236+
* and received, since there is some checking on send.
237+
*/
238+
func TestCorrelIDParsingOnSend(t *testing.T) {
239+
240+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
241+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
242+
assert.Nil(t, cfErr)
243+
244+
// Creates a connection to the queue manager, using defer to close it automatically
245+
// at the end of the function (if it was created successfully)
246+
context, ctxErr := cf.CreateContext()
247+
assert.Nil(t, ctxErr)
248+
if context != nil {
249+
defer context.Close()
250+
}
251+
252+
// First, check the Send queue is initially empty
253+
queue := context.CreateQueue("DEV.QUEUE.1")
254+
consumer, rConErr := context.CreateConsumer(queue)
255+
assert.Nil(t, rConErr)
256+
if consumer != nil {
257+
defer consumer.Close()
258+
}
259+
reqMsgTest, rcvErr := consumer.ReceiveNoWait()
260+
assert.Nil(t, rcvErr)
261+
assert.Nil(t, reqMsgTest)
262+
263+
producer := context.CreateProducer().SetTimeToLive(1000)
264+
assert.NotNil(t, producer)
265+
266+
// Try out the various combinations of correlation ID
267+
testCorrel := ""
268+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, "")
269+
270+
// MessageID format
271+
testCorrel = "414d5120514d312020202020202020201017155c0255b621"
272+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, testCorrel)
273+
274+
// Empty correlationID
275+
testCorrel = "000000000000000000000000000000000000000000000000"
276+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, "")
277+
278+
testCorrel = "ThisIsAVeryLongCorrelationIDWhichIsMoreThanTwentyFourCharacters"
279+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, testCorrel[0:12])
280+
281+
testCorrel = "Hello World"
282+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, testCorrel)
283+
284+
testCorrel = " "
285+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, testCorrel)
286+
287+
testCorrel = "010203040506"
288+
checkCorrelIDOnSendReceive(t, context, queue, producer, consumer, testCorrel, testCorrel)
289+
290+
}

mqjms/MessageImpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (msg *MessageImpl) SetJMSCorrelationID(correlID string) jms20subset.JMSExce
146146
}
147147

148148
// Store the bytes form of the correlID
149-
msg.mqmd.CorrelId = correlHexBytes
149+
copy(msg.mqmd.CorrelId, []byte(correlHexBytes))
150150

151151
return retErr
152152
}
@@ -213,7 +213,7 @@ func (msg *MessageImpl) GetJMSCorrelationID() string {
213213

214214
// An error occurred while decoding to a plain text string, so encode
215215
// the bytes that we have into a raw string representation themselves.
216-
correlID = hex.EncodeToString(correlIDBytes)
216+
correlID = hex.EncodeToString(correlIDBytes[0:realLength])
217217
}
218218

219219
}

0 commit comments

Comments
 (0)