Add ability to simulate network failures to TestSpectatorClient

This commit is contained in:
Dean Herbert
2022-02-24 02:18:35 +09:00
parent 14c8ce50a0
commit c94e7e2abe
4 changed files with 22 additions and 8 deletions

View File

@ -3,6 +3,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
@ -20,10 +21,15 @@ namespace osu.Game.Tests.Visual.Spectator
public class TestSpectatorClient : SpectatorClient
{
/// <summary>
/// Maximum number of frames sent per bundle via <see cref="SendFrames"/>.
/// Maximum number of frames sent per bundle via <see cref="SendFramesFromUser"/>.
/// </summary>
public const int FRAME_BUNDLE_SIZE = 10;
/// <summary>
/// Whether to force send operations to fail (simulating a network issue).
/// </summary>
public bool ShouldFailSendingFrames { get; set; }
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>(true);
public IReadOnlyDictionary<int, ReplayFrame> LastReceivedUserFrames => lastReceivedUserFrames;
@ -75,10 +81,12 @@ namespace osu.Game.Tests.Visual.Spectator
/// <summary>
/// Sends frames for an arbitrary user, in bundles containing 10 frames each.
/// This bypasses the standard queueing mechanism completely and should only be used to test cases where multiple users need to be sending data.
/// Importantly, <see cref="ShouldFailSendingFrames"/> will have no effect.
/// </summary>
/// <param name="userId">The user to send frames for.</param>
/// <param name="count">The total number of frames to send.</param>
public void SendFrames(int userId, int count)
public void SendFramesFromUser(int userId, int count)
{
var frames = new List<LegacyReplayFrame>();
@ -120,7 +128,13 @@ namespace osu.Game.Tests.Visual.Spectator
return ((ISpectatorClient)this).UserBeganPlaying(api.LocalUser.Value.Id, state);
}
protected override Task SendFramesInternal(FrameDataBundle data) => ((ISpectatorClient)this).UserSentFrames(api.LocalUser.Value.Id, data);
protected override Task SendFramesInternal(FrameDataBundle bundle)
{
if (ShouldFailSendingFrames)
return Task.FromException(new InvalidOperationException());
return ((ISpectatorClient)this).UserSentFrames(api.LocalUser.Value.Id, bundle);
}
protected override Task EndPlayingInternal(SpectatorState state) => ((ISpectatorClient)this).UserFinishedPlaying(api.LocalUser.Value.Id, state);