-
Notifications
You must be signed in to change notification settings - Fork 450
feat: Add test for named message sent and named message received metrics #861
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
bendoyon
merged 8 commits into
experimental/netstats-dispatcher
from
experimental/netstats/named-message-test
Jun 4, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa439c5
Add test for named message sent
965c7a9
Add test for named messages sent to multiple clients
0a06ce5
Merge remote-tracking branch 'origin/experimental/netstats-dispatcher…
543f82d
Add test for named message received
3c6c2f7
Revert editor changes
d31b2da
Code review fixes
446b662
Merge branch 'experimental/netstats-dispatcher' into experimental/net…
0bc0853
Merge test fixes
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
182 changes: 182 additions & 0 deletions
182
com.unity.multiplayer.mlapi/Tests/Runtime/Metrics/NetworkMetricsTests.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,182 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using MLAPI.Metrics; | ||
using NUnit.Framework; | ||
using Unity.Multiplayer.NetStats.Dispatch; | ||
using Unity.Multiplayer.NetStats.Metrics; | ||
using Unity.Multiplayer.NetworkProfiler; | ||
using Unity.Multiplayer.NetworkProfiler.Models; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
|
||
namespace MLAPI.RuntimeTests.Metrics | ||
{ | ||
#if true | ||
public class NetworkMetricsTests | ||
{ | ||
[UnityTest] | ||
public IEnumerator NetworkMetrics_WhenNamedMessageSent_TracksNamedMessageSentMetric() | ||
{ | ||
NetworkManagerHelper.StartNetworkManager(out var networkManager); | ||
var networkMetrics = networkManager.NetworkMetrics as NetworkMetrics; | ||
var messageName = Guid.NewGuid().ToString(); | ||
|
||
var clientId = 100UL; | ||
networkMetrics.Dispatcher.RegisterObserver(new TestObserver(collection => | ||
{ | ||
var namedMessageSentMetric = AssertSingleMetricEventOfType<NamedMessageEvent>(collection, MetricNames.NamedMessageSent); | ||
Assert.AreEqual(1, namedMessageSentMetric.Values.Count); | ||
|
||
var namedMessageSent = namedMessageSentMetric.Values.First(); | ||
Assert.AreEqual(messageName, namedMessageSent.Name); | ||
Assert.AreEqual(clientId, namedMessageSent.Connection.Id); | ||
})); | ||
|
||
networkManager.CustomMessagingManager.SendNamedMessage(messageName, clientId, Stream.Null); | ||
|
||
yield return WaitForMetricsDispatch(); | ||
|
||
NetworkManagerHelper.ShutdownNetworkManager(); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator NetworkMetrics_WhenNamedMessageSentToMultipleClients_TracksNamedMessageSentMetric() | ||
{ | ||
NetworkManagerHelper.StartNetworkManager(out var networkManager); | ||
var networkMetrics = networkManager.NetworkMetrics as NetworkMetrics; | ||
var messageName = Guid.NewGuid().ToString(); | ||
|
||
networkMetrics.Dispatcher.RegisterObserver(new TestObserver(collection => | ||
{ | ||
var namedMessageSentMetric = AssertSingleMetricEventOfType<NamedMessageEvent>(collection, MetricNames.NamedMessageSent); | ||
Assert.AreEqual(3, namedMessageSentMetric.Values.Count); | ||
|
||
var clientIds = namedMessageSentMetric.Values.Select(x => x.Connection.Id).ToList(); | ||
Assert.Contains(100UL, clientIds); | ||
Assert.Contains(200UL, clientIds); | ||
Assert.Contains(300UL, clientIds); | ||
})); | ||
|
||
networkManager.CustomMessagingManager.SendNamedMessage(messageName, new List<ulong> { 100, 200, 300 }, Stream.Null); | ||
|
||
yield return WaitForMetricsDispatch(); | ||
|
||
NetworkManagerHelper.ShutdownNetworkManager(); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator NetworkMetrics_WhenNamedMessageReceived_TracksNamedMessageReceivedMetric() | ||
{ | ||
var createServerAndSingleClient = new CreateServerAndSingleClient(); | ||
yield return createServerAndSingleClient.Run(); | ||
var server = createServerAndSingleClient.Server; | ||
var client = createServerAndSingleClient.Client; | ||
var clientMetrics = client.NetworkMetrics as NetworkMetrics; | ||
|
||
var messageName = Guid.NewGuid().ToString(); | ||
LogAssert.Expect(LogType.Log, $"Received from {server.LocalClientId}"); | ||
client.CustomMessagingManager.RegisterNamedMessageHandler(messageName, (sender, payload) => | ||
{ | ||
Debug.Log($"Received from {sender}"); | ||
}); | ||
|
||
var found = false; | ||
clientMetrics.Dispatcher.RegisterObserver(new TestObserver(collection => | ||
{ | ||
var namedMessageSentMetric = collection.Metrics.SingleOrDefault(x => x.Name == MetricNames.NamedMessageReceived); | ||
Assert.NotNull(namedMessageSentMetric); | ||
|
||
var typedMetric = namedMessageSentMetric as IEventMetric<NamedMessageEvent>; | ||
Assert.NotNull(typedMetric); | ||
if (typedMetric.Values.Any()) // We always get the metric, but when it has values, something has been tracked | ||
{ | ||
Assert.AreEqual(1, typedMetric.Values.Count); | ||
|
||
var namedMessageSent = typedMetric.Values.First(); | ||
Assert.AreEqual(messageName, namedMessageSent.Name); | ||
Assert.AreEqual(server.LocalClientId, namedMessageSent.Connection.Id); | ||
|
||
found = true; | ||
} | ||
})); | ||
|
||
server.CustomMessagingManager.SendNamedMessage(messageName, client.LocalClientId, Stream.Null); | ||
|
||
yield return WaitForAFewFrames(); // Client does not receive message synchronously | ||
|
||
MultiInstanceHelpers.Destroy(); | ||
|
||
Assert.True(found); | ||
} | ||
|
||
private IEnumerator WaitForMetricsDispatch() | ||
{ | ||
yield return new WaitForEndOfFrame(); | ||
yield return new WaitForEndOfFrame(); | ||
} | ||
|
||
private IEnumerator WaitForAFewFrames() | ||
{ | ||
yield return new WaitForSeconds(0.5f); | ||
} | ||
|
||
private IEventMetric<TEvent> AssertSingleMetricEventOfType<TEvent>(MetricCollection collection, string name) | ||
{ | ||
var metric = collection.Metrics.SingleOrDefault(x => x.Name == name); | ||
Assert.NotNull(metric); | ||
|
||
var typedMetric = metric as IEventMetric<TEvent>; | ||
Assert.NotNull(typedMetric); | ||
Assert.IsNotEmpty(typedMetric.Values); | ||
|
||
return typedMetric; | ||
} | ||
|
||
private class TestObserver : IMetricObserver | ||
{ | ||
private readonly Action<MetricCollection> m_Assertion; | ||
|
||
public TestObserver(Action<MetricCollection> assertion) | ||
{ | ||
m_Assertion = assertion; | ||
} | ||
|
||
public void Observe(MetricCollection collection) | ||
{ | ||
m_Assertion.Invoke(collection); | ||
} | ||
} | ||
|
||
public class CreateServerAndSingleClient | ||
{ | ||
public NetworkManager Server { get; private set; } | ||
|
||
public NetworkManager Client { get; private set; } | ||
|
||
public IEnumerator Run() | ||
{ | ||
if (!MultiInstanceHelpers.Create(1, out var server, out NetworkManager[] clients)) | ||
{ | ||
Debug.LogError("Failed to create instances"); | ||
Assert.Fail("Failed to create instances"); | ||
} | ||
|
||
if (!MultiInstanceHelpers.Start(true, server, clients)) | ||
{ | ||
Debug.LogError("Failed to start instances"); | ||
Assert.Fail("Failed to start instances"); | ||
} | ||
|
||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients)); | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientConnectedToServer(server)); | ||
|
||
Server = server; | ||
Client = clients.SingleOrDefault(); | ||
} | ||
} | ||
} | ||
#endif | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.multiplayer.mlapi/Tests/Runtime/Metrics/NetworkMetricsTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
com.unity.multiplayer.mlapi/Tests/Runtime/com.unity.multiplayer.mlapi.runtimetests.asmdef
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't go into develop since we're going to be an optional dependency. Is the intent that this will be removed before we merge? Or, more preferably, we should begin wrapping all the relevant code in the optional dependency ifdefs now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this will be fixed once we figure out the networkprofiler package and the relevant ifdef.
I have a wacky "ifdef true" everywhere where we need it right now.