|
| 1 | +using System.Threading.Tasks; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using SW.Bitween.Domain; |
| 4 | +using SW.Bitween.IntegrationTests.Fixtures; |
| 5 | +using SW.Bitween.Model; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace SW.Bitween.IntegrationTests.Tests; |
| 9 | + |
| 10 | +// The run flag is written with raw (parameterized) SQL that differs per database |
| 11 | +// provider, so it needs a real round trip to prove the statement and its parameter |
| 12 | +// binding are correct. There was no coverage here before. |
| 13 | +[Collection("Bitween")] |
| 14 | +public class RunFlagUpdaterTests |
| 15 | +{ |
| 16 | + private readonly BitweenFixture _fixture; |
| 17 | + |
| 18 | + public RunFlagUpdaterTests(BitweenFixture fixture) |
| 19 | + { |
| 20 | + _fixture = fixture; |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task Run_flag_claims_once_then_blocks_until_idle() |
| 25 | + { |
| 26 | + await using var scope = _fixture.CreateScope(); |
| 27 | + var db = scope.ServiceProvider.GetRequiredService<BitweenDbContext>(); |
| 28 | + var runFlag = scope.ServiceProvider.GetRequiredService<RunFlagUpdater>(); |
| 29 | + |
| 30 | + var document = new Document(6101, "Run Flag Test Doc"); |
| 31 | + db.Set<Document>().Add(document); |
| 32 | + var subscription = new Subscription("Run Flag Test", document.Id); |
| 33 | + subscription.Inactive = false; |
| 34 | + db.Set<Subscription>().Add(subscription); |
| 35 | + await db.SaveChangesAsync(); |
| 36 | + |
| 37 | + Assert.True(await runFlag.MarkAsRunning(subscription.Id)); // first claim wins |
| 38 | + Assert.False(await runFlag.MarkAsRunning(subscription.Id)); // already running |
| 39 | + |
| 40 | + await runFlag.MarkAsIdle(subscription.Id); |
| 41 | + |
| 42 | + Assert.True(await runFlag.MarkAsRunning(subscription.Id)); // claimable again |
| 43 | + await runFlag.MarkAsIdle(subscription.Id); |
| 44 | + } |
| 45 | + |
| 46 | + // Guards the parameter binding: a broken placeholder would either match no rows |
| 47 | + // or every row, and both would show up here. |
| 48 | + [Fact] |
| 49 | + public async Task Run_flag_only_affects_the_requested_subscription() |
| 50 | + { |
| 51 | + await using var scope = _fixture.CreateScope(); |
| 52 | + var db = scope.ServiceProvider.GetRequiredService<BitweenDbContext>(); |
| 53 | + var runFlag = scope.ServiceProvider.GetRequiredService<RunFlagUpdater>(); |
| 54 | + |
| 55 | + var document = new Document(6102, "Run Flag Isolation Doc"); |
| 56 | + db.Set<Document>().Add(document); |
| 57 | + var a = new Subscription("Run Flag A", document.Id); |
| 58 | + a.Inactive = false; |
| 59 | + var b = new Subscription("Run Flag B", document.Id); |
| 60 | + b.Inactive = false; |
| 61 | + db.Set<Subscription>().AddRange(a, b); |
| 62 | + await db.SaveChangesAsync(); |
| 63 | + |
| 64 | + Assert.True(await runFlag.MarkAsRunning(a.Id)); |
| 65 | + Assert.True(await runFlag.MarkAsRunning(b.Id)); // b untouched by a's update |
| 66 | + |
| 67 | + await runFlag.MarkAsIdle(a.Id); |
| 68 | + Assert.False(await runFlag.MarkAsRunning(b.Id)); // b still running, a's idle did not clear it |
| 69 | + |
| 70 | + await runFlag.MarkAsIdle(b.Id); |
| 71 | + } |
| 72 | +} |
0 commit comments