about summary refs log tree commit diff
path: root/Tests/LibMatrix.Tests/Tests/TestCleanup.cs
blob: d05634566dbe48f2edd6da3a0d4f4482acf81228 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Diagnostics;
using LibMatrix.Helpers;
using LibMatrix.Services;
using LibMatrix.Tests.Abstractions;
using LibMatrix.Tests.Fixtures;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
using Xunit.Microsoft.DependencyInjection.Abstracts;

namespace LibMatrix.Tests.Tests;

public class TestCleanup : TestBed<TestFixture> {
    // private readonly TestFixture _fixture;
    private readonly HomeserverResolverService _resolver;
    private readonly Config _config;
    private readonly HomeserverProviderService _provider;
    private readonly ILogger<TestCleanup> _logger;

    public TestCleanup(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) {
        // _fixture = fixture;
        _resolver = _fixture.GetService<HomeserverResolverService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverResolverService)}");
        _config = _fixture.GetService<Config>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}");
        _provider = _fixture.GetService<HomeserverProviderService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverProviderService)}");
        _logger = _fixture.GetService<ILogger<TestCleanup>>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(ILogger<TestCleanup>)}");
    }

    [Fact]
    public async Task Cleanup() {
        Assert.False(string.IsNullOrWhiteSpace(_config.TestHomeserver), $"{nameof(_config.TestHomeserver)} must be set in appsettings!");
        Assert.False(string.IsNullOrWhiteSpace(_config.TestUsername), $"{nameof(_config.TestUsername)} must be set in appsettings!");
        Assert.False(string.IsNullOrWhiteSpace(_config.TestPassword), $"{nameof(_config.TestPassword)} must be set in appsettings!");

        var hs = await HomeserverAbstraction.GetHomeserver();
        Assert.NotNull(hs);

        var syncHelper = new SyncHelper(hs, _logger) {
            Timeout = 3000
        };
        _testOutputHelper.WriteLine("Starting sync loop");
        var cancellationTokenSource = new CancellationTokenSource();
        var sw = Stopwatch.StartNew();
        syncHelper.SyncReceivedHandlers.Add(async response => {
            if (sw.ElapsedMilliseconds >= 3000) {
                _testOutputHelper.WriteLine("Cancelling sync loop");

                var tasks = (await hs.GetJoinedRooms()).Select(async room => {
                    _logger.LogInformation("Leaving room: {}", room.RoomId);
                    await room.LeaveAsync();
                    await room.ForgetAsync();
                    return room;
                }).ToList();
                await Task.WhenAll(tasks);

                cancellationTokenSource.Cancel();
            }

            sw.Restart();
            if (response.Rooms?.Leave is { Count: > 0 }) {
                // foreach (var room in response.Rooms.Leave) {
                // await hs.GetRoom(room.Key).ForgetAsync();
                // }
                var tasks = response.Rooms.Leave.Select(async room => {
                    await hs.GetRoom(room.Key).ForgetAsync();
                    return room;
                }).ToList();
                await Task.WhenAll(tasks);
            }
        });
        await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationTokenSource.Token);

        Assert.NotNull(hs);
        await hs.Logout();
    }
}