public void GameListener_OptionsValidation() { Mock <ILogger> loggerMock = new Mock <ILogger>(); Mock <ISocketConnectionFactory> connectionFactoryMock = new Mock <ISocketConnectionFactory>(); Mock <IDoSDefender> defenderMock = new Mock <IDoSDefender>(); GameListenerOptions goodGameListenerOptions = new GameListenerOptions() { Port = 7171 }; GameListenerOptions badGameListenerOptions = new GameListenerOptions(); // Initialize with null parameters, should throw. ExceptionAssert.Throws <ArgumentNullException>( () => new GameListener <ISocketConnectionFactory>(loggerMock.Object, null, connectionFactoryMock.Object, defenderMock.Object), "Value cannot be null. (Parameter 'options')"); ExceptionAssert.Throws <ValidationException>( () => new GameListener <ISocketConnectionFactory>(loggerMock.Object, Options.Create(badGameListenerOptions), connectionFactoryMock.Object, defenderMock.Object), "A port for the game listener must be speficied."); // And initialize with all good values. new GameListener <ISocketConnectionFactory>(loggerMock.Object, Options.Create(goodGameListenerOptions), connectionFactoryMock.Object, defenderMock.Object); }
public async Task GameListener_CallsNewConnectionEvent() { const ushort AnyEphemerealPort = 7171; const int ExpectedConnectionCount = 1; const int NewConnectionsToEmulate = 1; TimeSpan waitForConnectionDelay = TimeSpan.FromSeconds(2); Mock <ILogger> loggerMock = new Mock <ILogger>(); Mock <IDoSDefender> defenderMock = new Mock <IDoSDefender>(); Mock <ITcpListener> tcpListenerMock = this.SetupTcpListenerMock(NewConnectionsToEmulate); Mock <ISocketConnectionFactory> connectionFactoryMock = this.SetupSocketConnectionFactory(); GameListenerOptions gameListenerOptions = new GameListenerOptions() { Port = AnyEphemerealPort, }; IListener gameListener = new GameListener <ISocketConnectionFactory>( loggerMock.Object, Options.Create(gameListenerOptions), connectionFactoryMock.Object, defenderMock.Object, tcpListenerMock.Object); var connectionCount = 0; var listenerTask = gameListener.StartAsync(CancellationToken.None); gameListener.NewConnection += (connection) => { connectionCount++; }; // Delay for a second and check that the counter has gone up on connections count. await Task.Delay(waitForConnectionDelay).ContinueWith(prev => { Assert.AreEqual(ExpectedConnectionCount, connectionCount, "New connections events counter does not match."); }); }
public void GameListener_Initialization() { Mock <ILogger> loggerMock = new Mock <ILogger>(); Mock <ISocketConnectionFactory> connectionFactoryMock = new Mock <ISocketConnectionFactory>(); Mock <IDoSDefender> defenderMock = new Mock <IDoSDefender>(); GameListenerOptions gameListenerOptions = new GameListenerOptions() { Port = 7171, }; // Initialize with null parameters, should throw. ExceptionAssert.Throws <ArgumentNullException>(() => new GameListener <ISocketConnectionFactory>(null, Options.Create(gameListenerOptions), connectionFactoryMock.Object, defenderMock.Object), "Value cannot be null. (Parameter 'logger')"); ExceptionAssert.Throws <ArgumentNullException>(() => new GameListener <ISocketConnectionFactory>(loggerMock.Object, null, connectionFactoryMock.Object, defenderMock.Object), "Value cannot be null. (Parameter 'options')"); ExceptionAssert.Throws <ArgumentNullException>(() => new GameListener <ISocketConnectionFactory>(loggerMock.Object, Options.Create(gameListenerOptions), null, defenderMock.Object), "Value cannot be null. (Parameter 'socketConnectionFactory')"); // A null DoS defender is OK. new GameListener <ISocketConnectionFactory>(loggerMock.Object, Options.Create(gameListenerOptions), connectionFactoryMock.Object, null); // And initialize with all good values. new GameListener <ISocketConnectionFactory>(loggerMock.Object, Options.Create(gameListenerOptions), connectionFactoryMock.Object, defenderMock.Object); }