-
Notifications
You must be signed in to change notification settings - Fork 450
feat: Added "OnServerStopped" event [MPDSE-10] #2420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f5b9aee
[Feature] added "OnServerStopped" event that will trigger only on the…
RikuTheFuffs e7f9bf2
feat(documentation): added entry to changelog.md
RikuTheFuffs 62df235
feat: Added "OnClientStarted" and "OnClientStopped" events that will …
RikuTheFuffs 49c2837
feat(documentation): added entry to changelog.md
RikuTheFuffs c27d7cb
Merge branch 'develop' into feature/OnServerStopped
RikuTheFuffs 62b1a18
chore: fixed line endings
RikuTheFuffs 9810c25
documentation: updated documentation
d4f918c
fix: fixed OnServerStarted and OnClientStarted being called at differ…
RikuTheFuffs 138a8da
Merge branch 'develop' into feature/OnServerStopped
RikuTheFuffs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerEventsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
using System; | ||
using System.Collections; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
using NUnit.Framework; | ||
using Unity.Netcode.TestHelpers.Runtime; | ||
|
||
namespace Unity.Netcode.RuntimeTests | ||
{ | ||
public class NetworkManagerEventsTests | ||
{ | ||
private NetworkManager m_ClientManager; | ||
private NetworkManager m_ServerManager; | ||
|
||
[UnityTest] | ||
public IEnumerator OnServerStoppedCalledWhenServerStops() | ||
{ | ||
bool callbackInvoked = false; | ||
var gameObject = new GameObject(nameof(OnServerStoppedCalledWhenServerStops)); | ||
m_ServerManager = gameObject.AddComponent<NetworkManager>(); | ||
|
||
// Set dummy transport that does nothing | ||
var transport = gameObject.AddComponent<DummyTransport>(); | ||
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; | ||
|
||
Action<bool> onServerStopped = (bool wasAlsoClient) => | ||
{ | ||
callbackInvoked = true; | ||
Assert.IsFalse(wasAlsoClient); | ||
if (m_ServerManager.IsServer) | ||
{ | ||
Assert.Fail("OnServerStopped called when the server is still active"); | ||
} | ||
}; | ||
|
||
// Start server to cause initialization process | ||
Assert.True(m_ServerManager.StartServer()); | ||
Assert.True(m_ServerManager.IsListening); | ||
|
||
m_ServerManager.OnServerStopped += onServerStopped; | ||
m_ServerManager.Shutdown(); | ||
UnityEngine.Object.DestroyImmediate(gameObject); | ||
|
||
yield return WaitUntilManagerShutsdown(); | ||
|
||
Assert.False(m_ServerManager.IsListening); | ||
Assert.True(callbackInvoked, "OnServerStopped wasn't invoked"); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator OnClientStoppedCalledWhenClientStops() | ||
{ | ||
yield return InitializeServerAndAClient(); | ||
|
||
bool callbackInvoked = false; | ||
Action<bool> onClientStopped = (bool wasAlsoServer) => | ||
{ | ||
callbackInvoked = true; | ||
Assert.IsFalse(wasAlsoServer); | ||
if (m_ClientManager.IsClient) | ||
{ | ||
Assert.Fail("onClientStopped called when the client is still active"); | ||
} | ||
}; | ||
|
||
m_ClientManager.OnClientStopped += onClientStopped; | ||
m_ClientManager.Shutdown(); | ||
yield return WaitUntilManagerShutsdown(); | ||
|
||
Assert.True(callbackInvoked, "OnClientStopped wasn't invoked"); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator OnClientAndServerStoppedCalledWhenHostStops() | ||
{ | ||
var gameObject = new GameObject(nameof(OnClientAndServerStoppedCalledWhenHostStops)); | ||
m_ServerManager = gameObject.AddComponent<NetworkManager>(); | ||
|
||
// Set dummy transport that does nothing | ||
var transport = gameObject.AddComponent<DummyTransport>(); | ||
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; | ||
|
||
int callbacksInvoked = 0; | ||
Action<bool> onClientStopped = (bool wasAlsoServer) => | ||
{ | ||
callbacksInvoked++; | ||
Assert.IsTrue(wasAlsoServer); | ||
if (m_ServerManager.IsClient) | ||
{ | ||
Assert.Fail("onClientStopped called when the client is still active"); | ||
} | ||
}; | ||
|
||
Action<bool> onServerStopped = (bool wasAlsoClient) => | ||
{ | ||
callbacksInvoked++; | ||
Assert.IsTrue(wasAlsoClient); | ||
if (m_ServerManager.IsServer) | ||
{ | ||
Assert.Fail("OnServerStopped called when the server is still active"); | ||
} | ||
}; | ||
|
||
// Start server to cause initialization process | ||
Assert.True(m_ServerManager.StartHost()); | ||
Assert.True(m_ServerManager.IsListening); | ||
|
||
m_ServerManager.OnServerStopped += onServerStopped; | ||
m_ServerManager.OnClientStopped += onClientStopped; | ||
m_ServerManager.Shutdown(); | ||
UnityEngine.Object.DestroyImmediate(gameObject); | ||
|
||
yield return WaitUntilManagerShutsdown(); | ||
|
||
Assert.False(m_ServerManager.IsListening); | ||
Assert.AreEqual(2, callbacksInvoked, "either OnServerStopped or OnClientStopped wasn't invoked"); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator OnServerStartedCalledWhenServerStarts() | ||
{ | ||
var gameObject = new GameObject(nameof(OnServerStartedCalledWhenServerStarts)); | ||
m_ServerManager = gameObject.AddComponent<NetworkManager>(); | ||
|
||
// Set dummy transport that does nothing | ||
var transport = gameObject.AddComponent<DummyTransport>(); | ||
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; | ||
|
||
bool callbackInvoked = false; | ||
Action onServerStarted = () => | ||
{ | ||
callbackInvoked = true; | ||
if (!m_ServerManager.IsServer) | ||
{ | ||
Assert.Fail("OnServerStarted called when the server is not active yet"); | ||
} | ||
}; | ||
|
||
// Start server to cause initialization process | ||
m_ServerManager.OnServerStarted += onServerStarted; | ||
|
||
Assert.True(m_ServerManager.StartServer()); | ||
Assert.True(m_ServerManager.IsListening); | ||
|
||
yield return WaitUntilServerBufferingIsReady(); | ||
|
||
Assert.True(callbackInvoked, "OnServerStarted wasn't invoked"); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator OnClientStartedCalledWhenClientStarts() | ||
{ | ||
bool callbackInvoked = false; | ||
Action onClientStarted = () => | ||
{ | ||
callbackInvoked = true; | ||
if (!m_ClientManager.IsClient) | ||
{ | ||
Assert.Fail("onClientStarted called when the client is not active yet"); | ||
} | ||
}; | ||
|
||
yield return InitializeServerAndAClient(onClientStarted); | ||
|
||
Assert.True(callbackInvoked, "OnClientStarted wasn't invoked"); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator OnClientAndServerStartedCalledWhenHostStarts() | ||
{ | ||
var gameObject = new GameObject(nameof(OnClientAndServerStartedCalledWhenHostStarts)); | ||
m_ServerManager = gameObject.AddComponent<NetworkManager>(); | ||
|
||
// Set dummy transport that does nothing | ||
var transport = gameObject.AddComponent<DummyTransport>(); | ||
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; | ||
|
||
int callbacksInvoked = 0; | ||
Action onClientStarted = () => | ||
{ | ||
callbacksInvoked++; | ||
if (!m_ServerManager.IsClient) | ||
{ | ||
Assert.Fail("OnClientStarted called when the client is not active yet"); | ||
} | ||
}; | ||
|
||
Action onServerStarted = () => | ||
{ | ||
callbacksInvoked++; | ||
if (!m_ServerManager.IsServer) | ||
{ | ||
Assert.Fail("OnServerStarted called when the server is not active yet"); | ||
} | ||
}; | ||
|
||
m_ServerManager.OnServerStarted += onServerStarted; | ||
m_ServerManager.OnClientStarted += onClientStarted; | ||
|
||
// Start server to cause initialization process | ||
Assert.True(m_ServerManager.StartHost()); | ||
Assert.True(m_ServerManager.IsListening); | ||
|
||
yield return WaitUntilServerBufferingIsReady(); | ||
Assert.AreEqual(2, callbacksInvoked, "either OnServerStarted or OnClientStarted wasn't invoked"); | ||
} | ||
|
||
private IEnumerator WaitUntilManagerShutsdown() | ||
{ | ||
/* Need two updates to actually shut down. First one to see the transport failing, which | ||
marks the NetworkManager as shutting down. Second one where actual shutdown occurs. */ | ||
yield return null; | ||
yield return null; | ||
} | ||
|
||
private IEnumerator InitializeServerAndAClient(Action onClientStarted = null) | ||
{ | ||
// Create multiple NetworkManager instances | ||
if (!NetcodeIntegrationTestHelpers.Create(1, out m_ServerManager, out NetworkManager[] clients, 30)) | ||
{ | ||
Debug.LogError("Failed to create instances"); | ||
Assert.Fail("Failed to create instances"); | ||
} | ||
|
||
// passing no clients on purpose to start them manually later | ||
NetcodeIntegrationTestHelpers.Start(false, m_ServerManager, new NetworkManager[] { }); | ||
|
||
yield return WaitUntilServerBufferingIsReady(); | ||
m_ClientManager = clients[0]; | ||
|
||
if (onClientStarted != null) | ||
{ | ||
m_ClientManager.OnClientStarted += onClientStarted; | ||
} | ||
|
||
Assert.True(m_ClientManager.StartClient()); | ||
NetcodeIntegrationTestHelpers.RegisterHandlers(clients[0]); | ||
// Wait for connection on client side | ||
yield return NetcodeIntegrationTestHelpers.WaitForClientsConnected(clients); | ||
} | ||
|
||
private IEnumerator WaitUntilServerBufferingIsReady() | ||
{ | ||
/* wait until at least more than 2 server ticks have passed | ||
Note: Waiting for more than 2 ticks on the server is due | ||
to the time system applying buffering to the received time | ||
in NetworkTimeSystem.Sync */ | ||
yield return new WaitUntil(() => m_ServerManager.NetworkTickSystem.ServerTime.Tick > 2); | ||
} | ||
|
||
[UnityTearDown] | ||
public virtual IEnumerator Teardown() | ||
{ | ||
NetcodeIntegrationTestHelpers.Destroy(); | ||
yield return null; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerEventsTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.