Browse Source

Make the tests more stable.

pull/967/head
Sebastian 4 years ago
parent
commit
701215f892
  1. 8
      backend/src/Squidex.Infrastructure/Log/BackgroundRequestLogStore.cs
  2. 23
      backend/src/Squidex.Infrastructure/UsageTracking/BackgroundUsageTracker.cs
  3. 4
      backend/tests/Squidex.Infrastructure.Tests/Log/BackgroundRequestLogStoreTests.cs
  4. 4
      backend/tests/Squidex.Infrastructure.Tests/UsageTracking/BackgroundUsageTrackerTests.cs

8
backend/src/Squidex.Infrastructure/Log/BackgroundRequestLogStore.cs

@ -19,8 +19,9 @@ public sealed class BackgroundRequestLogStore : DisposableObjectBase, IRequestLo
private readonly CompletionTimer logTimer; private readonly CompletionTimer logTimer;
private readonly RequestLogStoreOptions options; private readonly RequestLogStoreOptions options;
private readonly ConcurrentQueue<Request> jobs = new ConcurrentQueue<Request>(); private readonly ConcurrentQueue<Request> jobs = new ConcurrentQueue<Request>();
private bool isUpdating;
public int PendingJobs => jobs.Count; public bool HasPendingJobs => !jobs.IsEmpty || isUpdating;
public bool IsEnabled => options.StoreEnabled; public bool IsEnabled => options.StoreEnabled;
@ -63,6 +64,7 @@ public sealed class BackgroundRequestLogStore : DisposableObjectBase, IRequestLo
return; return;
} }
isUpdating = true;
try try
{ {
var batch = new List<Request>(options.BatchSize); var batch = new List<Request>(options.BatchSize);
@ -87,6 +89,10 @@ public sealed class BackgroundRequestLogStore : DisposableObjectBase, IRequestLo
{ {
log.LogError(ex, "Failed to track usage in background."); log.LogError(ex, "Failed to track usage in background.");
} }
finally
{
isUpdating = false;
}
} }
public Task DeleteAsync(string key, public Task DeleteAsync(string key,

23
backend/src/Squidex.Infrastructure/UsageTracking/BackgroundUsageTracker.cs

@ -18,8 +18,9 @@ public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker
private readonly ILogger<BackgroundUsageTracker> log; private readonly ILogger<BackgroundUsageTracker> log;
private readonly CompletionTimer usageTimer; private readonly CompletionTimer usageTimer;
private ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters> jobs = new ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters>(); private ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters> jobs = new ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters>();
private bool isUpdating;
public int PendingJobs => jobs.Count; public bool HasPendingJobs => !jobs.IsEmpty || isUpdating;
public string FallbackCategory => "*"; public string FallbackCategory => "*";
@ -52,35 +53,41 @@ public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker
{ {
try try
{ {
isUpdating = true;
var localUsages = Interlocked.Exchange(ref jobs, new ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters>()); var localUsages = Interlocked.Exchange(ref jobs, new ConcurrentDictionary<(string Key, string Category, DateTime Date), Counters>());
if (!localUsages.IsEmpty) if (!localUsages.IsEmpty)
{ {
var updates = new UsageUpdate[localUsages.Count]; var updateBatch = new UsageUpdate[localUsages.Count];
var updateIndex = 0; var updateIndex = 0;
foreach (var (key, value) in localUsages) foreach (var (key, value) in localUsages)
{ {
if (updateIndex >= updates.Length) if (updateIndex >= updateBatch.Length)
{ {
break; break;
} }
updates[updateIndex].Key = key.Key; updateBatch[updateIndex].Key = key.Key;
updates[updateIndex].Category = key.Category; updateBatch[updateIndex].Category = key.Category;
updates[updateIndex].Counters = value; updateBatch[updateIndex].Counters = value;
updates[updateIndex].Date = key.Date; updateBatch[updateIndex].Date = key.Date;
updateIndex++; updateIndex++;
} }
await usageRepository.TrackUsagesAsync(updates, ct); await usageRepository.TrackUsagesAsync(updateBatch, ct);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
log.LogError(ex, "Failed to track usage in background."); log.LogError(ex, "Failed to track usage in background.");
} }
finally
{
isUpdating = false;
}
} }
public Task DeleteAsync(string key, public Task DeleteAsync(string key,

4
backend/tests/Squidex.Infrastructure.Tests/Log/BackgroundRequestLogStoreTests.cs

@ -121,12 +121,14 @@ public class BackgroundRequestLogStoreTests
using var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5000)); using var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5000));
while (sut.PendingJobs > 0) while (sut.HasPendingJobs)
{ {
tcs.Token.ThrowIfCancellationRequested(); tcs.Token.ThrowIfCancellationRequested();
await Task.Delay(20, tcs.Token); await Task.Delay(20, tcs.Token);
} }
sut.Dispose();
} }
private static IEnumerable<Request> Batch(string from, string to) private static IEnumerable<Request> Batch(string from, string to)

4
backend/tests/Squidex.Infrastructure.Tests/UsageTracking/BackgroundUsageTrackerTests.cs

@ -249,12 +249,14 @@ public class BackgroundUsageTrackerTests
using var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5)); using var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5));
while (sut.PendingJobs > 0) while (sut.HasPendingJobs)
{ {
tcs.Token.ThrowIfCancellationRequested(); tcs.Token.ThrowIfCancellationRequested();
await Task.Delay(20, tcs.Token); await Task.Delay(20, tcs.Token);
} }
sut.Dispose();
} }
private static Counters Counters(double? a = null, double? b = null) private static Counters Counters(double? a = null, double? b = null)

Loading…
Cancel
Save