-
Notifications
You must be signed in to change notification settings - Fork 19
/
asyncput_test.go
755 lines (596 loc) · 25.9 KB
/
asyncput_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* Copyright (c) IBM Corporation 2021
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package main
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
/*
* Minimal example showing how to send a message asynchronously, which can give a higher
* rate of sending non-persistent messages, in exchange for less/no checking for errors.
*/
func TestAsyncPutSample(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if context != nil {
defer context.Close()
}
// Set up a Producer for NonPersistent messages and Destination the PutAsyncAllowed=true
producer := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT)
asyncQueue := context.CreateQueue("DEV.QUEUE.1").SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
// Send a message (asynchronously)
msg := context.CreateTextMessageWithString("some text")
errSend := producer.Send(asyncQueue, msg)
assert.Nil(t, errSend)
// Tidy up the message to leave the test clean.
consumer, errCons := context.CreateConsumer(asyncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
_, errRvc := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, errRvc)
}
/*
* Compare the performance benefit of sending messages non-persistent, non-transactional
* messages asynchronously - which can give a higher message rate, in exchange for
* less/no checking for errors.
*
* The test checks that async put is at least 10% faster than synchronous put.
* (in testing against a remote queue manager it was actually 30% faster)
*/
func TestAsyncPutComparison(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Check the default value for SendCheckCount, which means never check for errors.
assert.Equal(t, 0, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if context != nil {
defer context.Close()
}
// --------------------------------------------------------
// Start by sending a set of messages using the normal synchronous approach, in
// order that we can get a baseline timing.
// Set up the producer and consumer with the SYNCHRONOUS (not async yet) queue
syncQueue := context.CreateQueue("DEV.QUEUE.1")
producer := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT).SetTimeToLive(60000)
consumer, errCons := context.CreateConsumer(syncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
syncMsgPrefix := "syncput_" + testcasePrefix + "_"
asyncMsgPrefix := "asyncput_" + testcasePrefix + "_"
numberMessages := 50
// First get a baseline for how long it takes us to send the batch of messages
// WITHOUT async put (i.e. using normal synchronous put)
syncStartTime := currentTimeMillis()
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := context.CreateTextMessageWithString(syncMsgPrefix + strconv.Itoa(i))
errSend := producer.Send(syncQueue, msg)
assert.Nil(t, errSend)
}
syncEndTime := currentTimeMillis()
syncSendTime := syncEndTime - syncStartTime
//fmt.Println("Took " + strconv.FormatInt(syncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " synchronous messages.")
// Receive the messages back again to tidy the queue back to a clean state
finishedReceiving := false
rcvCount := 0
for !finishedReceiving {
rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, errRvc)
if rcvTxt != nil {
// Check the message bod matches what we expect
assert.Equal(t, syncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt)
rcvCount++
} else {
finishedReceiving = true
}
}
// --------------------------------------------------------
// Now repeat the experiment but with ASYNC message put
asyncQueue := context.CreateQueue("DEV.QUEUE.1").SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
asyncStartTime := currentTimeMillis()
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := context.CreateTextMessageWithString(asyncMsgPrefix + strconv.Itoa(i))
errSend := producer.Send(asyncQueue, msg)
assert.Nil(t, errSend)
}
asyncEndTime := currentTimeMillis()
asyncSendTime := asyncEndTime - asyncStartTime
//fmt.Println("Took " + strconv.FormatInt(asyncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " ASYNC messages.")
// Receive the messages back again to tidy the queue back to a clean state
finishedReceiving = false
rcvCount = 0
for !finishedReceiving {
rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, errRvc)
if rcvTxt != nil {
// Check the message bod matches what we expect
assert.Equal(t, asyncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt)
rcvCount++
} else {
finishedReceiving = true
}
}
assert.Equal(t, numberMessages, rcvCount)
// Expect that async put is at least 10% faster than sync put for non-persistent messages
// (in testing against a remote queue manager it was actually 30% faster)
assert.True(t, 100*asyncSendTime < 90*syncSendTime)
}
/*
* Test the ability to successfully send async messages with checking enabled.
*
* This test is checking that no failures are reported when the interval checking
* is enabled.
*/
func TestAsyncPutCheckCount(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Set the CF flag to enable checking for errors after a certain number of messages
cf.SendCheckCount = 10
// Check the default value for SendCheckCount
assert.Equal(t, 10, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if context != nil {
defer context.Close()
}
// Set up the producer and consumer with the async queue.
asyncQueue := context.CreateQueue("DEV.QUEUE.1").SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
producer := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT)
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
msgPrefix := "checkCount_" + testcasePrefix + "_"
numberMessages := 50
// --------------------------------------------------------
// Do ASYNC message put
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := context.CreateTextMessageWithString(msgPrefix + strconv.Itoa(i))
errSend := producer.Send(asyncQueue, msg)
assert.Nil(t, errSend)
}
// ----------------------------------
// Receive the messages back again to tidy the queue back to a clean state
consumer, errCons := context.CreateConsumer(asyncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
finishedReceiving := false
for !finishedReceiving {
rcvMsg, errRvc := consumer.ReceiveNoWait()
assert.Nil(t, errRvc)
if rcvMsg == nil {
finishedReceiving = true
}
}
}
/*
* Validate that errors are reported at the correct interval when a problem occurs.
*
* This test case forces a failure to occur by sending 50 messages to a queue that has had its
* maximum depth set to 25. With SendCheckCount of 10 we will not receive an error until message 30,
* which is the first time the error check is made after the point at which the queue has filled up.
*/
func TestAsyncPutCheckCountWithFailure(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Set the CF flag to enable checking for errors after a certain number of messages
cf.SendCheckCount = 10
// Check the value for SendCheckCount was stored correctly.
assert.Equal(t, 10, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if context != nil {
defer context.Close()
}
// Set up the producer and consumer with the async queue.
QUEUE_25_NAME := "DEV.MAXDEPTH25"
asyncQueue := context.CreateQueue(QUEUE_25_NAME).SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
producer := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT)
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
msgPrefix := "checkCount_" + testcasePrefix + "_"
numberMessages := 50
// Variable to track whether the queue exists or not.
queueExists := true
// --------------------------------------------------------
// Send ASYNC message put
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := context.CreateTextMessageWithString(msgPrefix + strconv.Itoa(i))
errSend := producer.Send(asyncQueue, msg)
// Messages will start to fail at number 25 but we don't get an error until
// the next check which takes place at 30.
if i == 0 && isUnknownObjectName(errSend) {
fmt.Println("Skipping TestAsyncPutCheckCountWithFailure as queue " + QUEUE_25_NAME + " is not defined.")
queueExists = false
break // Stop the loop at this point as we know it won't change.
} else if i < 30 {
assert.Nil(t, errSend)
} else if i == 30 {
assert.NotNil(t, errSend)
assert.Equal(t, "AsyncPutFailure", errSend.GetErrorCode())
// Message should be "N failures"
assert.True(t, strings.Contains(errSend.GetReason(), "6 failures"))
assert.True(t, strings.Contains(errSend.GetReason(), "0 warnings"))
// Linked message should have reason of MQRC_Q_FULL
linkedErr := errSend.GetLinkedError()
assert.NotNil(t, linkedErr)
linkedReason := linkedErr.(jms20subset.JMSExceptionImpl).GetReason()
assert.Equal(t, "MQRC_Q_FULL", linkedReason)
} else if i == 40 {
assert.NotNil(t, errSend)
assert.Equal(t, "AsyncPutFailure", errSend.GetErrorCode())
// Message should be "N failures"
assert.True(t, strings.Contains(errSend.GetReason(), "10 failures")) // all of these failed
assert.True(t, strings.Contains(errSend.GetReason(), "0 warnings"))
// Linked message should have reason of MQRC_Q_FULL
linkedErr := errSend.GetLinkedError()
assert.NotNil(t, linkedErr)
linkedReason := linkedErr.(jms20subset.JMSExceptionImpl).GetReason()
assert.Equal(t, "MQRC_Q_FULL", linkedReason)
} else {
// Messages 31, 32, ... 39, 41, 42, ...
// do not give an error because we don't make an error check.
assert.Nil(t, errSend)
}
}
// If the queue exists then tidy up the messages we sent.
if queueExists {
// ----------------------------------
// Receive the messages back again to tidy the queue back to a clean state
consumer, errCons := context.CreateConsumer(asyncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
// Receive the messages back again
finishedReceiving := false
for !finishedReceiving {
rcvMsg, errRvc := consumer.ReceiveNoWait()
assert.Nil(t, errRvc)
if rcvMsg == nil {
finishedReceiving = true
}
}
}
}
/*
* Test the getter/setter functions for controlling async put.
*/
func TestAsyncPutGetterSetter(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if context != nil {
defer context.Close()
}
// Set up the producer and consumer
queue := context.CreateQueue("DEV.QUEUE.1")
// Check the default
assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST, queue.GetPutAsyncAllowed())
// Check enabled
queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED, queue.GetPutAsyncAllowed())
// Check disabled
queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_DISABLED)
assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_DISABLED, queue.GetPutAsyncAllowed())
// Check as-dest
queue = queue.SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST)
assert.Equal(t, jms20subset.Destination_PUT_ASYNC_ALLOWED_AS_DEST, queue.GetPutAsyncAllowed())
}
/*
* Compare the performance benefit of sending messages persistent, transactional
* messages asynchronously - which can give a higher message rate, in exchange for
* less/no checking for errors.
*
* The test checks that async put is at least 10% faster than synchronous put.
* (in testing against a remote queue manager it was actually 30% faster)
*/
func TestAsyncPutPersistentTransactedComparison(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// Check the default value for SendCheckCount, which means never check for errors.
assert.Equal(t, 0, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
transactedContext, ctxErr := cf.CreateContextWithSessionMode(jms20subset.JMSContextSESSIONTRANSACTED)
assert.Nil(t, ctxErr)
if transactedContext != nil {
defer transactedContext.Close()
}
// --------------------------------------------------------
// Start by sending a set of messages using the normal synchronous approach, in
// order that we can get a baseline timing.
// Set up the producer and consumer with the SYNCHRONOUS (not async yet) queue
syncQueue := transactedContext.CreateQueue("DEV.QUEUE.1")
producer := transactedContext.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT).SetTimeToLive(60000)
consumer, errCons := transactedContext.CreateConsumer(syncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
syncMsgPrefix := "syncput_" + testcasePrefix + "_"
asyncMsgPrefix := "asyncput_" + testcasePrefix + "_"
numberMessages := 50
// First get a baseline for how long it takes us to send the batch of messages
// WITHOUT async put (i.e. using normal synchronous put)
syncStartTime := currentTimeMillis()
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := transactedContext.CreateTextMessageWithString(syncMsgPrefix + strconv.Itoa(i))
errSend := producer.Send(syncQueue, msg)
assert.Nil(t, errSend)
}
transactedContext.Commit()
syncEndTime := currentTimeMillis()
syncSendTime := syncEndTime - syncStartTime
//fmt.Println("Took " + strconv.FormatInt(syncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " transacted synchronous messages.")
// Receive the messages back again to tidy the queue back to a clean state
finishedReceiving := false
rcvCount := 0
for !finishedReceiving {
rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, errRvc)
if rcvTxt != nil {
// Check the message bod matches what we expect
assert.Equal(t, syncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt)
rcvCount++
} else {
finishedReceiving = true
}
}
transactedContext.Commit()
// --------------------------------------------------------
// Now repeat the experiment but with ASYNC message put
asyncQueue := transactedContext.CreateQueue("DEV.QUEUE.1").SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
asyncStartTime := currentTimeMillis()
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := transactedContext.CreateTextMessageWithString(asyncMsgPrefix + strconv.Itoa(i))
errSend := producer.Send(asyncQueue, msg)
assert.Nil(t, errSend)
}
transactedContext.Commit()
asyncEndTime := currentTimeMillis()
asyncSendTime := asyncEndTime - asyncStartTime
//fmt.Println("Took " + strconv.FormatInt(asyncSendTime, 10) + "ms to send " + strconv.Itoa(numberMessages) + " transacted ASYNC messages.")
// Receive the messages back again to tidy the queue back to a clean state
finishedReceiving = false
rcvCount = 0
for !finishedReceiving {
rcvTxt, errRvc := consumer.ReceiveStringBodyNoWait()
assert.Nil(t, errRvc)
if rcvTxt != nil {
// Check the message bod matches what we expect
assert.Equal(t, asyncMsgPrefix+strconv.Itoa(rcvCount), *rcvTxt)
rcvCount++
} else {
finishedReceiving = true
}
}
transactedContext.Commit()
assert.Equal(t, numberMessages, rcvCount)
// Expect that async put is at least 10% faster than sync put for non-persistent messages
// (in testing against a remote queue manager it was actually 30% faster)
assert.True(t, 100*asyncSendTime < 90*syncSendTime)
}
/*
* Validate that errors are reported at the correct interval when a problem occurs during a
* transactional put of persistent messages (i.e. when the commit occurs)
*
* This test case forces a failure to occur by sending 50 messages to a queue that has had its
* maximum depth set to 25. In the transacted async put case the Send method always completes
* successfully, but the error will be returned on Commit.
*/
func TestAsyncPutTransactedCheckCountWithFailure(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// The SendCheckCount is not used for async put under a transacted session.
assert.Equal(t, 0, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
transactedContext, ctxErr := cf.CreateContextWithSessionMode(jms20subset.JMSContextSESSIONTRANSACTED)
assert.Nil(t, ctxErr)
if transactedContext != nil {
defer transactedContext.Close()
}
// Set up the producer and consumer with the async queue.
QUEUE_25_NAME := "DEV.MAXDEPTH25"
asyncQueue := transactedContext.CreateQueue(QUEUE_25_NAME).SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
producer := transactedContext.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT).SetTimeToLive(60000)
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
msgPrefix := "checkCount_" + testcasePrefix + "_"
numberMessages := 50
// Variable to track whether the queue exists or not.
queueExists := true
// --------------------------------------------------------
// Send ASYNC message put
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := transactedContext.CreateTextMessageWithString(msgPrefix + strconv.Itoa(i))
errSend2 := producer.Send(asyncQueue, msg)
// In the Transacted case the response from Send is always Nil, because any errors
// will be reflected on the Commit call.
assert.Nil(t, errSend2)
// Skip the test if the destination does not exist on this queue manager.
if i == 0 {
if err := transactedContext.Commit(); isUnknownObjectName(err) {
fmt.Println("Skipping TestAsyncPutTransactedCheckCountWithFailure as queue " + QUEUE_25_NAME + " is not defined.")
queueExists = false
break // Stop the loop at this point as we know it won't change.
}
}
if i%10 == 0 {
commitErr := transactedContext.Commit()
if i == 30 || i == 40 {
assert.NotNil(t, commitErr)
assert.Equal(t, "2003", commitErr.GetErrorCode())
assert.Equal(t, "MQRC_BACKED_OUT", commitErr.GetReason())
// Linked error is out normal async put error with message about "N failures"
linkedErr1 := commitErr.GetLinkedError()
assert.Equal(t, "AsyncPutFailure", linkedErr1.(jms20subset.JMSExceptionImpl).GetErrorCode())
assert.True(t, strings.Contains(linkedErr1.(jms20subset.JMSExceptionImpl).GetReason(), "6 failures"))
assert.True(t, strings.Contains(linkedErr1.(jms20subset.JMSExceptionImpl).GetReason(), "0 warnings"))
// Second level linked message should have reason of MQRC_Q_FULL
linkedErr2 := linkedErr1.(jms20subset.JMSExceptionImpl).GetLinkedError()
assert.NotNil(t, linkedErr2)
linkedReason := linkedErr2.(jms20subset.JMSExceptionImpl).GetReason()
assert.Equal(t, "MQRC_Q_FULL", linkedReason)
} else {
// Messages 31, 32, ... 39, 41, 42, ...
// do not give an error because we don't make an error check.
assert.Nil(t, commitErr)
}
}
}
// Clear out the transaction context.
transactedContext.Commit()
// If the queue exists then tidy up the messages we sent.
if queueExists {
// ----------------------------------
// Receive the messages back again to tidy the queue back to a clean state
consumer, errCons := transactedContext.CreateConsumer(asyncQueue)
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
// Receive the messages back again
finishedReceiving := false
for !finishedReceiving {
rcvMsg, errRvc := consumer.ReceiveNoWait()
assert.Nil(t, errRvc)
if rcvMsg == nil {
finishedReceiving = true
}
}
transactedContext.Commit()
}
}
/*
* Validate that NO errors are reported when a problem occurs during a
* transactional put of a non-persistent message.
*
* (per https://www.ibm.com/docs/en/ibm-mq/9.1?topic=application-putting-messages-asynchronously-in-mq-classes-jms)
*
* This test case forces a failure to occur by sending 50 messages to a queue that has had its
* maximum depth set to 25.
*/
func TestAsyncPutTransactedNonPersistentCheckCountWithFailure(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)
// The SendCheckCount is not used for async put under a transacted session.
assert.Equal(t, 0, cf.SendCheckCount)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
transactedContext, ctxErr := cf.CreateContextWithSessionMode(jms20subset.JMSContextSESSIONTRANSACTED)
assert.Nil(t, ctxErr)
if transactedContext != nil {
defer transactedContext.Close()
}
// Set up the producer and consumer with the async queue.
QUEUE_25_NAME := "DEV.MAXDEPTH25"
asyncQueue := transactedContext.CreateQueue(QUEUE_25_NAME).SetPutAsyncAllowed(jms20subset.Destination_PUT_ASYNC_ALLOWED_ENABLED)
producer := transactedContext.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT).SetTimeToLive(60000)
// Create a unique message prefix representing this execution of the test case.
testcasePrefix := strconv.FormatInt(currentTimeMillis(), 10)
msgPrefix := "checkCount_" + testcasePrefix + "_"
numberMessages := 50
// Variable to track whether the queue exists or not.
queueExists := true
// --------------------------------------------------------
// Send ASYNC message put
for i := 0; i < numberMessages; i++ {
// Create a TextMessage and send it.
msg := transactedContext.CreateTextMessageWithString(msgPrefix + strconv.Itoa(i))
errSend2 := producer.Send(asyncQueue, msg)
// In the Transacted case the response from Send is always Nil, because any errors
// will be reflected on the Commit call.
assert.Nil(t, errSend2)
if i%10 == 0 {
commitErr := transactedContext.Commit()
// No errors reported for NonPersistent messages in a transaction.
assert.Nil(t, commitErr)
}
}
// ----------------------------------
// Receive the messages back again to tidy the queue back to a clean state
consumer, errCons := transactedContext.CreateConsumer(asyncQueue)
// Skip the test if the destination does not exist on this queue manager.
if isUnknownObjectName(errCons) {
fmt.Println("Skipping TestAsyncPutTransactedNonPersistentCheckCountWithFailure as queue " + QUEUE_25_NAME + " is not defined.")
queueExists = false
}
// If the queue exists then tidy up the messages we sent.
if queueExists {
assert.Nil(t, errCons)
if consumer != nil {
defer consumer.Close()
}
// Receive the messages back again
finishedReceiving := false
for !finishedReceiving {
rcvMsg, errRvc := consumer.ReceiveNoWait()
assert.Nil(t, errRvc)
if rcvMsg == nil {
finishedReceiving = true
}
}
transactedContext.Commit()
}
}
func isUnknownObjectName(exception jms20subset.JMSException) bool {
if exception != nil {
if exception.GetReason() == "MQRC_UNKNOWN_OBJECT_NAME" {
return true
}
if err, ok := exception.GetLinkedError().(jms20subset.JMSExceptionImpl); ok {
return err.GetReason() == "MQRC_UNKNOWN_OBJECT_NAME" || isUnknownObjectName(err)
}
}
return false
}