Skip to content

Commit

Permalink
Merge pull request ppy#12151 from peppy/fail-dummy-api-requests
Browse files Browse the repository at this point in the history
Fail all API requests sent to DummyAPIAccess
  • Loading branch information
smoogipoo authored Mar 23, 2021
2 parents ade5421 + 578128f commit bb6e50e
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 30 deletions.
8 changes: 6 additions & 2 deletions osu.Game.Tests/Online/TestDummyAPIRequestHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public void TestGenericRequestHandling()
{
case CommentVoteRequest cRequest:
cRequest.TriggerSuccess(new CommentBundle());
break;
return true;
}
return false;
});

CommentVoteRequest request = null;
Expand Down Expand Up @@ -108,8 +110,10 @@ private void registerHandler()
{
case LeaveChannelRequest cRequest:
cRequest.TriggerSuccess();
break;
return true;
}
return false;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ private void registerBackgroundsResponse(DateTimeOffset endDate)
dummyAPI.HandleRequest = request =>
{
if (dummyAPI.State.Value != APIState.Online || !(request is GetSeasonalBackgroundsRequest backgroundsRequest))
return;
return false;
backgroundsRequest.TriggerSuccess(new APISeasonalBackgrounds
{
Backgrounds = seasonal_background_urls.Select(url => new APISeasonalBackground { Url = url }).ToList(),
EndDate = endDate
});
return true;
};
});

Expand Down
13 changes: 7 additions & 6 deletions osu.Game.Tests/Visual/Online/TestSceneBeatmapListingOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ private void load()

((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;
};
}

Expand Down
6 changes: 4 additions & 2 deletions osu.Game.Tests/Visual/Online/TestSceneChangelogOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ public void SetUp() => Schedule(() =>
Builds = builds.Values.ToList()
};
changelogRequest.TriggerSuccess(changelogResponse);
break;
return true;
case GetChangelogBuildRequest buildRequest:
if (requestedBuild != null)
buildRequest.TriggerSuccess(requestedBuild);
break;
return true;
}
return false;
};
Child = changelog = new TestChangelogOverlay();
Expand Down
20 changes: 20 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat;
using osu.Game.Overlays;
using osu.Game.Overlays.Chat.Selection;
Expand Down Expand Up @@ -64,6 +66,24 @@ public void Setup()
});
}

[SetUpSteps]
public void SetUpSteps()
{
AddStep("register request handling", () =>
{
((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case JoinChannelRequest _:
return true;
}
return false;
};
});
}

[Test]
public void TestHideOverlay()
{
Expand Down
3 changes: 2 additions & 1 deletion osu.Game.Tests/Visual/Online/TestSceneCommentsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ private void setUpCommentsResponse(CommentBundle commentBundle)
dummyAPI.HandleRequest = request =>
{
if (!(request is GetCommentsRequest getCommentsRequest))
return;
return false;
getCommentsRequest.TriggerSuccess(commentBundle);
return true;
};
});

Expand Down
3 changes: 2 additions & 1 deletion osu.Game.Tests/Visual/Online/TestSceneNewsOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ private void setUpNewsResponse(GetNewsResponse r)
dummyAPI.HandleRequest = request =>
{
if (!(request is GetNewsRequest getNewsRequest))
return;
return false;
getNewsRequest.TriggerSuccess(r);
return true;
};
});

Expand Down
13 changes: 13 additions & 0 deletions osu.Game.Tests/Visual/Playlists/TestScenePlaylistsResultsScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ private void waitForDisplay()

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;
double delay = delayed ? 3000 : 0;
Expand All @@ -196,6 +207,8 @@ private void bindHandler(bool delayed = false, ScoreInfo userScore = null, bool
break;
}
}, delay);
return true;
};

private void triggerSuccess<T>(APIRequest<T> req, T result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public override void SetUpSteps()
{
case GetUserRequest userRequest:
userRequest.TriggerSuccess(getUser(userRequest.Ruleset.ID));
break;
return true;
}
return false;
};
});

Expand Down
16 changes: 10 additions & 6 deletions osu.Game/Online/API/APIRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ protected virtual void PostProcess()
{
}

private bool succeeded;

internal virtual void TriggerSuccess()
{
succeeded = true;
Success?.Invoke();
}

Expand All @@ -145,10 +148,7 @@ internal void TriggerFailure(Exception e)

public void Fail(Exception e)
{
if (WebRequest?.Completed == true)
return;

if (cancelled)
if (succeeded || cancelled)
return;

cancelled = true;
Expand Down Expand Up @@ -181,9 +181,13 @@ public void Fail(Exception e)
/// <returns>Whether we are in a failed or cancelled state.</returns>
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;
return true;
}
Expand Down
10 changes: 8 additions & 2 deletions osu.Game/Online/API/DummyAPIAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class DummyAPIAccess : Component, IAPIProvider

/// <summary>
/// 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>
public Action<APIRequest> HandleRequest;
public Func<APIRequest, bool> HandleRequest;

private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);

Expand All @@ -55,7 +56,12 @@ public DummyAPIAccess()

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);
Expand Down
18 changes: 10 additions & 8 deletions osu.Game/Tests/Visual/Multiplayer/TestMultiplayerRoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ protected override void LoadComplete()
Rooms.Add(createdRoom);
createRoomRequest.TriggerSuccess(createdRoom);
break;
return true;
case JoinRoomRequest joinRoomRequest:
joinRoomRequest.TriggerSuccess();
break;
return true;
case PartRoomRequest partRoomRequest:
partRoomRequest.TriggerSuccess();
break;
return true;
case GetRoomsRequest getRoomsRequest:
var roomsWithoutParticipants = new List<Room>();
Expand All @@ -76,11 +76,11 @@ protected override void LoadComplete()
}
getRoomsRequest.TriggerSuccess(roomsWithoutParticipants);
break;
return true;
case GetRoomRequest getRoomRequest:
getRoomRequest.TriggerSuccess(Rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId));
break;
return true;
case GetBeatmapSetRequest getBeatmapSetRequest:
var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type);
Expand All @@ -89,11 +89,11 @@ protected override void LoadComplete()
// Get the online API from the game's dependencies.
game.Dependencies.Get<IAPIProvider>().Queue(onlineReq);
break;
return true;
case CreateRoomScoreRequest createRoomScoreRequest:
createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 });
break;
return true;
case SubmitRoomScoreRequest submitRoomScoreRequest:
submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore
Expand All @@ -108,8 +108,10 @@ protected override void LoadComplete()
User = api.LocalUser.Value,
Statistics = new Dictionary<HitResult, int>()
});
break;
return true;
}
return false;
};
}

Expand Down

0 comments on commit bb6e50e

Please sign in to comment.