Skip to content

Commit

Permalink
fix: SceneManager Exceptions and Tests (#287)
Browse files Browse the repository at this point in the history
* fix: throw exceptions for improper client calls

* add tests for exceptions
  • Loading branch information
uweeby committed Jul 14, 2020
1 parent acb6dd1 commit 388d218
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Assets/Mirror/Runtime/NetworkSceneManager.cs
Expand Up @@ -215,17 +215,21 @@ void OnClientAuthenticated(INetworkConnection conn)
internal void ClientSceneMessage(INetworkConnection conn, SceneMessage msg)
{
if (!client.IsConnected)
return;
{
throw new InvalidOperationException("ClientSceneMessage: cannot change network scene while client is disconnected");
}

if (server && server.LocalClient)
return;

{
throw new InvalidOperationException("ClientSceneMessage: cannot change client network scene while operating in host mode");
}

if (string.IsNullOrEmpty(msg.sceneName))
{
throw new ArgumentNullException(msg.sceneName, "ClientChangeScene: " + msg.sceneName + " cannot be empty or null");
throw new ArgumentNullException(msg.sceneName, "ClientSceneMessage: " + msg.sceneName + " cannot be empty or null");
}

if (logger.LogEnabled()) logger.Log("ClientChangeScene newSceneName:" + msg.sceneName + " networkSceneName:" + networkSceneName);
if (logger.LogEnabled()) logger.Log("ClientSceneMessage: changing scenes from: " + networkSceneName + " to:" + msg.sceneName);

// Let client prepare for scene change
OnClientChangeScene(msg.sceneName, msg.sceneOperation);
Expand Down
22 changes: 22 additions & 0 deletions Assets/Mirror/Tests/Runtime/NetworkSceneManagerTests.cs
Expand Up @@ -137,6 +137,15 @@ public void ReadyNull()
});
}

[Test]
public void HostModeClientSceneException()
{
Assert.Throws<InvalidOperationException>(() =>
{
sceneManager.ClientSceneMessage(null, new SceneMessage());
});
}

int ClientChangeCalled;
public void ClientChangeScene(string sceneName, SceneOperation sceneOperation)
{
Expand Down Expand Up @@ -214,5 +223,18 @@ public void FinishLoadSceneHostTest()
Assert.That(onAuthInvokeCounter, Is.EqualTo(1));
Assert.That(onOnClientSceneChangedCounter, Is.EqualTo(1));
}

[UnityTest]
public IEnumerator ClientOfflineSceneException() => RunAsync(async () =>
{
client.Disconnect();
await WaitFor(() => !client.Active);
Assert.Throws<InvalidOperationException>(() =>
{
client.sceneManager.ClientSceneMessage(null, new SceneMessage());
});
});
}
}

0 comments on commit 388d218

Please sign in to comment.