private void TestCallbackConnection(
            ConcurrentDictionary <int, ICallbackHandler> callbackHandlersDict,
            ITestCallbackHandler callbackHandler,
            int inputToHandler,
            CancellationToken token)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            serverListener.Listen();

            var ipEndpoint = (IPEndPoint)serverListener.LocalEndPoint;

            using ISocketWrapper clientSocket = SocketFactory.CreateSocket();
            clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port);

            // Don't use "using" here. The CallbackConnection will dispose the socket.
            ISocketWrapper serverSocket       = serverListener.Accept();
            var            callbackConnection = new CallbackConnection(0, serverSocket, callbackHandlersDict);
            Task           task = Task.Run(() => callbackConnection.Run(token));

            if (token.IsCancellationRequested)
            {
                task.Wait();
                Assert.False(callbackConnection.IsRunning);
            }
            else
            {
                WriteAndReadTestData(clientSocket, callbackHandler, inputToHandler);

                if (callbackHandler.Throws)
                {
                    task.Wait();
                    Assert.False(callbackConnection.IsRunning);
                }
                else
                {
                    Assert.True(callbackConnection.IsRunning);

                    // Clean up CallbackConnection
                    Stream outputStream = clientSocket.OutputStream;
                    SerDe.Write(outputStream, (int)CallbackConnection.ConnectionStatus.REQUEST_CLOSE);
                    outputStream.Flush();
                    task.Wait();
                    Assert.False(callbackConnection.IsRunning);
                }
            }
        }
Exemple #2
0
        private void TestCallbackConnection(
            ConcurrentDictionary <int, ICallbackHandler> callbackHandlersDict,
            ITestCallbackHandler callbackHandler,
            int inputToHandler,
            CancellationToken token)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            serverListener.Listen();

            var            ipEndpoint   = (IPEndPoint)serverListener.LocalEndPoint;
            ISocketWrapper clientSocket = SocketFactory.CreateSocket();

            clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port);

            var callbackConnection = new CallbackConnection(0, clientSocket, callbackHandlersDict);

            Task.Run(() => callbackConnection.Run(token));

            using ISocketWrapper serverSocket = serverListener.Accept();
            WriteAndReadTestData(serverSocket, callbackHandler, inputToHandler, token);
        }