1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 16:32:54 +08:00

Merge pull request #12151 from peppy/fail-dummy-api-requests

Fail all API requests sent to DummyAPIAccess
This commit is contained in:
Dan Balasescu 2021-03-23 20:54:04 +09:00 committed by GitHub
commit bb6e50eb27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 88 additions and 30 deletions

View File

@ -23,8 +23,10 @@ namespace osu.Game.Tests.Online
{ {
case CommentVoteRequest cRequest: case CommentVoteRequest cRequest:
cRequest.TriggerSuccess(new CommentBundle()); cRequest.TriggerSuccess(new CommentBundle());
break; return true;
} }
return false;
}); });
CommentVoteRequest request = null; CommentVoteRequest request = null;
@ -108,8 +110,10 @@ namespace osu.Game.Tests.Online
{ {
case LeaveChannelRequest cRequest: case LeaveChannelRequest cRequest:
cRequest.TriggerSuccess(); cRequest.TriggerSuccess();
break; return true;
} }
return false;
}); });
} }
} }

View File

@ -135,13 +135,15 @@ namespace osu.Game.Tests.Visual.Background
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (dummyAPI.State.Value != APIState.Online || !(request is GetSeasonalBackgroundsRequest backgroundsRequest)) if (dummyAPI.State.Value != APIState.Online || !(request is GetSeasonalBackgroundsRequest backgroundsRequest))
return; return false;
backgroundsRequest.TriggerSuccess(new APISeasonalBackgrounds backgroundsRequest.TriggerSuccess(new APISeasonalBackgrounds
{ {
Backgrounds = seasonal_background_urls.Select(url => new APISeasonalBackground { Url = url }).ToList(), Backgrounds = seasonal_background_urls.Select(url => new APISeasonalBackground { Url = url }).ToList(),
EndDate = endDate EndDate = endDate
}); });
return true;
}; };
}); });

View File

@ -30,13 +30,14 @@ namespace osu.Game.Tests.Visual.Online
((DummyAPIAccess)API).HandleRequest = req => ((DummyAPIAccess)API).HandleRequest = req =>
{ {
if (req is SearchBeatmapSetsRequest searchBeatmapSetsRequest) if (!(req is SearchBeatmapSetsRequest searchBeatmapSetsRequest)) return false;
searchBeatmapSetsRequest.TriggerSuccess(new SearchBeatmapSetsResponse
{ {
searchBeatmapSetsRequest.TriggerSuccess(new SearchBeatmapSetsResponse BeatmapSets = setsForResponse,
{ });
BeatmapSets = setsForResponse,
}); return true;
}
}; };
} }

View File

@ -63,13 +63,15 @@ namespace osu.Game.Tests.Visual.Online
Builds = builds.Values.ToList() Builds = builds.Values.ToList()
}; };
changelogRequest.TriggerSuccess(changelogResponse); changelogRequest.TriggerSuccess(changelogResponse);
break; return true;
case GetChangelogBuildRequest buildRequest: case GetChangelogBuildRequest buildRequest:
if (requestedBuild != null) if (requestedBuild != null)
buildRequest.TriggerSuccess(requestedBuild); buildRequest.TriggerSuccess(requestedBuild);
break; return true;
} }
return false;
}; };
Child = changelog = new TestChangelogOverlay(); Child = changelog = new TestChangelogOverlay();

View File

@ -11,6 +11,8 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Chat.Selection; using osu.Game.Overlays.Chat.Selection;
@ -64,6 +66,24 @@ namespace osu.Game.Tests.Visual.Online
}); });
} }
[SetUpSteps]
public void SetUpSteps()
{
AddStep("register request handling", () =>
{
((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case JoinChannelRequest _:
return true;
}
return false;
};
});
}
[Test] [Test]
public void TestHideOverlay() public void TestHideOverlay()
{ {

View File

@ -85,9 +85,10 @@ namespace osu.Game.Tests.Visual.Online
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (!(request is GetCommentsRequest getCommentsRequest)) if (!(request is GetCommentsRequest getCommentsRequest))
return; return false;
getCommentsRequest.TriggerSuccess(commentBundle); getCommentsRequest.TriggerSuccess(commentBundle);
return true;
}; };
}); });

View File

@ -33,9 +33,10 @@ namespace osu.Game.Tests.Visual.Online
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (!(request is GetNewsRequest getNewsRequest)) if (!(request is GetNewsRequest getNewsRequest))
return; return false;
getNewsRequest.TriggerSuccess(r); getNewsRequest.TriggerSuccess(r);
return true;
}; };
}); });

View File

@ -170,6 +170,17 @@ namespace osu.Game.Tests.Visual.Playlists
private void bindHandler(bool delayed = false, ScoreInfo userScore = null, bool failRequests = false) => ((DummyAPIAccess)API).HandleRequest = request => private void bindHandler(bool delayed = false, ScoreInfo userScore = null, bool failRequests = false) => ((DummyAPIAccess)API).HandleRequest = request =>
{ {
// pre-check for requests we should be handling (as they are scheduled below).
switch (request)
{
case ShowPlaylistUserScoreRequest _:
case IndexPlaylistScoresRequest _:
break;
default:
return false;
}
requestComplete = false; requestComplete = false;
double delay = delayed ? 3000 : 0; double delay = delayed ? 3000 : 0;
@ -196,6 +207,8 @@ namespace osu.Game.Tests.Visual.Playlists
break; break;
} }
}, delay); }, delay);
return true;
}; };
private void triggerSuccess<T>(APIRequest<T> req, T result) private void triggerSuccess<T>(APIRequest<T> req, T result)

