|
| 1 | +// --------------------------------------------------------------------- |
| 2 | +//This file is part of DotNetWorkQueue |
| 3 | +//Copyright © 2015-2026 Brian Lehnen |
| 4 | +// |
| 5 | +//This library is free software; you can redistribute it and/or |
| 6 | +//modify it under the terms of the GNU Lesser General Public |
| 7 | +//License as published by the Free Software Foundation; either |
| 8 | +//version 2.1 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +//This library is distributed in the hope that it will be useful, |
| 11 | +//but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +//Lesser General Public License for more details. |
| 14 | +// |
| 15 | +//You should have received a copy of the GNU Lesser General Public |
| 16 | +//License along with this library; if not, write to the Free Software |
| 17 | +//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +// --------------------------------------------------------------------- |
| 19 | +using System; |
| 20 | +using System.Threading; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using DotNetWorkQueue.Configuration; |
| 23 | +using DotNetWorkQueue.IntegrationTests.Shared; |
| 24 | +using DotNetWorkQueue.Transport.PostgreSQL.Basic; |
| 25 | +using DotNetWorkQueue.Transport.RelationalDatabase; |
| 26 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 27 | + |
| 28 | +namespace DotNetWorkQueue.Transport.PostgreSQL.Integration.Tests.Inbox |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Async inbox-pattern integration tests for PostgreSQL. |
| 32 | + /// </summary> |
| 33 | + [TestClass] |
| 34 | + public class PostgreSqlInboxAsyncHandlerTests : PostgreSqlInboxIntegrationTestBase |
| 35 | + { |
| 36 | + [ClassInitialize] |
| 37 | + public static void Init(TestContext _) => EnsureActivityListenerRegistered(); |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Async_Commit_BothRowsVisible() |
| 41 | + { |
| 42 | + var connStr = ConnectionInfo.ConnectionString; |
| 43 | + var qc = new QueueConnection(NewQueueName(), connStr); |
| 44 | + var businessTable = NewBusinessTableName(); |
| 45 | + |
| 46 | + using var queue = CreateQueue(qc, enableHoldTransaction: true); |
| 47 | + CreateBusinessTable(connStr, businessTable); |
| 48 | + try |
| 49 | + { |
| 50 | + var handlerInvoked = new ManualResetEventSlim(false); |
| 51 | + Exception capturedException = null; |
| 52 | + var castSucceeded = false; |
| 53 | + |
| 54 | + using (var queueContainer = new QueueContainer<PostgreSqlMessageQueueInit>()) |
| 55 | + { |
| 56 | + using (var producer = queueContainer.CreateProducer<FakeMessage>(qc)) |
| 57 | + { |
| 58 | + var sendResult = producer.Send(new FakeMessage()); |
| 59 | + Assert.IsFalse(sendResult.HasError, sendResult.SendingException?.ToString()); |
| 60 | + } |
| 61 | + |
| 62 | + using (var consumer = queueContainer.CreateConsumerAsync(qc)) |
| 63 | + { |
| 64 | + consumer.Configuration.Worker.WorkerCount = 1; |
| 65 | + consumer.Start<FakeMessage>((message, workerNotification) => |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + if (workerNotification is IRelationalWorkerNotification relational) |
| 70 | + { |
| 71 | + castSucceeded = true; |
| 72 | + InsertBusinessRowOnInboxTransaction(relational.Transaction, businessTable, 1, "commit-async"); |
| 73 | + } |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + capturedException = ex; |
| 78 | + throw; |
| 79 | + } |
| 80 | + finally |
| 81 | + { |
| 82 | + handlerInvoked.Set(); |
| 83 | + } |
| 84 | + return Task.CompletedTask; |
| 85 | + }, null); |
| 86 | + |
| 87 | + Assert.IsTrue(handlerInvoked.Wait(TimeSpan.FromSeconds(30)), "async handler was not invoked within 30s"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Assert.IsNull(capturedException, $"async handler threw unexpectedly: {capturedException}"); |
| 92 | + Assert.IsTrue(castSucceeded, "capability cast to IRelationalWorkerNotification failed when option=true"); |
| 93 | + AssertBusinessRowCountFromSeparateConnection(connStr, businessTable, 1); |
| 94 | + } |
| 95 | + finally |
| 96 | + { |
| 97 | + DropBusinessTable(connStr, businessTable); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void Async_Rollback_NeitherRowVisible() |
| 103 | + { |
| 104 | + var connStr = ConnectionInfo.ConnectionString; |
| 105 | + var qc = new QueueConnection(NewQueueName(), connStr); |
| 106 | + var businessTable = NewBusinessTableName(); |
| 107 | + |
| 108 | + using var queue = CreateQueue(qc, enableHoldTransaction: true); |
| 109 | + CreateBusinessTable(connStr, businessTable); |
| 110 | + try |
| 111 | + { |
| 112 | + var handlerInvoked = new ManualResetEventSlim(false); |
| 113 | + var castSucceeded = false; |
| 114 | + |
| 115 | + using (var queueContainer = new QueueContainer<PostgreSqlMessageQueueInit>()) |
| 116 | + { |
| 117 | + using (var producer = queueContainer.CreateProducer<FakeMessage>(qc)) |
| 118 | + { |
| 119 | + var sendResult = producer.Send(new FakeMessage()); |
| 120 | + Assert.IsFalse(sendResult.HasError, sendResult.SendingException?.ToString()); |
| 121 | + } |
| 122 | + |
| 123 | + using (var consumer = queueContainer.CreateConsumerAsync(qc)) |
| 124 | + { |
| 125 | + consumer.Configuration.Worker.WorkerCount = 1; |
| 126 | + consumer.Configuration.TransportConfiguration.RetryDelayBehavior.Add( |
| 127 | + typeof(InvalidOperationException), |
| 128 | + new System.Collections.Generic.List<TimeSpan> |
| 129 | + { |
| 130 | + TimeSpan.FromMilliseconds(100) |
| 131 | + }); |
| 132 | + |
| 133 | + consumer.Start<FakeMessage>((message, workerNotification) => |
| 134 | + { |
| 135 | + if (workerNotification is IRelationalWorkerNotification relational) |
| 136 | + { |
| 137 | + castSucceeded = true; |
| 138 | + InsertBusinessRowOnInboxTransaction(relational.Transaction, businessTable, 1, "rollback-async"); |
| 139 | + } |
| 140 | + handlerInvoked.Set(); |
| 141 | + throw new InvalidOperationException("intentional throw — should roll back inbox transaction"); |
| 142 | + }, null); |
| 143 | + |
| 144 | + Assert.IsTrue(handlerInvoked.Wait(TimeSpan.FromSeconds(30)), "async handler was not invoked within 30s"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + Assert.IsTrue(castSucceeded, "capability cast to IRelationalWorkerNotification failed when option=true"); |
| 149 | + AssertBusinessRowCountStaysAt(connStr, businessTable, 0); |
| 150 | + } |
| 151 | + finally |
| 152 | + { |
| 153 | + DropBusinessTable(connStr, businessTable); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments