Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Antoine-Soucy committed Apr 23, 2024
1 parent 7055a21 commit 7ba0cd5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
using System;
using System.Reactive.Linq;
using ApplicationTemplate.DataAccess;
using Microsoft.Extensions.Logging;

namespace ApplicationTemplate.Business;

/// <summary>
/// Implementation of the IKillSwitchService.
/// </summary>
public class KillSwitchService : IKillSwitchService
public class KillSwitchService : IKillSwitchService, IDisposable
{
private readonly IKillSwitchRepository _killSwitchRepository;
private readonly ILogger<KillSwitchService> _logger;
private readonly IDisposable _subscription;

/// <summary>
/// Initializes a new instance of the <see cref="KillSwitchService"/> class.
/// </summary>
/// <param name="killSwitchRepository">The <see cref="IKillSwitchRepository"/>.</param>
public KillSwitchService(IKillSwitchRepository killSwitchRepository)
/// <param name="logger">The <see cref="ILogger{KillSwitchService}"/>.</param>
public KillSwitchService(IKillSwitchRepository killSwitchRepository, ILogger<KillSwitchService> logger)
{
_killSwitchRepository = killSwitchRepository;
_logger = logger;

_subscription = _killSwitchRepository.ObserveKillSwitchActivation()
.Subscribe(isActive =>
{
_logger.LogInformation("Kill switch is now {isActive}", isActive);
});
}

/// <inheritdoc/>
public IObservable<bool> ObserveKillSwitchActivation() => _killSwitchRepository.ObserveKillSwitchActivation();

/// <inheritdoc/>
public void Dispose()
{
_subscription.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationTemplate.DataAccess;

Expand All @@ -26,17 +25,7 @@ public async Task RedirectTheAppToForceUpdatePage()
// This will raise the update required event.
minimumVersionReposiory.CheckMinimumVersion();

var timeoutTask = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() == typeof(ForcedUpdatePageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
await WaitForNavigation(viewModelType: typeof(ForcedUpdatePageViewModel), isDestination: true);

// Assert
ActiveViewModel.Should().BeOfType<ForcedUpdatePageViewModel>();
Expand All @@ -57,17 +46,7 @@ public async Task DisableTheBackButton()
// This will raise the update required event.
minimumVersionReposiory.CheckMinimumVersion();

var timeoutTask = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() == typeof(ForcedUpdatePageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
await WaitForNavigation(viewModelType: typeof(ForcedUpdatePageViewModel), isDestination: true);

NavigateBackUsingHardwareButton();

Expand Down
25 changes: 25 additions & 0 deletions src/app/ApplicationTemplate.Tests.Functional/FunctionalTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationTemplate.DataAccess;
using Chinook.BackButtonManager;
Expand Down Expand Up @@ -127,6 +129,29 @@ public void NavigateBackUsingHardwareButton()
_backButtonSource.RaiseBackRequested();
}

/// <summary>
/// Waits for navigation out of or to a specific ViewModel type to be completed.
/// This is for when you cannot let the test continue until the navigation is completed.
/// It should be used when you know that the navigation is going to happen.
/// </summary>
/// <param name="viewModelType">The type of viewmodel we are navigating to or from.</param>
/// <param name="isDestination">Whether the viewmodel is the destination or the origin.</param>
protected async Task WaitForNavigation(Type viewModelType, bool isDestination)
{
var sectionNavigator = GetService<ISectionsNavigator>();

var timeoutTask = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
&& (x.GetCurrentOrNextViewModelType() == viewModelType) == isDestination)
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
}

/// <summary>
/// Configures the services required for functional testing.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Reactive.Subjects;
using System.Threading;
using ApplicationTemplate.DataAccess;
using ApplicationTemplate.Tests;
using Microsoft.Extensions.Hosting;
Expand All @@ -26,22 +25,11 @@ public async Task NavigateToKillSwitchPageWhenTheKillSwitchIsActivated()
{
// Arrange
var vm = await this.ReachLoginPage();
var sectionNavigator = GetService<ISectionsNavigator>();

// Act
_killSwitchActivatedSubject.OnNext(true);

var timeoutTask = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() == typeof(KillSwitchPageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
await WaitForNavigation(viewModelType: typeof(KillSwitchPageViewModel), isDestination: true);

// Assert
ActiveViewModel.Should().BeOfType<KillSwitchPageViewModel>();
Expand All @@ -60,17 +48,7 @@ public async Task DisableTheBackButton()
// Act
_killSwitchActivatedSubject.OnNext(true);

var timeoutTask = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() == typeof(KillSwitchPageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
await WaitForNavigation(viewModelType: typeof(KillSwitchPageViewModel), isDestination: true);

// Navigate back using the hardware button.
NavigateBackUsingHardwareButton();
Expand All @@ -93,44 +71,20 @@ public async Task NavigateBackWhenTheKillSwitchIsDeactivated()
{
// Arrange
var vm = await this.ReachLoginPage();
var sectionNavigator = GetService<ISectionsNavigator>();
var killSwitchRepository = GetService<IKillSwitchRepository>();

// Act
_killSwitchActivatedSubject.OnNext(true);

var timeoutTask = Task.Delay(5000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() == typeof(KillSwitchPageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask, timeoutTask);
await WaitForNavigation(viewModelType: typeof(KillSwitchPageViewModel), isDestination: true);

_killSwitchActivatedSubject.OnNext(false);

var timeoutTask2 = Task.Delay(1000);

// Waits for the navigation to be completed, we need the StartWith in case the navigation already finished before we started observing.
var navTask2 = sectionNavigator.ObserveCurrentState()
.Where(x => x.LastRequestState != NavigatorRequestState.Processing
// This sometimes takes longer, so we need to need to make sure
&& x.GetCurrentOrNextViewModelType() != typeof(KillSwitchPageViewModel))
// The CancellationToken is so that this returns a task.
.FirstAsync(CancellationToken.None);

await Task.WhenAny(navTask2, timeoutTask2);
await WaitForNavigation(viewModelType: typeof(KillSwitchPageViewModel), isDestination: false);

// Assert
ActiveViewModel.Should().NotBeOfType<KillSwitchPageViewModel>();
}



/// <inheritdoc />
protected override void ConfigureHost(IHostBuilder hostBuilder)
{
Expand Down

0 comments on commit 7ba0cd5

Please sign in to comment.