|
| 1 | +namespace ServiceControl.Audit.Persistence.Tests |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Text; |
| 6 | + using System.Threading.Tasks; |
| 7 | + using Microsoft.Data.SqlClient; |
| 8 | + using NUnit.Framework; |
| 9 | + using ServiceControl.SagaAudit; |
| 10 | + |
| 11 | + // SQL Server reuses a cached plan only when the parameter declarations match too, so these prove |
| 12 | + // that an unbounded text column is always declared nvarchar(max), whether its value is null, short or long. |
| 13 | + class IngestionPlanReuseTests : EFPersistenceTestFixture |
| 14 | + { |
| 15 | + [Test] |
| 16 | + public async Task Messages_with_no_body_a_short_body_and_a_long_body_share_one_plan() |
| 17 | + { |
| 18 | + await Ingest(MakeMessage(contentType: "text/plain")); |
| 19 | + await Ingest(MakeMessage(contentType: "text/plain"), Encoding.UTF8.GetBytes(new string('x', 10))); |
| 20 | + await Ingest(MakeMessage(contentType: "text/plain"), Encoding.UTF8.GetBytes(new string('x', 4001))); |
| 21 | + |
| 22 | + var plans = await CachedPlans("AuditMessages"); |
| 23 | + |
| 24 | + Assert.That(plans, Has.Count.EqualTo(1)); |
| 25 | + Assert.That(plans[0].UseCount, Is.EqualTo(3)); |
| 26 | + Assert.That(plans[0].Text, Does.Contain("nvarchar(max)").And.Not.Contain("nvarchar(4000)")); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task Saga_snapshots_with_no_state_a_short_state_and_a_long_state_share_one_plan() |
| 31 | + { |
| 32 | + await RecordSnapshot(state: null); |
| 33 | + await RecordSnapshot(state: new string('x', 10)); |
| 34 | + await RecordSnapshot(state: new string('x', 4001)); |
| 35 | + |
| 36 | + var plans = await CachedPlans("SagaSnapshots"); |
| 37 | + |
| 38 | + Assert.That(plans, Has.Count.EqualTo(1)); |
| 39 | + Assert.That(plans[0].UseCount, Is.EqualTo(3)); |
| 40 | + Assert.That(plans[0].Text, Does.Contain("nvarchar(max)").And.Not.Contain("nvarchar(4000)")); |
| 41 | + } |
| 42 | + |
| 43 | + async Task RecordSnapshot(string state) |
| 44 | + { |
| 45 | + await using var unitOfWork = await StartAuditUnitOfWork(1); |
| 46 | + await unitOfWork.RecordSagaSnapshot(new SagaSnapshot |
| 47 | + { |
| 48 | + SagaId = Guid.NewGuid(), |
| 49 | + SagaType = "Shipping.ShippingPolicy", |
| 50 | + Status = SagaStateChangeStatus.Updated, |
| 51 | + StartTime = TimeSent, |
| 52 | + FinishTime = TimeSent, |
| 53 | + StateAfterChange = state |
| 54 | + }); |
| 55 | + await unitOfWork.Complete(); |
| 56 | + } |
| 57 | + |
| 58 | + async Task<List<(string Text, int UseCount)>> CachedPlans(string table) |
| 59 | + { |
| 60 | + await using var connection = new SqlConnection(configuration.ConnectionString); |
| 61 | + await connection.OpenAsync(); |
| 62 | + |
| 63 | + await using var command = connection.CreateCommand(); |
| 64 | + command.CommandText = """ |
| 65 | + SELECT st.text, cp.usecounts |
| 66 | + FROM sys.dm_exec_cached_plans AS cp |
| 67 | + CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st |
| 68 | + WHERE CHARINDEX(@statement, st.text) > 0 |
| 69 | + """; |
| 70 | + command.Parameters.AddWithValue("@statement", $"INSERT INTO [{configuration.Schema}].[{table}]"); |
| 71 | + |
| 72 | + var plans = new List<(string Text, int UseCount)>(); |
| 73 | + await using var reader = await command.ExecuteReaderAsync(); |
| 74 | + while (await reader.ReadAsync()) |
| 75 | + { |
| 76 | + plans.Add((reader.GetString(0), reader.GetInt32(1))); |
| 77 | + } |
| 78 | + |
| 79 | + return plans; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments