|
| 1 | +using System.Collections.Concurrent; |
| 2 | +using ByteBard.GUSTO; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | + |
| 7 | +public class ServiceProviderScopeTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public async Task StorageProvider_UsedConcurrently_ThrowsDbContextConcurrencyError() |
| 11 | + { |
| 12 | + // This test demonstrates a bug with scoped services (particular dbcontext usage): when multiple jobs run in parallel, |
| 13 | + // they share the same storage provider instance (and its scoped DbContext), |
| 14 | + // causing EF Core to throw a concurrency exception |
| 15 | + |
| 16 | + FakeDbContext.SeenHashes.Clear(); |
| 17 | + FakeDbContext.DetectedConcurrentAccess = false; |
| 18 | + FakeJobService.Completed.Clear(); |
| 19 | + |
| 20 | + var services = new ServiceCollection(); |
| 21 | + var inMemorySettings = new Dictionary<string, string?> |
| 22 | + { |
| 23 | + ["Gusto:BatchSize"] = "5", |
| 24 | + ["Gusto:Concurrency"] = "5", |
| 25 | + ["Gusto:PollInterval"] = "00:00:01" |
| 26 | + }; |
| 27 | + |
| 28 | + IConfiguration configuration = new ConfigurationBuilder() |
| 29 | + .AddInMemoryCollection(inMemorySettings) |
| 30 | + .Build(); |
| 31 | + services.AddLogging(); |
| 32 | + services.AddGusto<FakeJobRecord, InMemoryJobStorageProvider>(configuration); |
| 33 | + |
| 34 | + services.AddScoped<IFakeDbContext, FakeDbContext>(); |
| 35 | + services.AddScoped<FakeJobService>(); |
| 36 | + |
| 37 | + var sp = services.BuildServiceProvider(); |
| 38 | + var jobQueue = sp.GetRequiredService<JobQueue<FakeJobRecord>>(); |
| 39 | + var worker = sp.GetRequiredService<IHostedService>(); |
| 40 | + |
| 41 | + // Arrange barriers |
| 42 | + JobQueueWorker<FakeJobRecord>.BatchStartBarrier = new TaskCompletionSource(); |
| 43 | + JobQueueWorker<FakeJobRecord>.BatchCompletedBarrier = new TaskCompletionSource(); |
| 44 | + |
| 45 | + for (int i = 0; i < 5; i++) |
| 46 | + { |
| 47 | + await jobQueue.EnqueueAsync<FakeJobService>( |
| 48 | + svc => svc.RunAsync($"job-{i}")); |
| 49 | + } |
| 50 | + |
| 51 | + // Act |
| 52 | + await worker.StartAsync(CancellationToken.None); |
| 53 | + JobQueueWorker<FakeJobRecord>.BatchStartBarrier.SetResult(); |
| 54 | + await JobQueueWorker<FakeJobRecord>.BatchCompletedBarrier.Task; |
| 55 | + await worker.StopAsync(CancellationToken.None); |
| 56 | + |
| 57 | + Assert.False(FakeDbContext.DetectedConcurrentAccess, |
| 58 | + "Concurrent DbContext access was detected! " + |
| 59 | + "Multiple parallel jobs are sharing the same storage provider instance " + |
| 60 | + "(with the same scoped DbContext). Each job should get its own scope."); |
| 61 | + } |
| 62 | + |
| 63 | +} |
| 64 | +public interface IFakeDbContext |
| 65 | +{ |
| 66 | + Task DoWorkAsync(string jobId); |
| 67 | +} |
| 68 | + |
| 69 | +public class FakeDbContext : IFakeDbContext |
| 70 | +{ |
| 71 | + public static ConcurrentBag<int> SeenHashes = new(); |
| 72 | + public static volatile bool DetectedConcurrentAccess = false; |
| 73 | + |
| 74 | + private int _inUse; |
| 75 | + |
| 76 | + public async Task DoWorkAsync(string jobId) |
| 77 | + { |
| 78 | + SeenHashes.Add(GetHashCode()); |
| 79 | + if (Interlocked.Exchange(ref _inUse, 1) == 1) |
| 80 | + { |
| 81 | + DetectedConcurrentAccess = true; |
| 82 | + throw new InvalidOperationException( |
| 83 | + $"A second operation was started on this context instance before a previous operation completed. " + |
| 84 | + $"DbContext {GetHashCode()} already in use! This simulates EF Core's concurrency detection."); |
| 85 | + } |
| 86 | + |
| 87 | + try { await Task.Delay(50); } |
| 88 | + finally { Interlocked.Exchange(ref _inUse, 0); } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +public class FakeJobService |
| 94 | +{ |
| 95 | + private readonly IFakeDbContext _db; |
| 96 | + public static ConcurrentBag<string> Completed = new(); |
| 97 | + |
| 98 | + public FakeJobService(IFakeDbContext db) => _db = db; |
| 99 | + |
| 100 | + public async Task RunAsync(string id) |
| 101 | + { |
| 102 | + await _db.DoWorkAsync(id); |
| 103 | + Completed.Add(id); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +public class InMemoryJobStorageProvider : IJobStorageProvider<FakeJobRecord> |
| 108 | +{ |
| 109 | + private static readonly List<FakeJobRecord> _jobs = new(); |
| 110 | + private readonly object _lock = new(); |
| 111 | + private readonly IFakeDbContext _dbContext; |
| 112 | + |
| 113 | + public InMemoryJobStorageProvider(IFakeDbContext dbContext) |
| 114 | + { |
| 115 | + _dbContext = dbContext; |
| 116 | + } |
| 117 | + |
| 118 | + public Task StoreJobAsync(FakeJobRecord jobStorageRecord, CancellationToken cancellationToken) |
| 119 | + { |
| 120 | + lock (_lock) |
| 121 | + { |
| 122 | + _jobs.Add(jobStorageRecord); |
| 123 | + } |
| 124 | + return Task.CompletedTask; |
| 125 | + } |
| 126 | + |
| 127 | + public Task<IEnumerable<FakeJobRecord>> GetBatchAsync(JobSearchParams<FakeJobRecord> parameters, CancellationToken cancellationToken) |
| 128 | + { |
| 129 | + lock (_lock) |
| 130 | + { |
| 131 | + var results = _jobs.Where(j => j.Status == JobStatus.Ready).Where(parameters.Match.Compile()).Take(parameters.Limit); |
| 132 | + return Task.FromResult(results); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public async Task MarkJobAsCompleteAsync(FakeJobRecord jobStorageRecord, CancellationToken cancellationToken) |
| 137 | + { |
| 138 | + // Simulate async database operation like EF Core would do |
| 139 | + await _dbContext.DoWorkAsync($"MarkComplete-{jobStorageRecord.TrackingId}"); |
| 140 | + |
| 141 | + lock (_lock) |
| 142 | + { |
| 143 | + var job = _jobs.FirstOrDefault(j => j.TrackingId == jobStorageRecord.TrackingId); |
| 144 | + if (job != null) |
| 145 | + { |
| 146 | + job.IsComplete = true; |
| 147 | + |
| 148 | + ProcessContinuations(job.TrackingId, successful: true); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public Task CancelJobAsync(Guid trackingId, CancellationToken cancellationToken) |
| 154 | + { |
| 155 | + lock (_lock) |
| 156 | + { |
| 157 | + _jobs.RemoveAll(j => j.TrackingId == trackingId); |
| 158 | + } |
| 159 | + return Task.CompletedTask; |
| 160 | + } |
| 161 | + |
| 162 | + public async Task OnHandlerExecutionFailureAsync(FakeJobRecord jobStorageRecord, Exception exception, CancellationToken cancellationToken) |
| 163 | + { |
| 164 | + // Simulate async database operation like EF Core would do |
| 165 | + await _dbContext.DoWorkAsync($"HandleFailure-{jobStorageRecord.TrackingId}"); |
| 166 | + |
| 167 | + lock (_lock) |
| 168 | + { |
| 169 | + jobStorageRecord.ExecuteAfter = DateTime.UtcNow.AddMinutes(5); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private void ProcessContinuations(Guid completedJobId, bool successful) |
| 174 | + { |
| 175 | + var continuations = _jobs.Where(j => |
| 176 | + j.Status == JobStatus.WaitingForParent && |
| 177 | + j.ParentJobId == completedJobId).ToList(); |
| 178 | + |
| 179 | + foreach (var continuation in continuations) |
| 180 | + { |
| 181 | + if (successful) |
| 182 | + { |
| 183 | + continuation.Status = JobStatus.Ready; |
| 184 | + continuation.ExecuteAfter = DateTime.UtcNow; |
| 185 | + |
| 186 | + // don't forget to store the change if moving to persisted storage. |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +public class FakeJobRecord : IJobStorageRecord |
| 193 | +{ |
| 194 | + public Guid TrackingId { get; set; } |
| 195 | + public DateTime CreatedOn { get; set; } |
| 196 | + public DateTime? ExecuteAfter { get; set; } |
| 197 | + public DateTime? ExpireOn { get; set; } |
| 198 | + public bool IsComplete { get; set; } |
| 199 | + public string JobType { get; set; } |
| 200 | + public string MethodName { get; set; } |
| 201 | + public string ArgumentsJson { get; set; } |
| 202 | + public JobStatus Status { get; set; } |
| 203 | + public Guid ParentJobId { get; set; } |
| 204 | +} |
| 205 | + |
| 206 | +public enum JobStatus |
| 207 | +{ |
| 208 | + Ready, |
| 209 | + WaitingForParent, |
| 210 | +} |
0 commit comments