@@ -134,7 +134,7 @@ func TestLargeReceiveStringBodyTextMessage(t *testing.T) {
134
134
135
135
// The message is still left on the queue since it failed to be received successfully.
136
136
137
- // Since the buffer size is configured using the ConnectionFactoy we will
137
+ // Since the buffer size is configured using the ConnectionFactory we will
138
138
// create a second connection in order to successfully retrieve the message.
139
139
cf .ReceiveBufferSize = len (txtOver32kb ) + 50
140
140
@@ -295,6 +295,80 @@ func TestLargeReceiveBytesBodyBytesMessage(t *testing.T) {
295
295
296
296
}
297
297
298
+ /*
299
+ * Test receiving a truncated message, where the body of the message is larger than the receive buffer.
300
+ */
301
+ func TestTruncatedTextMessage (t * testing.T ) {
302
+
303
+ // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
304
+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
305
+ cf .ReceiveBufferSize = 1024
306
+ // TODO - set parameter to allow receive of truncated message
307
+ assert .Nil (t , cfErr )
308
+
309
+ // Creates a connection to the queue manager, using defer to close it automatically
310
+ // at the end of the function (if it was created successfully)
311
+ context , ctxErr := cf .CreateContext ()
312
+ assert .Nil (t , ctxErr )
313
+ if context != nil {
314
+ defer context .Close ()
315
+ }
316
+
317
+ // Get a long text string over 32kb in length
318
+ txtOver32kb := getStringOver32kb ()
319
+
320
+ // Create a TextMessage with it
321
+ msg := context .CreateTextMessageWithString (txtOver32kb )
322
+
323
+ // Now send the message and get it back again.
324
+ queue := context .CreateQueue ("DEV.QUEUE.1" )
325
+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).Send (queue , msg )
326
+ assert .Nil (t , errSend )
327
+
328
+ consumer , errCons := context .CreateConsumer (queue ) // with 1kb buffer size as applied at the beginning of this test
329
+ assert .Nil (t , errCons )
330
+ if consumer != nil {
331
+ defer consumer .Close ()
332
+ }
333
+
334
+ // This will fail because the default buffer size when receiving a
335
+ // message is 32kb.
336
+ truncMsg , errRcv := consumer .ReceiveNoWait ()
337
+ assert .NotNil (t , errRcv )
338
+ assert .Equal (t , "MQRC_TRUNCATED_MSG_ACCEPTED" , errRcv .GetReason ())
339
+ assert .Equal (t , "2079" , errRcv .GetErrorCode ())
340
+
341
+ assert .NotNil (t , truncMsg ) // We have received the message, but it is truncated.
342
+
343
+ switch txtTruncMsg := truncMsg .(type ) {
344
+ case jms20subset.TextMessage :
345
+ receivedTxt := txtTruncMsg .GetText ()
346
+ assert .Equal (t , cf .ReceiveBufferSize , len (* receivedTxt ))
347
+ default :
348
+ assert .Fail (t , "Got something other than a text message" )
349
+ }
350
+
351
+ // Make sure we tidy up in case the previous part of the test failed.
352
+ cf .ReceiveBufferSize = len (txtOver32kb ) + 50
353
+
354
+ context2 , ctxErr2 := cf .CreateContext ()
355
+ assert .Nil (t , ctxErr2 )
356
+ if context2 != nil {
357
+ defer context2 .Close ()
358
+ }
359
+
360
+ consumer2 , errCons2 := context2 .CreateConsumer (queue )
361
+ assert .Nil (t , errCons2 )
362
+ if consumer2 != nil {
363
+ defer consumer2 .Close ()
364
+ }
365
+
366
+ rcvMsg2 , errRcv2 := consumer2 .ReceiveNoWait ()
367
+ assert .Nil (t , errRcv2 )
368
+ assert .NotNil (t , rcvMsg2 )
369
+
370
+ }
371
+
298
372
func getStringOver32kb () string {
299
373
300
374
// Build a text string which is over 32KB (in a not very efficient way!)
0 commit comments