22using System . Collections ;
33using System . Linq ;
44using NUnit . Framework ;
5+ using Unity . Collections ;
56using Unity . Multiplayer . Tools . MetricTypes ;
67using UnityEngine . TestTools ;
78using Unity . Netcode . TestHelpers . Runtime . Metrics ;
89
910namespace Unity . Netcode . RuntimeTests . Metrics
1011{
11- internal class RpcMetricsTests : SingleClientMetricTestBase
12+ internal class RpcMetricsTests : DualClientMetricTestBase
1213 {
1314 protected override void OnCreatePlayerPrefab ( )
1415 {
@@ -17,30 +18,79 @@ protected override void OnCreatePlayerPrefab()
1718 }
1819
1920 [ UnityTest ]
20- public IEnumerator TrackRpcSentMetricOnServer ( )
21+ public IEnumerator TrackRpcSentMetricOnServerToOnlyOneClientWithArray ( )
2122 {
2223 var waitForMetricValues = new WaitForEventMetricValues < RpcEvent > ( ServerMetrics . Dispatcher , NetworkMetricTypes . RpcSent ) ;
2324
24- m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ Client . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( ) ;
25+ m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( new ClientRpcParams
26+ {
27+ Send = new ClientRpcSendParams
28+ {
29+ TargetClientIds = new [ ] { FirstClient . LocalClientId }
30+ }
31+ } ) ;
2532
2633 yield return waitForMetricValues . WaitForMetricsReceived ( ) ;
2734
2835 var serverRpcSentValues = waitForMetricValues . AssertMetricValuesHaveBeenFound ( ) ;
29- Assert . AreEqual ( 2 , serverRpcSentValues . Count ) ; // Server will receive this, since it's host
36+ Assert . AreEqual ( 1 , serverRpcSentValues . Count ) ;
37+
38+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . Name == nameof ( RpcTestComponent . MyClientRpc ) ) ) ;
39+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . NetworkBehaviourName == nameof ( RpcTestComponent ) ) ) ;
40+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . BytesCount != 0 ) ) ;
41+ Assert . AreEqual ( FirstClient . LocalClientId , serverRpcSentValues . First ( ) . Connection . Id ) ;
42+ }
43+
44+ [ UnityTest ]
45+ public IEnumerator TrackRpcSentMetricOnServerToOnlyOneClientWithNativeArray ( )
46+ {
47+ var waitForMetricValues = new WaitForEventMetricValues < RpcEvent > ( ServerMetrics . Dispatcher , NetworkMetricTypes . RpcSent ) ;
48+
49+ m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( new ClientRpcParams
50+ {
51+ Send = new ClientRpcSendParams
52+ {
53+ TargetClientIdsNativeArray = new NativeArray < ulong > ( new [ ] { FirstClient . LocalClientId } , Allocator . Temp )
54+ }
55+ } ) ;
56+
57+ yield return waitForMetricValues . WaitForMetricsReceived ( ) ;
58+
59+ var serverRpcSentValues = waitForMetricValues . AssertMetricValuesHaveBeenFound ( ) ;
60+ Assert . AreEqual ( 1 , serverRpcSentValues . Count ) ;
61+
62+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . Name == nameof ( RpcTestComponent . MyClientRpc ) ) ) ;
63+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . NetworkBehaviourName == nameof ( RpcTestComponent ) ) ) ;
64+ Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . BytesCount != 0 ) ) ;
65+ Assert . AreEqual ( FirstClient . LocalClientId , serverRpcSentValues . First ( ) . Connection . Id ) ;
66+ }
67+
68+ [ UnityTest ]
69+ public IEnumerator TrackRpcSentMetricOnServerToAllClients ( )
70+ {
71+ var waitForMetricValues = new WaitForEventMetricValues < RpcEvent > ( ServerMetrics . Dispatcher , NetworkMetricTypes . RpcSent ) ;
72+
73+ m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( ) ;
74+
75+ yield return waitForMetricValues . WaitForMetricsReceived ( ) ;
76+
77+ var serverRpcSentValues = waitForMetricValues . AssertMetricValuesHaveBeenFound ( ) ;
78+ Assert . AreEqual ( 3 , serverRpcSentValues . Count ) ; // Server will receive this, since it's host
3079
3180 Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . Name == nameof ( RpcTestComponent . MyClientRpc ) ) ) ;
3281 Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . NetworkBehaviourName == nameof ( RpcTestComponent ) ) ) ;
3382 Assert . That ( serverRpcSentValues , Has . All . Matches < RpcEvent > ( x => x . BytesCount != 0 ) ) ;
3483 Assert . Contains ( Server . LocalClientId , serverRpcSentValues . Select ( x => x . Connection . Id ) . ToArray ( ) ) ;
35- Assert . Contains ( Client . LocalClientId , serverRpcSentValues . Select ( x => x . Connection . Id ) . ToArray ( ) ) ;
84+ Assert . Contains ( FirstClient . LocalClientId , serverRpcSentValues . Select ( x => x . Connection . Id ) . ToArray ( ) ) ;
85+ Assert . Contains ( SecondClient . LocalClientId , serverRpcSentValues . Select ( x => x . Connection . Id ) . ToArray ( ) ) ;
3686 }
3787
3888 [ UnityTest ]
3989 public IEnumerator TrackRpcSentMetricOnClient ( )
4090 {
41- var waitForClientMetricsValues = new WaitForEventMetricValues < RpcEvent > ( ClientMetrics . Dispatcher , NetworkMetricTypes . RpcSent ) ;
91+ var waitForClientMetricsValues = new WaitForEventMetricValues < RpcEvent > ( FirstClientMetrics . Dispatcher , NetworkMetricTypes . RpcSent ) ;
4292
43- m_PlayerNetworkObjects [ Client . LocalClientId ] [ Client . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyServerRpc ( ) ;
93+ m_PlayerNetworkObjects [ FirstClient . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyServerRpc ( ) ;
4494
4595 yield return waitForClientMetricsValues . WaitForMetricsReceived ( ) ;
4696
@@ -58,15 +108,15 @@ public IEnumerator TrackRpcSentMetricOnClient()
58108 public IEnumerator TrackRpcReceivedMetricOnServer ( )
59109 {
60110 var waitForServerMetricsValues = new WaitForEventMetricValues < RpcEvent > ( ServerMetrics . Dispatcher , NetworkMetricTypes . RpcReceived ) ;
61- m_PlayerNetworkObjects [ Client . LocalClientId ] [ Client . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyServerRpc ( ) ;
111+ m_PlayerNetworkObjects [ FirstClient . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyServerRpc ( ) ;
62112
63113 yield return waitForServerMetricsValues . WaitForMetricsReceived ( ) ;
64114
65115 var serverRpcReceivedValues = waitForServerMetricsValues . AssertMetricValuesHaveBeenFound ( ) ;
66116 Assert . AreEqual ( 1 , serverRpcReceivedValues . Count ) ;
67117
68118 var rpcReceived = serverRpcReceivedValues . First ( ) ;
69- Assert . AreEqual ( Client . LocalClientId , rpcReceived . Connection . Id ) ;
119+ Assert . AreEqual ( FirstClient . LocalClientId , rpcReceived . Connection . Id ) ;
70120 Assert . AreEqual ( nameof ( RpcTestComponent . MyServerRpc ) , rpcReceived . Name ) ;
71121 Assert . AreEqual ( nameof ( RpcTestComponent ) , rpcReceived . NetworkBehaviourName ) ;
72122 Assert . AreNotEqual ( 0 , rpcReceived . BytesCount ) ;
@@ -75,9 +125,9 @@ public IEnumerator TrackRpcReceivedMetricOnServer()
75125 [ UnityTest ]
76126 public IEnumerator TrackRpcReceivedMetricOnClient ( )
77127 {
78- var waitForClientMetricsValues = new WaitForEventMetricValues < RpcEvent > ( ClientMetrics . Dispatcher , NetworkMetricTypes . RpcReceived ) ;
128+ var waitForClientMetricsValues = new WaitForEventMetricValues < RpcEvent > ( FirstClientMetrics . Dispatcher , NetworkMetricTypes . RpcReceived ) ;
79129
80- m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ Client . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( ) ;
130+ m_PlayerNetworkObjects [ m_ServerNetworkManager . LocalClientId ] [ FirstClient . LocalClientId ] . GetComponent < RpcTestComponent > ( ) . MyClientRpc ( ) ;
81131
82132 yield return waitForClientMetricsValues . WaitForMetricsReceived ( ) ;
83133
0 commit comments