View File

@ -32,8 +32,10 @@ namespace osu.Game.Tests.Visual.SongSelect
{ {
case GetUserRequest userRequest: case GetUserRequest userRequest:
userRequest.TriggerSuccess(getUser(userRequest.Ruleset.ID)); userRequest.TriggerSuccess(getUser(userRequest.Ruleset.ID));
break; return true;
} }
return false;
}; };
}); });

View File

@ -131,8 +131,11 @@ namespace osu.Game.Online.API
{ {
} }
private bool succeeded;
internal virtual void TriggerSuccess() internal virtual void TriggerSuccess()
{ {
succeeded = true;
Success?.Invoke(); Success?.Invoke();
} }
@ -145,10 +148,7 @@ namespace osu.Game.Online.API
public void Fail(Exception e) public void Fail(Exception e)
{ {
if (WebRequest?.Completed == true) if (succeeded || cancelled)
return;
if (cancelled)
return; return;
cancelled = true; cancelled = true;
@ -181,9 +181,13 @@ namespace osu.Game.Online.API
/// <returns>Whether we are in a failed or cancelled state.</returns> /// <returns>Whether we are in a failed or cancelled state.</returns>
private bool checkAndScheduleFailure() private bool checkAndScheduleFailure()
{ {
if (API == null || pendingFailure == null) return cancelled; if (pendingFailure == null) return cancelled;
if (API == null)
pendingFailure();
else
API.Schedule(pendingFailure);
API.Schedule(pendingFailure);
pendingFailure = null; pendingFailure = null;
return true; return true;
} }

View File

@ -34,8 +34,9 @@ namespace osu.Game.Online.API
/// <summary> /// <summary>
/// Provide handling logic for an arbitrary API request. /// Provide handling logic for an arbitrary API request.
/// Should return true is a request was handled. If null or false return, the request will be failed with a <see cref="NotSupportedException"/>.
/// </summary> /// </summary>
public Action<APIRequest> HandleRequest; public Func<APIRequest, bool> HandleRequest;
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online); private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
@ -55,7 +56,12 @@ namespace osu.Game.Online.API
public virtual void Queue(APIRequest request) public virtual void Queue(APIRequest request)
{ {
HandleRequest?.Invoke(request); if (HandleRequest?.Invoke(request) != true)
{
// this will fail due to not receiving an APIAccess, and trigger a failure on the request.
// this is intended - any request in testing that needs non-failures should use HandleRequest.
request.Perform(this);
}
} }
public void Perform(APIRequest request) => HandleRequest?.Invoke(request); public void Perform(APIRequest request) => HandleRequest?.Invoke(request);

View File

@ -52,15 +52,15 @@ namespace osu.Game.Tests.Visual.Multiplayer
Rooms.Add(createdRoom); Rooms.Add(createdRoom);
createRoomRequest.TriggerSuccess(createdRoom); createRoomRequest.TriggerSuccess(createdRoom);
break; return true;
case JoinRoomRequest joinRoomRequest: case JoinRoomRequest joinRoomRequest:
joinRoomRequest.TriggerSuccess(); joinRoomRequest.TriggerSuccess();
break; return true;
case PartRoomRequest partRoomRequest: case PartRoomRequest partRoomRequest:
partRoomRequest.TriggerSuccess(); partRoomRequest.TriggerSuccess();
break; return true;
case GetRoomsRequest getRoomsRequest: case GetRoomsRequest getRoomsRequest:
var roomsWithoutParticipants = new List<Room>(); var roomsWithoutParticipants = new List<Room>();
@ -76,11 +76,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
} }
getRoomsRequest.TriggerSuccess(roomsWithoutParticipants); getRoomsRequest.TriggerSuccess(roomsWithoutParticipants);
break; return true;
case GetRoomRequest getRoomRequest: case GetRoomRequest getRoomRequest:
getRoomRequest.TriggerSuccess(Rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId)); getRoomRequest.TriggerSuccess(Rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId));
break; return true;
case GetBeatmapSetRequest getBeatmapSetRequest: case GetBeatmapSetRequest getBeatmapSetRequest:
var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type); var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type);
@ -89,11 +89,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
// Get the online API from the game's dependencies. // Get the online API from the game's dependencies.
game.Dependencies.Get<IAPIProvider>().Queue(onlineReq); game.Dependencies.Get<IAPIProvider>().Queue(onlineReq);
break; return true;
case CreateRoomScoreRequest createRoomScoreRequest: case CreateRoomScoreRequest createRoomScoreRequest:
createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 }); createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 });
break; return true;
case SubmitRoomScoreRequest submitRoomScoreRequest: case SubmitRoomScoreRequest submitRoomScoreRequest:
submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore
@ -108,8 +108,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
User = api.LocalUser.Value, User = api.LocalUser.Value,
Statistics = new Dictionary<HitResult, int>() Statistics = new Dictionary<HitResult, int>()
}); });
break; return true;
} }
return false;
}; };
} }