Skip to content

Commit 03cb110

Browse files
committed
Reduced usages of RefreshDataAsync in tests
1 parent d23417f commit 03cb110

File tree

10 files changed

+57
-77
lines changed

10 files changed

+57
-77
lines changed

tests/Exceptionless.Tests/Controllers/EventControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ await CreateDataAsync(d =>
503503
[Fact]
504504
public async Task WillGetStackEvents()
505505
{
506-
(List<Stack>? stacks, _) = await CreateDataAsync(d =>
506+
(var stacks, _) = await CreateDataAsync(d =>
507507
{
508508
d.Event().TestProject();
509509
});

tests/Exceptionless.Tests/Controllers/ProjectControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public async Task CanGetProjectListStats()
118118
Assert.Equal(0, project.StackCount);
119119
Assert.Equal(0, project.EventCount);
120120

121-
(List<Stack>? stacks, List<PersistentEvent>? events) = await CreateDataAsync(d =>
121+
(var stacks, var events) = await CreateDataAsync(d =>
122122
{
123123
d.Event().Message("test");
124124
});

tests/Exceptionless.Tests/IntegrationTestsBase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ protected virtual void RegisterServices(IServiceCollection services)
125125
await stackRepository.AddAsync(stacks, o => o.ImmediateConsistency());
126126
await eventRepository.AddAsync(events, o => o.ImmediateConsistency());
127127

128-
await RefreshDataAsync();
129-
130128
return (stacks.ToList(), events.ToList());
131129
}
132130

tests/Exceptionless.Tests/Pipeline/EventPipelineTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public async Task CloseExistingAutoSessionAsync()
243243

244244
var contexts = await _pipeline.RunAsync(events, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject());
245245
Assert.DoesNotContain(contexts, c => c.HasError);
246-
Assert.Equal(1, contexts.Count(c => c.IsCancelled && c.IsDiscarded));
246+
Assert.Equal(1, contexts.Count(c => c is { IsCancelled: true, IsDiscarded: true }));
247247
Assert.Contains(contexts, c => c.IsProcessed);
248248

249249
await RefreshDataAsync();
@@ -304,7 +304,7 @@ public async Task WillMarkAutoSessionHeartbeatStackHiddenAsync()
304304

305305
var contexts = await _pipeline.RunAsync(events, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject());
306306
Assert.DoesNotContain(contexts, c => c.HasError);
307-
Assert.Equal(1, contexts.Count(c => c.IsCancelled && c.IsDiscarded));
307+
Assert.Equal(1, contexts.Count(c => c is { IsCancelled: true, IsDiscarded: true }));
308308
Assert.Equal(0, contexts.Count(c => c.IsProcessed));
309309

310310
await RefreshDataAsync();
@@ -459,7 +459,7 @@ public async Task CloseExistingManualSessionAsync()
459459

460460
var contexts = await _pipeline.RunAsync(events, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject());
461461
Assert.DoesNotContain(contexts, c => c.HasError);
462-
Assert.Equal(1, contexts.Count(c => c.IsCancelled && c.IsDiscarded));
462+
Assert.Equal(1, contexts.Count(c => c is { IsCancelled: true, IsDiscarded: true }));
463463
Assert.Contains(contexts, c => c.IsProcessed);
464464

465465
events =
@@ -518,7 +518,7 @@ public async Task WillMarkManualSessionHeartbeatStackHiddenAsync()
518518

519519
var contexts = await _pipeline.RunAsync(events, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject());
520520
Assert.DoesNotContain(contexts, c => c.HasError);
521-
Assert.Equal(1, contexts.Count(c => c.IsCancelled && c.IsDiscarded));
521+
Assert.Equal(1, contexts.Count(c => c is { IsCancelled: true, IsDiscarded: true }));
522522
Assert.Equal(0, contexts.Count(c => c.IsProcessed));
523523

524524
await RefreshDataAsync();
@@ -1164,10 +1164,10 @@ private async Task CreateProjectDataAsync(BillingPlan? plan = null)
11641164
organization.SuspensionDate = SystemClock.UtcNow;
11651165
}
11661166

1167-
await _organizationRepository.AddAsync(organization, o => o.Cache());
1167+
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency().Cache());
11681168
}
11691169

1170-
await _projectRepository.AddAsync(ProjectData.GenerateSampleProjects(), o => o.Cache());
1170+
await _projectRepository.AddAsync(ProjectData.GenerateSampleProjects(), o => o.ImmediateConsistency().Cache());
11711171

11721172
foreach (var user in UserData.GenerateSampleUsers())
11731173
{
@@ -1180,10 +1180,8 @@ private async Task CreateProjectDataAsync(BillingPlan? plan = null)
11801180
if (!user.IsEmailAddressVerified)
11811181
user.CreateVerifyEmailAddressToken();
11821182

1183-
await _userRepository.AddAsync(user, o => o.Cache());
1183+
await _userRepository.AddAsync(user, o => o.ImmediateConsistency().Cache());
11841184
}
1185-
1186-
await RefreshDataAsync();
11871185
}
11881186

11891187
private static PersistentEvent GenerateEvent(DateTimeOffset? occurrenceDate = null, string? userIdentity = null, string? type = null, string? sessionId = null)

tests/Exceptionless.Tests/Repositories/EventRepositoryTests.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public async Task GetNextEventIdInStackTestAsync()
133133

134134
_logger.LogDebug("");
135135
_logger.LogDebug("Tests:");
136-
await RefreshDataAsync();
137136
Assert.Equal(_ids.Count, await _repository.CountAsync());
138137
for (int i = 0; i < sortedIds.Count; i++)
139138
{
@@ -164,9 +163,8 @@ public async Task CanGetPreviousAndNExtEventIdWithFilterTestAsync()
164163
public async Task GetByReferenceIdAsync()
165164
{
166165
string referenceId = ObjectId.GenerateNewId().ToString();
167-
await _repository.AddAsync(EventData.GenerateEvents(3, TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId2, referenceId: referenceId).ToList());
166+
await _repository.AddAsync(EventData.GenerateEvents(3, TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId2, referenceId: referenceId).ToList(), o => o.ImmediateConsistency());
168167

169-
await RefreshDataAsync();
170168
var results = await _repository.GetByReferenceIdAsync(TestConstants.ProjectId, referenceId);
171169
Assert.True(results.Total > 0);
172170
Assert.NotNull(results.Documents.First());
@@ -193,9 +191,8 @@ public async Task GetOpenSessionsAsync()
193191
closedSession
194192
};
195193

196-
await _repository.AddAsync(events);
194+
await _repository.AddAsync(events, o => o.ImmediateConsistency());
197195

198-
await RefreshDataAsync();
199196
var results = await _repository.GetOpenSessionsAsync(SystemClock.UtcNow.SubtractMinutes(30));
200197
Assert.Equal(3, results.Total);
201198
}
@@ -204,13 +201,12 @@ public async Task GetOpenSessionsAsync()
204201
public async Task RemoveAllByClientIpAndDateAsync()
205202
{
206203
const string _clientIpAddress = "123.123.12.255";
207-
208204
const int NUMBER_OF_EVENTS_TO_CREATE = 50;
205+
209206
var events = EventData.GenerateEvents(NUMBER_OF_EVENTS_TO_CREATE, TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId2, startDate: SystemClock.UtcNow.SubtractDays(2), endDate: SystemClock.UtcNow).ToList();
210207
events.ForEach(e => e.AddRequestInfo(new RequestInfo { ClientIpAddress = _clientIpAddress }));
211-
await _repository.AddAsync(events);
208+
await _repository.AddAsync(events, o => o.ImmediateConsistency());
212209

213-
await RefreshDataAsync();
214210
events = (await _repository.GetByProjectIdAsync(TestConstants.ProjectId, o => o.PageLimit(NUMBER_OF_EVENTS_TO_CREATE))).Documents.ToList();
215211
Assert.Equal(NUMBER_OF_EVENTS_TO_CREATE, events.Count);
216212
events.ForEach(e =>
@@ -220,9 +216,8 @@ public async Task RemoveAllByClientIpAndDateAsync()
220216
Assert.Equal(_clientIpAddress, ri.ClientIpAddress);
221217
});
222218

223-
await _repository.RemoveAllAsync(TestConstants.OrganizationId, _clientIpAddress, SystemClock.UtcNow.SubtractDays(3), SystemClock.UtcNow.AddDays(2));
219+
await _repository.RemoveAllAsync(TestConstants.OrganizationId, _clientIpAddress, SystemClock.UtcNow.SubtractDays(3), SystemClock.UtcNow.AddDays(2), o => o.ImmediateConsistency());
224220

