From 5a90de954ee2d975cd038438495dfaa5a8d9d6d7 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 11 Mar 2020 23:11:58 +0800 Subject: Make all integrated tests async. --- Timeline.Tests/Helpers/ResponseAssertions.cs | 8 ++- Timeline.Tests/Helpers/TestApplication.cs | 31 ++++++----- .../IntegratedTests/IntegratedTestBase.cs | 61 +++++++++++++++------- Timeline.Tests/IntegratedTests/TimelineTest.cs | 6 ++- 4 files changed, 71 insertions(+), 35 deletions(-) (limited to 'Timeline.Tests') diff --git a/Timeline.Tests/Helpers/ResponseAssertions.cs b/Timeline.Tests/Helpers/ResponseAssertions.cs index f01a0677..024732f5 100644 --- a/Timeline.Tests/Helpers/ResponseAssertions.cs +++ b/Timeline.Tests/Helpers/ResponseAssertions.cs @@ -33,7 +33,9 @@ namespace Timeline.Tests.Helpers try { - var body = res.Content.ReadAsStringAsync().Result; + var task = res.Content.ReadAsStringAsync(); + task.Wait(); + var body = task.Result; if (body.Length > 40) { body = body[0..40] + " ..."; @@ -83,7 +85,9 @@ namespace Timeline.Tests.Helpers string body; try { - body = Subject.Content.ReadAsStringAsync().Result; + var task = Subject.Content.ReadAsStringAsync(); + task.Wait(); + body = task.Result; } catch (AggregateException e) { diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs index 52c2f2e2..11fe8f87 100644 --- a/Timeline.Tests/Helpers/TestApplication.cs +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -3,29 +3,35 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using System; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using Timeline.Entities; using Timeline.Migrations; +using Xunit; namespace Timeline.Tests.Helpers { - public class TestApplication : IDisposable + public class TestApplication : IAsyncLifetime { - public SqliteConnection DatabaseConnection { get; } + public SqliteConnection DatabaseConnection { get; private set; } - public WebApplicationFactory Factory { get; } + public WebApplicationFactory Factory { get; private set; } - public string WorkDir { get; } + public string WorkDir { get; private set; } public TestApplication(WebApplicationFactory factory) + { + Factory = factory; + } + + public async Task InitializeAsync() { WorkDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(WorkDir); DatabaseConnection = new SqliteConnection("Data Source=:memory:;"); - DatabaseConnection.Open(); + await DatabaseConnection.OpenAsync(); var options = new DbContextOptionsBuilder() .UseSqlite(DatabaseConnection) @@ -33,15 +39,15 @@ namespace Timeline.Tests.Helpers using (var context = new DatabaseContext(options)) { - context.Database.EnsureCreated(); + await context.Database.EnsureCreatedAsync(); context.JwtToken.Add(new JwtTokenEntity { Key = JwtTokenGenerateHelper.GenerateKey() }); - context.SaveChanges(); + await context.SaveChangesAsync(); } - Factory = factory.WithWebHostBuilder(builder => + Factory = Factory.WithWebHostBuilder(builder => { builder.ConfigureAppConfiguration((context, config) => { @@ -60,11 +66,10 @@ namespace Timeline.Tests.Helpers }); } - public void Dispose() + public async Task DisposeAsync() { - DatabaseConnection.Close(); - DatabaseConnection.Dispose(); - + await DatabaseConnection.CloseAsync(); + await DatabaseConnection.DisposeAsync(); Directory.Delete(WorkDir, true); } } diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index 66904629..a4a7638c 100644 --- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -15,12 +15,16 @@ using Xunit; namespace Timeline.Tests.IntegratedTests { - public abstract class IntegratedTestBase : IClassFixture>, IDisposable + public abstract class IntegratedTestBase : IClassFixture>, IAsyncLifetime { protected TestApplication TestApp { get; } protected WebApplicationFactory Factory => TestApp.Factory; + public IReadOnlyList UserInfos { get; private set; } + + private readonly int _userCount; + public IntegratedTestBase(WebApplicationFactory factory) : this(factory, 1) { @@ -31,8 +35,30 @@ namespace Timeline.Tests.IntegratedTests if (userCount < 0) throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative."); - TestApp = new TestApplication(factory); + _userCount = userCount; + TestApp = new TestApplication(factory); + } + + protected virtual Task OnInitializeAsync() + { + return Task.CompletedTask; + } + + protected virtual Task OnDisposeAsync() + { + return Task.CompletedTask; + } + + protected virtual void OnDispose() + { + + } + + public async Task InitializeAsync() + { + await TestApp.InitializeAsync(); + using (var scope = Factory.Services.CreateScope()) { var users = new List() @@ -46,7 +72,7 @@ namespace Timeline.Tests.IntegratedTests } }; - for (int i = 1; i <= userCount; i++) + for (int i = 1; i <= _userCount; i++) { users.Add(new User { @@ -62,10 +88,10 @@ namespace Timeline.Tests.IntegratedTests var userService = scope.ServiceProvider.GetRequiredService(); foreach (var user in users) { - userService.CreateUser(user).Wait(); + await userService.CreateUser(user); } - using var client = CreateDefaultClient().Result; + using var client = await CreateDefaultClient(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase @@ -74,26 +100,23 @@ namespace Timeline.Tests.IntegratedTests options.Converters.Add(new JsonDateTimeConverter()); foreach (var user in users) { - var s = client.GetStringAsync($"/users/{user.Username}").Result; + var s = await client.GetStringAsync($"/users/{user.Username}"); userInfoList.Add(JsonSerializer.Deserialize(s, options)); } UserInfos = userInfoList; } + + await OnInitializeAsync(); + } + + public async Task DisposeAsync() + { + await OnDisposeAsync(); + OnDispose(); + await TestApp.DisposeAsync(); } - protected virtual void OnDispose() - { - } - - public void Dispose() - { - OnDispose(); - TestApp.Dispose(); - } - - public IReadOnlyList UserInfos { get; } - public Task CreateDefaultClient() { return Task.FromResult(Factory.CreateDefaultClient()); @@ -128,6 +151,6 @@ namespace Timeline.Tests.IntegratedTests public Task CreateClientAsUser() { return CreateClientAs(1); - } + } } } diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 8801a3ef..69a04003 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -43,7 +43,11 @@ namespace Timeline.Tests.IntegratedTests public TimelineTest(WebApplicationFactory factory) : base(factory, 3) { - CreateTestTimelines().Wait(); + } + + protected override async Task OnInitializeAsync() + { + await CreateTestTimelines(); } private List _testTimelines; -- cgit v1.2.3