1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 18:07:23 +08:00

Merge pull request #8714 from peppy/dummy-api-request-support

Add support for testing arbitrary API requests/responses via Dummy API
This commit is contained in:
Dan Balasescu 2020-04-14 14:08:54 +09:00 committed by GitHub
commit 79a306cb79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 7 deletions

View File

@ -0,0 +1,115 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osu.Game.Tests.Visual;
using osu.Game.Users;
namespace osu.Game.Tests.Online
{
public class TestDummyAPIRequestHandling : OsuTestScene
{
[Test]
public void TestGenericRequestHandling()
{
AddStep("register request handling", () => ((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case CommentVoteRequest cRequest:
cRequest.TriggerSuccess(new CommentBundle());
break;
}
});
CommentVoteRequest request = null;
CommentBundle response = null;
AddStep("fire request", () =>
{
response = null;
request = new CommentVoteRequest(1, CommentVoteAction.Vote);
request.Success += res => response = res;
API.Queue(request);
});
AddAssert("response event fired", () => response != null);
AddAssert("request has response", () => request.Result == response);
}
[Test]
public void TestQueueRequestHandling()
{
registerHandler();
LeaveChannelRequest request;
bool gotResponse = false;
AddStep("fire request", () =>
{
gotResponse = false;
request = new LeaveChannelRequest(new Channel(), new User());
request.Success += () => gotResponse = true;
API.Queue(request);
});
AddAssert("response event fired", () => gotResponse);
}
[Test]
public void TestPerformRequestHandling()
{
registerHandler();
LeaveChannelRequest request;
bool gotResponse = false;
AddStep("fire request", () =>
{
gotResponse = false;
request = new LeaveChannelRequest(new Channel(), new User());
request.Success += () => gotResponse = true;
API.Perform(request);
});
AddAssert("response event fired", () => gotResponse);
}
[Test]
public void TestPerformAsyncRequestHandling()
{
registerHandler();
LeaveChannelRequest request;
bool gotResponse = false;
AddStep("fire request", () =>
{
gotResponse = false;
request = new LeaveChannelRequest(new Channel(), new User());
request.Success += () => gotResponse = true;
API.PerformAsync(request);
});
AddAssert("response event fired", () => gotResponse);
}
private void registerHandler()
{
AddStep("register request handling", () => ((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case LeaveChannelRequest cRequest:
cRequest.TriggerSuccess();
break;
}
});
}
}
}

View File

@ -16,20 +16,27 @@ namespace osu.Game.Online.API
{
protected override WebRequest CreateWebRequest() => new OsuJsonWebRequest<T>(Uri);
public T Result => ((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject;
public T Result { get; private set; }
protected APIRequest()
{
base.Success += onSuccess;
base.Success += () => TriggerSuccess(((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject);
}
private void onSuccess() => Success?.Invoke(Result);
/// <summary>
/// Invoked on successful completion of an API request.
/// This will be scheduled to the API's internal scheduler (run on update thread automatically).
/// </summary>
public new event APISuccessHandler<T> Success;
internal void TriggerSuccess(T result)
{
if (Result != null)
throw new InvalidOperationException("Attempted to trigger success more than once");
Result = result;
Success?.Invoke(result);
}
}
/// <summary>
@ -96,10 +103,15 @@ namespace osu.Game.Online.API
{
if (cancelled) return;
Success?.Invoke();
TriggerSuccess();
});
}
internal void TriggerSuccess()
{
Success?.Invoke();
}
public void Cancel() => Fail(new OperationCanceledException(@"Request cancelled"));
public void Fail(Exception e)

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -30,6 +31,11 @@ namespace osu.Game.Online.API
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
/// <summary>
/// Provide handling logic for an arbitrary API request.
/// </summary>
public Action<APIRequest> HandleRequest;
public APIState State
{
get => state;
@ -55,11 +61,16 @@ namespace osu.Game.Online.API
public virtual void Queue(APIRequest request)
{
HandleRequest?.Invoke(request);
}
public void Perform(APIRequest request) { }
public void Perform(APIRequest request) => HandleRequest?.Invoke(request);
public Task PerformAsync(APIRequest request) => Task.CompletedTask;
public Task PerformAsync(APIRequest request)
{
HandleRequest?.Invoke(request);
return Task.CompletedTask;
}
public void Register(IOnlineComponent component)
{