225-
await RefreshDataAsync();
226221
events = (await _repository.GetByProjectIdAsync(TestConstants.ProjectId, o => o.PageLimit(NUMBER_OF_EVENTS_TO_CREATE))).Documents.ToList();
227222
Assert.Empty(events);
228223
}
@@ -236,7 +231,7 @@ private async Task CreateDataAsync()
236231
var occurrenceDateMid = baseDate;
237232
var occurrenceDateEnd = baseDate.AddMinutes(30);
238233

239-
await _stackRepository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, organizationId: TestConstants.OrganizationId, projectId: TestConstants.ProjectId));
234+
await _stackRepository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, organizationId: TestConstants.OrganizationId, projectId: TestConstants.ProjectId), o => o.ImmediateConsistency());
240235

241236
var occurrenceDates = new List<DateTime> {
242237
occurrenceDateStart,
@@ -255,10 +250,8 @@ private async Task CreateDataAsync()
255250

256251
foreach (var date in occurrenceDates)
257252
{
258-
var ev = await _repository.AddAsync(EventData.GenerateEvent(projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId, stackId: TestConstants.StackId, occurrenceDate: date));
253+
var ev = await _repository.AddAsync(EventData.GenerateEvent(projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId, stackId: TestConstants.StackId, occurrenceDate: date), o => o.ImmediateConsistency());
259254
_ids.Add(Tuple.Create(ev.Id, date));
260255
}
261-
262-
await RefreshDataAsync();
263256
}
264257
}

tests/Exceptionless.Tests/Repositories/OrganizationRepositoryTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ public async Task CanCreateUpdateRemoveAsync()
3131
var organization = new Organization { Name = "Test Organization", PlanId = _plans.FreePlan.Id };
3232
Assert.Null(organization.Id);
3333

34-
await _repository.AddAsync(organization);
35-
await RefreshDataAsync();
34+
await _repository.AddAsync(organization, o => o.ImmediateConsistency());
3635
Assert.NotNull(organization.Id);
3736

3837
organization = await _repository.GetByIdAsync(organization.Id);
3938
Assert.NotNull(organization);
4039

4140
organization.Name = "New organization";
4241
await _repository.SaveAsync(organization);
43-
4442
await _repository.RemoveAsync(organization.Id);
4543
}
4644

@@ -51,8 +49,7 @@ public async Task CanAddAndGetByCachedAsync()
5149
Assert.Null(organization.Id);
5250

5351
Assert.Equal(0, _cache.Count);
54-
await _repository.AddAsync(organization, o => o.Cache());
55-
await RefreshDataAsync();
52+
await _repository.AddAsync(organization, o => o.ImmediateConsistency().Cache());
5653
Assert.NotNull(organization.Id);
5754
Assert.Equal(1, _cache.Count);
5855

@@ -62,8 +59,7 @@ public async Task CanAddAndGetByCachedAsync()
6259
Assert.NotNull(organization.Id);
6360
Assert.Equal(1, _cache.Count);
6461

65-
await _repository.RemoveAllAsync();
66-
await RefreshDataAsync();
62+
await _repository.RemoveAllAsync(o => o.ImmediateConsistency());
6763
Assert.Equal(0, _cache.Count);
6864
}
6965
}

