|
| 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.Data.SQLite; |
| 21 | +using DotNetWorkQueue.Configuration; |
| 22 | +using DotNetWorkQueue.IntegrationTests.Shared; |
| 23 | +using DotNetWorkQueue.Messages; |
| 24 | +using DotNetWorkQueue.Transport.RelationalDatabase.Basic; |
| 25 | +using DotNetWorkQueue.Transport.SQLite.Basic; |
| 26 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 27 | + |
| 28 | +namespace DotNetWorkQueue.Transport.SQLite.Integration.Tests.Outbox |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Integration tests proving <see cref="AdditionalMessageData"/> survives the |
| 32 | + /// caller-transaction Send path on SQLite. Verification uses direct SQL queries against |
| 33 | + /// the queue's MetaData table rather than a live consumer dequeue, avoiding consumer |
| 34 | + /// lifecycle complexity. |
| 35 | + /// |
| 36 | + /// Assertions: |
| 37 | + /// 1. Exactly 1 metadata row after commit (write confirmation). |
| 38 | + /// 2. The CorrelationID persisted in the MetaData table matches the Guid returned by |
| 39 | + /// <c>sendResult.SentMessage.CorrelationId.Id.Value</c>. Auto-assigned by |
| 40 | + /// <c>GenerateMessageHeaders.HeaderSetup</c>; the persisted value must equal what |
| 41 | + /// the producer returned to the caller. |
| 42 | + /// 3. The Priority column persists the caller-supplied value — the regression guard |
| 43 | + /// catches a silent drop of <c>IAdditionalMessageData</c> on the external-transaction |
| 44 | + /// path (ISSUE-037). |
| 45 | + /// </summary> |
| 46 | + [TestClass] |
| 47 | + public class SqliteOutboxAdditionalDataTests : SqliteOutboxIntegrationTestBase |
| 48 | + { |
| 49 | + [ClassInitialize] |
| 50 | + public static void Init(TestContext _) => EnsureActivityListenerRegistered(); |
| 51 | + |
| 52 | + private const byte ExpectedPriority = 7; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Verifies that CorrelationID and Priority both round-trip through the external-transaction |
| 56 | + /// Send path to the MetaData table. Runs for both in-memory and file-based SQLite to confirm |
| 57 | + /// the outbox contract holds regardless of connection mode. |
| 58 | + /// </summary> |
| 59 | + [TestMethod] |
| 60 | + [DataRow(true)] |
| 61 | + [DataRow(false)] |
| 62 | + public void AdditionalMessageData_RoundTrip_PreservesHeadersAndCorrelation(bool inMemory) |
| 63 | + { |
| 64 | + using var connInfo = new IntegrationConnectionInfo(inMemory); |
| 65 | + var qc = new QueueConnection(NewQueueName(), connInfo.ConnectionString); |
| 66 | + using var queue = CreateQueue(qc, enablePriority: true); |
| 67 | + using var producer = CreateRelationalProducer(qc); |
| 68 | + |
| 69 | + var data = new AdditionalMessageData(); |
| 70 | + // WHY: Set priority explicitly. Unlike CorrelationID (auto-assigned by |
| 71 | + // GenerateMessageHeaders.HeaderSetup), Priority has no fallback in the |
| 72 | + // producer path — asserting its persisted value catches a regression where |
| 73 | + // the producer silently drops AdditionalMessageData on the external-transaction |
| 74 | + // path (ISSUE-037). |
| 75 | + data.SetPriority(ExpectedPriority); |
| 76 | + var msg = GenerateMessage.Create<FakeMessage>(); |
| 77 | + |
| 78 | + IQueueOutputMessage sendResult; |
| 79 | + using var conn = new SQLiteConnection(connInfo.ConnectionString); |
| 80 | + conn.Open(); |
| 81 | + using (var transaction = conn.BeginTransaction()) |
| 82 | + { |
| 83 | + sendResult = producer.RelationalProducer.Send(msg, data, transaction); |
| 84 | + Assert.IsFalse(sendResult.HasError, sendResult.SendingException?.ToString()); |
| 85 | + transaction.Commit(); |
| 86 | + } |
| 87 | + |
| 88 | + // Sanity: exactly one metadata row was committed. |
| 89 | + AssertQueueRowCount(qc, 1); |
| 90 | + |
| 91 | + // Retrieve the CorrelationID that the producer returned to the caller. |
| 92 | + var returnedCorrelationGuid = (Guid)sendResult.SentMessage.CorrelationId.Id.Value; |
| 93 | + |
| 94 | + // Query the MetaData table directly and assert the persisted GUID matches. |
| 95 | + AssertCorrelationIdInMetadata(qc, returnedCorrelationGuid); |
| 96 | + |
| 97 | + // Priority round-trip: the regression guard described above. |
| 98 | + AssertPriorityInMetadata(qc, ExpectedPriority); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Queries the queue's MetaData table for a single CorrelationID row and asserts it |
| 103 | + /// equals <paramref name="expected"/>. Opens a fresh connection to isolate the read |
| 104 | + /// from the producer's commit visibility. |
| 105 | + /// </summary> |
| 106 | + private static void AssertCorrelationIdInMetadata(QueueConnection queueConnection, Guid expected) |
| 107 | + { |
| 108 | + var info = new SqliteConnectionInformation(queueConnection, new DbDataSource()); |
| 109 | + var helper = new TableNameHelper(info); |
| 110 | + // WHY: Fresh connection per assertion — isolates the read from the producer |
| 111 | + // connection and absorbs any SQLite commit-visibility edge cases. |
| 112 | + using var conn = new SQLiteConnection(queueConnection.Connection); |
| 113 | + conn.Open(); |
| 114 | + using var cmd = conn.CreateCommand(); |
| 115 | + cmd.CommandText = $"SELECT CorrelationID FROM {helper.MetaDataName} LIMIT 1"; |
| 116 | + var raw = cmd.ExecuteScalar(); |
| 117 | + // WHY: SQLite stores CorrelationID via DbType.StringFixedLength size 38 |
| 118 | + // (SendMessage.cs lines 124, 182). Read-back is a string; Guid.Parse |
| 119 | + // tolerates both {B} and D formats produced by the SQLite driver. |
| 120 | + var actual = Guid.Parse((string)raw!); |
| 121 | + Assert.AreEqual(expected, actual, |
| 122 | + $"CorrelationID did not round-trip: expected {expected}, persisted {actual}."); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Queries the queue's MetaData table for the Priority column and asserts the single |
| 127 | + /// persisted row equals <paramref name="expected"/>. Opens a fresh connection to isolate |
| 128 | + /// the read from the producer's commit visibility. |
| 129 | + /// </summary> |
| 130 | + private static void AssertPriorityInMetadata(QueueConnection queueConnection, byte expected) |
| 131 | + { |
| 132 | + var info = new SqliteConnectionInformation(queueConnection, new DbDataSource()); |
| 133 | + var helper = new TableNameHelper(info); |
| 134 | + // WHY: Fresh connection per assertion — same isolation rationale as AssertCorrelationIdInMetadata. |
| 135 | + using var conn = new SQLiteConnection(queueConnection.Connection); |
| 136 | + conn.Open(); |
| 137 | + using var cmd = conn.CreateCommand(); |
| 138 | + cmd.CommandText = $"SELECT Priority FROM {helper.MetaDataName} LIMIT 1"; |
| 139 | + var raw = cmd.ExecuteScalar(); |
| 140 | + // WHY: SQLite INTEGER affinity surfaces from ExecuteScalar as long; |
| 141 | + // Convert.ToByte handles the narrowing safely (same idiom as PG test). |
| 142 | + var actual = Convert.ToByte(raw); |
| 143 | + Assert.AreEqual(expected, actual, |
| 144 | + $"Priority did not round-trip: expected {expected}, persisted {actual}."); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments