|
| 1 | +namespace ServiceControl.Transport.Tests; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using IBM.WMQ; |
| 7 | +using NUnit.Framework; |
| 8 | +using Transports; |
| 9 | +using Transports.IBMMQ; |
| 10 | + |
| 11 | +[TestFixture] |
| 12 | +class DeadLetterQueueCheckTests |
| 13 | +{ |
| 14 | + [Test] |
| 15 | + public async Task Should_pass_when_custom_checks_disabled() |
| 16 | + { |
| 17 | + var settings = new TransportSettings |
| 18 | + { |
| 19 | + ConnectionString = ConnectionString, |
| 20 | + RunCustomChecks = false |
| 21 | + }; |
| 22 | + |
| 23 | + var check = new DeadLetterQueueCheck(settings); |
| 24 | + var result = await check.PerformCheck().ConfigureAwait(false); |
| 25 | + |
| 26 | + Assert.That(result.HasFailed, Is.False); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task Should_pass_when_dlq_is_empty() |
| 31 | + { |
| 32 | + DrainDeadLetterQueue(); |
| 33 | + |
| 34 | + var settings = new TransportSettings |
| 35 | + { |
| 36 | + ConnectionString = ConnectionString, |
| 37 | + RunCustomChecks = true |
| 38 | + }; |
| 39 | + |
| 40 | + var check = new DeadLetterQueueCheck(settings); |
| 41 | + var result = await check.PerformCheck().ConfigureAwait(false); |
| 42 | + |
| 43 | + Assert.That(result.HasFailed, Is.False); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public async Task Should_fail_when_dlq_has_messages() |
| 48 | + { |
| 49 | + DrainDeadLetterQueue(); |
| 50 | + PutMessageOnDeadLetterQueue(); |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + var settings = new TransportSettings |
| 55 | + { |
| 56 | + ConnectionString = ConnectionString, |
| 57 | + RunCustomChecks = true |
| 58 | + }; |
| 59 | + |
| 60 | + var check = new DeadLetterQueueCheck(settings); |
| 61 | + var result = await check.PerformCheck().ConfigureAwait(false); |
| 62 | + |
| 63 | + Assert.That(result.HasFailed, Is.True); |
| 64 | + Assert.That(result.FailureReason, Does.Contain("messages in the Dead Letter Queue")); |
| 65 | + } |
| 66 | + finally |
| 67 | + { |
| 68 | + DrainDeadLetterQueue(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public async Task Should_fail_when_connection_is_invalid() |
| 74 | + { |
| 75 | + var settings = new TransportSettings |
| 76 | + { |
| 77 | + ConnectionString = "mq://admin:passw0rd@localhost:19999/BOGUS", |
| 78 | + RunCustomChecks = true |
| 79 | + }; |
| 80 | + |
| 81 | + var check = new DeadLetterQueueCheck(settings); |
| 82 | + var result = await check.PerformCheck().ConfigureAwait(false); |
| 83 | + |
| 84 | + Assert.That(result.HasFailed, Is.True); |
| 85 | + Assert.That(result.FailureReason, Does.Contain("Unable to check Dead Letter Queue")); |
| 86 | + Assert.That(result.FailureReason, Does.Contain("RC=")); |
| 87 | + } |
| 88 | + |
| 89 | + static void PutMessageOnDeadLetterQueue() |
| 90 | + { |
| 91 | + var (qmName, props) = ParseConnectionString(); |
| 92 | + using var qm = new MQQueueManager(qmName, props); |
| 93 | + var dlqName = qm.DeadLetterQueueName.Trim(); |
| 94 | + using var dlq = qm.AccessQueue(dlqName, MQC.MQOO_OUTPUT); |
| 95 | + var msg = new MQMessage(); |
| 96 | + msg.WriteString("DLQ test message"); |
| 97 | + dlq.Put(msg); |
| 98 | + } |
| 99 | + |
| 100 | + static void DrainDeadLetterQueue() |
| 101 | + { |
| 102 | + var (qmName, props) = ParseConnectionString(); |
| 103 | + using var qm = new MQQueueManager(qmName, props); |
| 104 | + var dlqName = qm.DeadLetterQueueName.Trim(); |
| 105 | + using var dlq = qm.AccessQueue(dlqName, MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING); |
| 106 | + var gmo = new MQGetMessageOptions { WaitInterval = 0, Options = MQC.MQGMO_NO_WAIT }; |
| 107 | + while (true) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + dlq.Get(new MQMessage(), gmo); |
| 112 | + } |
| 113 | + catch (MQException e) when (e.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) |
| 114 | + { |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + static (string queueManagerName, Hashtable properties) ParseConnectionString() => |
| 121 | + ConnectionProperties.Parse(ConnectionString); |
| 122 | + |
| 123 | + static readonly string ConnectionString = |
| 124 | + Environment.GetEnvironmentVariable("ServiceControl_TransportTests_IBMMQ_ConnectionString") |
| 125 | + ?? Environment.GetEnvironmentVariable("SERVICECONTROL_TRANSPORTTESTS_IBMMQ_CONNECTIONSTRING") |
| 126 | + ?? "mq://admin:passw0rd@localhost:1414"; |
| 127 | +} |
0 commit comments