tests/Exceptionless.Tests/Repositories/TokenRepositoryTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ await _repository.AddAsync(new List<Token> {
3636
Assert.Equal(3, (await _repository.GetByProjectIdAsync(TestConstants.ProjectIdWithNoRoles)).Total);
3737

3838
await _repository.RemoveAllByProjectIdAsync(TestConstants.OrganizationId, TestConstants.ProjectId);
39-
4039
await RefreshDataAsync();
40+
4141
Assert.Equal(4, (await _repository.GetByOrganizationIdAsync(TestConstants.OrganizationId)).Total);
4242
Assert.Equal(1, (await _repository.GetByProjectIdAsync(TestConstants.ProjectId)).Total);
4343
Assert.Equal(3, (await _repository.GetByProjectIdAsync(TestConstants.ProjectIdWithNoRoles)).Total);
4444

4545
await _repository.RemoveAllByProjectIdAsync(TestConstants.OrganizationId, TestConstants.ProjectIdWithNoRoles);
46-
4746
await RefreshDataAsync();
47+
4848
Assert.Equal(3, (await _repository.GetByOrganizationIdAsync(TestConstants.OrganizationId)).Total);
4949
Assert.Equal(1, (await _repository.GetByProjectIdAsync(TestConstants.ProjectId)).Total);
5050
Assert.Equal(2, (await _repository.GetByProjectIdAsync(TestConstants.ProjectIdWithNoRoles)).Total);
5151

5252
await _repository.RemoveAllByOrganizationIdAsync(TestConstants.OrganizationId);
53-
5453
await RefreshDataAsync();
54+
5555
Assert.Equal(0, (await _repository.GetByOrganizationIdAsync(TestConstants.OrganizationId)).Total);
5656
Assert.Equal(0, (await _repository.GetByProjectIdAsync(TestConstants.ProjectId)).Total);
5757
Assert.Equal(1, (await _repository.GetByProjectIdAsync(TestConstants.ProjectIdWithNoRoles)).Total);
@@ -68,8 +68,7 @@ await _repository.AddAsync(new List<Token> {
6868
Assert.Equal(1, (await _repository.GetByTypeAndUserIdAsync(TokenType.Access, TestConstants.UserId)).Total);
6969
Assert.Equal(1, (await _repository.GetByTypeAndUserIdAsync(TokenType.Authentication, TestConstants.UserId)).Total);
7070

71-
await _repository.RemoveAllByUserIdAsync(TestConstants.UserId);
72-
await RefreshDataAsync();
71+
await _repository.RemoveAllByUserIdAsync(TestConstants.UserId, o => o.ImmediateConsistency());
7372
Assert.Equal(0, (await _repository.GetByTypeAndUserIdAsync(TokenType.Access, TestConstants.UserId)).Total);
7473
Assert.Equal(0, (await _repository.GetByTypeAndUserIdAsync(TokenType.Authentication, TestConstants.UserId)).Total);
7574
Assert.Equal(1, (await _repository.GetByOrganizationIdAsync(TestConstants.OrganizationId)).Total);

tests/Exceptionless.Tests/Repositories/WebHookRepositoryTests.cs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Exceptionless.Core.Models;
22
using Exceptionless.Core.Repositories;
33
using Exceptionless.Tests.Utility;
4+
using Foundatio.Repositories;
45
using Xunit;
56
using Xunit.Abstractions;
67

@@ -18,34 +19,33 @@ public WebHookRepositoryTests(ITestOutputHelper output, AppWebHostFactory factor
1819
[Fact]
1920
public async Task GetByOrganizationIdOrProjectIdAsync()
2021
{
21-
await _repository.AddAsync(new WebHook
22+
await _repository.AddAsync([new WebHook
2223
{
2324
OrganizationId = TestConstants.OrganizationId,
2425
Url = "http://localhost:40000/test",
2526
EventTypes =
2627
[WebHook.KnownEventTypes.StackPromoted],
2728
Version = WebHook.KnownVersions.Version2
28-
});
29-
await _repository.AddAsync(new WebHook
30-
{
31-
OrganizationId = TestConstants.OrganizationId,
32-
ProjectId = TestConstants.ProjectId,
33-
Url = "http://localhost:40000/test1",
34-
EventTypes =
29+
},
30+
new WebHook
31+
{
32+
OrganizationId = TestConstants.OrganizationId,
33+
ProjectId = TestConstants.ProjectId,
34+
Url = "http://localhost:40000/test1",
35+
EventTypes =
3536
[WebHook.KnownEventTypes.StackPromoted],
36-
Version = WebHook.KnownVersions.Version2
37-
});
38-
await _repository.AddAsync(new WebHook
39-
{
40-
OrganizationId = TestConstants.OrganizationId,
41-
ProjectId = TestConstants.ProjectIdWithNoRoles,
42-
Url = "http://localhost:40000/test1",
43-
EventTypes =
37+
Version = WebHook.KnownVersions.Version2
38+
},
39+
new WebHook
40+
{
41+
OrganizationId = TestConstants.OrganizationId,
42+
ProjectId = TestConstants.ProjectIdWithNoRoles,
43+
Url = "http://localhost:40000/test1",
44+
EventTypes =
4445
[WebHook.KnownEventTypes.StackPromoted],
45-
Version = WebHook.KnownVersions.Version2
46-
});
46+
Version = WebHook.KnownVersions.Version2
47+
}], o => o.ImmediateConsistency());
4748

48-
await RefreshDataAsync();
4949
Assert.Equal(3, (await _repository.GetByOrganizationIdAsync(TestConstants.OrganizationId)).Total);
5050
Assert.Equal(2, (await _repository.GetByOrganizationIdOrProjectIdAsync(TestConstants.OrganizationId, TestConstants.ProjectId)).Total);
5151
Assert.Equal(1, (await _repository.GetByProjectIdAsync(TestConstants.ProjectId)).Total);
@@ -55,26 +55,25 @@ await _repository.AddAsync(new WebHook
5555
[Fact]
5656
public async Task CanSaveWebHookVersionAsync()
5757
{
58-
await _repository.AddAsync(new WebHook
58+
await _repository.AddAsync([new WebHook
5959
{
6060
OrganizationId = TestConstants.OrganizationId,
6161
ProjectId = TestConstants.ProjectId,
6262
Url = "http://localhost:40000/test",
6363
EventTypes =
6464
[WebHook.KnownEventTypes.StackPromoted],
6565
Version = WebHook.KnownVersions.Version1
66-
});
67-
await _repository.AddAsync(new WebHook
68-
{
69-
OrganizationId = TestConstants.OrganizationId,
70-
ProjectId = TestConstants.ProjectIdWithNoRoles,
71-
Url = "http://localhost:40000/test1",
72-
EventTypes =
66+
},
67+
new WebHook
68+
{
69+
OrganizationId = TestConstants.OrganizationId,
70+
ProjectId = TestConstants.ProjectIdWithNoRoles,
71+
Url = "http://localhost:40000/test1",
72+
EventTypes =
7373
[WebHook.KnownEventTypes.StackPromoted],
74-
Version = WebHook.KnownVersions.Version2
75-
});
74+
Version = WebHook.KnownVersions.Version2
75+
}], o => o.ImmediateConsistency());
7676

77-
await RefreshDataAsync();
7877
Assert.Equal(WebHook.KnownVersions.Version1, (await _repository.GetByProjectIdAsync(TestConstants.ProjectId)).Documents.First().Version);
7978
Assert.Equal(WebHook.KnownVersions.Version2, (await _repository.GetByProjectIdAsync(TestConstants.ProjectIdWithNoRoles)).Documents.First().Version);
8079
}

tests/Exceptionless.Tests/Services/StackServiceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public async Task IncrementUsage_OnlyChangeCache()
4343
Assert.True(occurrenceSet.IsNull || !occurrenceSet.HasValue || occurrenceSet.Value.Count == 0);
4444

4545
var firstUtcNow = SystemClock.UtcNow.Floor(TimeSpan.FromMilliseconds(1));
46-
await RefreshDataAsync();
4746
await _stackService.IncrementStackUsageAsync(TestConstants.OrganizationId, TestConstants.ProjectId, stack.Id, firstUtcNow, firstUtcNow, 1);
47+
await RefreshDataAsync();
4848

4949
// Assert stack state has no change after increment usage
5050
stack = await _stackRepository.GetByIdAsync(TestConstants.StackId);
@@ -60,7 +60,6 @@ public async Task IncrementUsage_OnlyChangeCache()
6060
Assert.Single(occurrenceSet.Value);
6161

6262
var secondUtcNow = SystemClock.UtcNow.Floor(TimeSpan.FromMilliseconds(1));
63-
await RefreshDataAsync();
6463
await _stackService.IncrementStackUsageAsync(TestConstants.OrganizationId, TestConstants.ProjectId, stack.Id, secondUtcNow, secondUtcNow, 2);
6564

6665
// Assert state in cache has been changed after increment usage again

tests/Exceptionless.Tests/Stats/AggregationTests.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,14 @@ public async Task CanGetSessionAggregationsAsync()
207207

208208
private async Task CreateDataAsync(int eventCount = 0, bool multipleProjects = true)
209209
{
210-
var orgs = OrganizationData.GenerateSampleOrganizations(_billingManager, _plans);
211-
await _organizationRepository.AddAsync(orgs, o => o.Cache());
210+
var organizations = OrganizationData.GenerateSampleOrganizations(_billingManager, _plans);
211+
await _organizationRepository.AddAsync(organizations, o => o.ImmediateConsistency().Cache());
212212

213213
var projects = ProjectData.GenerateSampleProjects();
214-
await _projectRepository.AddAsync(projects, o => o.Cache());
215-
await RefreshDataAsync();
214+
await _projectRepository.AddAsync(projects, o => o.ImmediateConsistency().Cache());
216215

217216
if (eventCount > 0)
218-
await CreateEventsAsync(eventCount, multipleProjects ? projects.Select(p => p.Id).ToArray() : [TestConstants.ProjectId
219-
]);
217+
await CreateEventsAsync(eventCount, multipleProjects ? projects.Select(p => p.Id).ToArray() : [TestConstants.ProjectId]);
220218
}
221219

222220
private async Task CreateEventsAsync(int eventCount, string[]? projectIds, decimal? value = -1)

0 commit comments

Comments
 (0)