@@ -9,52 +9,36 @@ internal class SessionVersionConnectionRequest : NetcodeIntegrationTest
99 {
1010 protected override int NumberOfClients => 0 ;
1111
12- // TODO: [CmbServiceTests] Adapt to run with the service
13- protected override bool UseCMBService ( )
14- {
15- return false ;
16- }
12+ // Use a specific version for the CMB tests
13+ // The CMB service has more detailed versioning logic. Not all lower versions are invalid to connect with the higher version.
14+ // This version will not connect with the version lower.
15+ private const int k_ValidCMBVersion = 5 ;
1716
1817 public SessionVersionConnectionRequest ( ) : base ( NetworkTopologyTypes . DistributedAuthority , HostOrServer . DAHost ) { }
1918
2019 private bool m_UseValidSessionVersion ;
2120 private bool m_ClientWasDisconnected ;
22- private NetworkManager m_ClientNetworkManager ;
21+ private bool m_CanStartClients ;
22+
23+ // Don't start automatically when using the CMB Service
24+ // We want to customize the SessionVersion of the session owner before they connect
25+ protected override bool CanStartServerAndClients ( ) => ! m_UseCmbService || m_CanStartClients ;
2326
2427 /// <summary>
2528 /// Callback used to mock the scenario where a client has an invalid session version
2629 /// </summary>
27- /// <returns><see cref="SessionConfig"/></returns>
28- private SessionConfig GetInavlidSessionConfig ( )
30+ private SessionConfig GetInvalidSessionConfig ( )
2931 {
3032 var authority = GetAuthorityNetworkManager ( ) ;
3133 return new SessionConfig ( authority . SessionConfig . SessionVersion - 1 ) ;
3234 }
3335
34- /// <summary>
35- /// Overriding this method allows us to configure the newly instantiated client's
36- /// NetworkManager prior to it being started.
37- /// </summary>
38- /// <param name="networkManager">the newly instantiated NetworkManager</param>
39- protected override void OnNewClientCreated ( NetworkManager networkManager )
40- {
41- m_ClientWasDisconnected = false ;
42- m_ClientNetworkManager = networkManager ;
43- m_ClientNetworkManager . OnClientDisconnectCallback += OnClientDisconnectCallback ;
44- if ( ! m_UseValidSessionVersion )
45- {
46- networkManager . OnGetSessionConfig = GetInavlidSessionConfig ;
47- }
48- base . OnNewClientCreated ( networkManager ) ;
49- }
50-
5136 /// <summary>
5237 /// Tracks if the client was disconnected or not
5338 /// </summary>
5439 private void OnClientDisconnectCallback ( ulong clientId )
5540 {
5641 m_ClientWasDisconnected = true ;
57- m_ClientNetworkManager . OnClientDisconnectCallback -= OnClientDisconnectCallback ;
5842 }
5943
6044 /// <summary>
@@ -69,51 +53,76 @@ protected override bool ShouldWaitForNewClientToConnect(NetworkManager networkMa
6953 {
7054 return m_UseValidSessionVersion ;
7155 }
72-
73- internal enum SessionVersionType
74- {
75- Valid ,
76- Invalid ,
77- }
78-
7956 /// <summary>
8057 /// Validates that when the client's session config version is valid a client will be
8158 /// allowed to connect and when it is not valid the client will be disconnected.
8259 /// </summary>
83- /// <remarks>
84- /// This is just a mock of the service logic to validate everything on the NGO side is
85- /// working correctly.
86- /// </remarks>
87- /// <param name="useValidSessionVersion">true = use valid session version | false = use invalid session version</param>
8860 [ UnityTest ]
89- public IEnumerator ValidateSessionVersion ( [ Values ] SessionVersionType type )
61+ public IEnumerator ValidateSessionVersion ( )
9062 {
91- // Test client being disconnected due to invalid session version
92- m_UseValidSessionVersion = type == SessionVersionType . Valid ;
93- yield return CreateAndStartNewClient ( ) ;
94- yield return s_DefaultWaitForTick ;
95- if ( ! m_UseValidSessionVersion )
63+ if ( m_UseCmbService )
9664 {
97- yield return WaitForConditionOrTimeOut ( ( ) => m_ClientWasDisconnected ) ;
98- AssertOnTimeout ( "Client was not disconnected when it should have been!" ) ;
99- Assert . True ( m_ClientNetworkManager . DisconnectReason . Contains ( ConnectionRequestMessage . InvalidSessionVersionMessage ) , "Client did not receive the correct invalid session version message!" ) ;
65+ var authority = GetAuthorityNetworkManager ( ) ;
66+ authority . OnGetSessionConfig = ( ) => new SessionConfig ( k_ValidCMBVersion ) ;
67+ m_CanStartClients = true ;
68+ yield return StartServerAndClients ( ) ;
10069 }
101- else
70+
71+ /*
72+ * Test client being disconnected due to invalid session version
73+ */
74+ m_UseValidSessionVersion = false ;
75+
76+ // Create and setup client to use invalid session config
77+ var invalidClient = CreateNewClient ( ) ;
78+ invalidClient . OnClientDisconnectCallback += OnClientDisconnectCallback ;
79+ invalidClient . OnGetSessionConfig = GetInvalidSessionConfig ;
80+
81+ // Start client and wait for disconnect callback
82+ m_ClientWasDisconnected = false ;
83+ yield return StartClient ( invalidClient ) ;
84+ Assert . True ( invalidClient . IsListening ) ;
85+ yield return s_DefaultWaitForTick ;
86+
87+ var timeoutHelper = new TimeoutHelper ( 30f ) ;
88+ yield return WaitForConditionOrTimeOut ( ( ) => ! invalidClient . IsListening , timeoutHelper ) ;
89+ AssertOnTimeout ( "Client is still listening when it should have been disconnected!" , timeoutHelper ) ;
90+
91+ yield return WaitForConditionOrTimeOut ( ( ) => m_ClientWasDisconnected ) ;
92+ AssertOnTimeout ( "Client was not disconnected when it should have been!" ) ;
93+
94+ var expectedReason = m_UseCmbService ? "incompatible ngo c# package versions for feature" : ConnectionRequestMessage . InvalidSessionVersionMessage ;
95+ Assert . That ( invalidClient . DisconnectReason , Does . Contain ( expectedReason ) , $ "Client did not receive the correct invalid session version message! Received: { invalidClient . DisconnectReason } ") ;
96+
97+ // Clean up invalid client
98+ invalidClient . OnClientDisconnectCallback -= OnClientDisconnectCallback ;
99+ yield return StopOneClient ( invalidClient , true ) ;
100+
101+ /*
102+ * Test a later client with a valid version
103+ * They should connect as normal
104+ */
105+ m_UseValidSessionVersion = true ;
106+
107+ // Create and setup client to use invalid session config
108+ var lateJoin = CreateNewClient ( ) ;
109+ lateJoin . OnClientDisconnectCallback += OnClientDisconnectCallback ;
110+ if ( m_UseCmbService )
102111 {
103- Assert . False ( m_ClientWasDisconnected , "Client was disconnected when it was expected to connect!" ) ;
104- Assert . True ( m_ClientNetworkManager . IsConnectedClient , "Client did not connect properly using the correct session version!" ) ;
112+ lateJoin . OnGetSessionConfig = ( ) => new SessionConfig ( k_ValidCMBVersion ) ;
105113 }
106- }
107114
108- /// <summary>
109- /// Invoked at the end of each integration test pass.
110- /// Primarily used to clean up for the next pass.
111- /// </summary>
112- protected override IEnumerator OnTearDown ( )
113- {
114- m_ClientNetworkManager . OnClientDisconnectCallback -= OnClientDisconnectCallback ;
115- m_ClientNetworkManager = null ;
116- yield return base . OnTearDown ( ) ;
115+ // Start client and wait for disconnect callback
116+ m_ClientWasDisconnected = false ;
117+ yield return StartClient ( lateJoin ) ;
118+ yield return s_DefaultWaitForTick ;
119+
120+ Assert . False ( m_ClientWasDisconnected , "Client was disconnected when it was expected to connect!" ) ;
121+ Assert . True ( lateJoin . IsConnectedClient , "Client did not connect properly using the correct session version!" ) ;
122+ Assert . That ( GetAuthorityNetworkManager ( ) . ConnectedClientsIds , Has . Member ( lateJoin . LocalClientId ) , "Newly joined client should be in connected list!" ) ;
123+
124+ // Clean up
125+ lateJoin . OnClientDisconnectCallback -= OnClientDisconnectCallback ;
117126 }
118127 }
119128}
0 commit comments