public void Start_CallTwice_DoesNotThrow()
        {
            // Arrange
            var dataStreamingTcpServer = new DataStreamingTcpServer(tcpServerMock.Object);

            // Assert
            Assert.DoesNotThrow(() =>
            {
                dataStreamingTcpServer.Start();
                dataStreamingTcpServer.Start();
            });
        }
Example #2
0
        static void Main(string[] args)
        {
            Task.Factory.StartNew(() =>
            {
                var server           = new DataStreamingTcpServer(new TcpServer(10002));
                server.DataReceived += DataStreamingTcpServer_DataReceived;

                server.Start();
            });

            var manualResetEvent = new ManualResetEvent(false);

            manualResetEvent.WaitOne();
        }
        public void Start_StartCalled_DataStreamingTcpServerSubscribedToAcceptedClient()
        {
            Smock.Run(context =>
            {
                // Arrange
                var tcpListenerMock = new Mock <ITcpListener>();

                context.Setup(() => MultiSourceTcpListener.Create(It.IsAny <int>()))
                .Returns(tcpListenerMock.Object);

                // Act
                var dataStreamingTcpServer = new DataStreamingTcpServer(new TcpServer(0));
                dataStreamingTcpServer.Start();

                // Assert
                var acceptedClientHandler = GetAcceptedClientHandlerByReflection(dataStreamingTcpServer);

                Assert.IsNotNull(acceptedClientHandler);
                Assert.AreEqual(1, acceptedClientHandler.GetInvocationList().Length);
            });
        }
        public void Stop_StopCalled_DataStreamingTcpServerUnsubscribedFromAcceptedClient()
        {
            Smock.Run(context =>
            {
                // Arrange
                var tcpListenerMock = new Mock <ITcpListener>();

                context.Setup(() => MultiSourceTcpListener.Create(It.IsAny <int>()))
                .Returns(tcpListenerMock.Object);

                var dataStreamingTcpServer = new DataStreamingTcpServer(new TcpServer(0));
                dataStreamingTcpServer.Start();

                // Act
                dataStreamingTcpServer.Stop();

                // Assert
                var acceptedClientHandler = GetAcceptedClientHandlerByReflection(dataStreamingTcpServer);

                Assert.IsNull(acceptedClientHandler);
            });
        }
        private Delegate GetAcceptedClientHandlerByReflection(DataStreamingTcpServer dataStreamingTcpServer)
        {
            TcpServer tcpServer = GetTcpServerInstanceByReflection(dataStreamingTcpServer);

            return(typeof(TcpServer).GetField(nameof(tcpServer.AcceptedClient), BindingFlags.NonPublic | BindingFlags.Instance).GetValue(tcpServer) as Delegate);
        }
 private TcpServer GetTcpServerInstanceByReflection(DataStreamingTcpServer dataStreamingTcpServer)
 {
     return((TcpServer)typeof(DataStreamingTcpServer).GetField("tcpServer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(dataStreamingTcpServer));
 }