public async Task MultipleClientConnectionsEventCalls()
        {
            await DefaultSetup(new CalculatorService(), 10);

            Task[]    connectionTasks = new Task[connections.Length];
            const int taskCount       = 25;

            for (int connectionIndex = 0; connectionIndex < connections.Length; connectionIndex++)
            {
                SimpleInMemConnection conn = connections[connectionIndex];
                connectionTasks[connectionIndex] = Task.Run(() =>
                {
                    Task[] tasks = new Task[taskCount];

                    for (int taskIndex = 0; taskIndex < taskCount; taskIndex++)
                    {
                        tasks[taskIndex] = Task.Run(() =>
                        {
                            var calculatorProxy = new CalculatorProxy <SimpleInMemConnection>(conn);
                            calculatorProxy.IncrementCountAsync();
                        });
                    }

                    Task.WaitAll(tasks);
                });
            }

            Task.WaitAll(connectionTasks);

            int       totalWaitTime = 0;
            const int maxWait       = 5000;
            // Intentionally avoiding exponential back-off due to simple nature of this test
            const int incrementalWait = 500;

            while (totalWaitTime < maxWait)
            {
                await Task.Delay(incrementalWait);

                totalWaitTime += incrementalWait;

                try
                {
                    Assert.AreEqual(CalculatorService.Count, connections.Length * taskCount);
                    break;
                }
                catch (AssertionException)
                {
                    // The implementation of SimpleInMem can guarantee delivery of events just be virtue of staying with in the process boundary.
                    // SimpleInMem event failing after 5 seconds needs to raise alarm and investigation.
                    if (totalWaitTime > maxWait)
                    {
                        throw;
                    }
                }
            }

            Console.WriteLine($"{nameof(MultipleClientConnectionsEventCalls)} - Count: {CalculatorService.Count}");
        }
Example #2
0
        internal QueueProcessor(SimpleInMemConnection connection, ServiceHost serviceHost,
            SimpleInMemTransport transport, Logger logger)
        {
            if (connection == null) throw new ArgumentNullException(nameof(connection));
            if (serviceHost == null) throw new ArgumentNullException(nameof(serviceHost));
            if (transport == null) throw new ArgumentNullException(nameof(transport));

            this.connection = connection;
            this.serviceHost = serviceHost;
            this.transport = transport;
            this.logger = logger;
        }
        public async void TestWithServerAndClientConnections()
        {
            await DefaultSetup(new CalculatorService(), 1);

            IEnumerator <Guid> pairIds = listener.GetPairIds().GetEnumerator();

            pairIds.MoveNext();
            Guid firstPair = pairIds.Current;
            SimpleInMemConnection serverConnection = listener.GetConnection(firstPair, ConnectionType.Server);
            SimpleInMemConnection clientConnection = listener.GetConnection(firstPair, ConnectionType.Client);

            const int first     = 91;
            const int second    = 23;
            int       addResult = first + second;
            int       subResult = first - second;

            var serverProxy = new CalculatorProxy <SimpleInMemConnection>(serverConnection);
            var clientProxy = new CalculatorProxy <SimpleInMemConnection>(clientConnection);


            var input = new PairedInput
            {
                First  = first,
                Second = second
            };
            var request = new Message <PairedInput>(input);
            IMessage <Output> addResponse = await clientProxy.AddAsync(request, System.Threading.CancellationToken.None);

            IMessage <Output> subResponse = await clientProxy.SubtractAsync(request, System.Threading.CancellationToken.None);

            Assert.IsFalse(addResponse.IsError);
            Assert.IsFalse(subResponse.IsError);
            Output addOutput = addResponse.Payload.Deserialize();
            Output subOutput = subResponse.Payload.Deserialize();

            Assert.AreEqual(addResult, addOutput.Result);
            Assert.AreEqual(subResult, subOutput.Result);

            addResponse = await serverProxy.AddAsync(request, System.Threading.CancellationToken.None);

            subResponse = await serverProxy.SubtractAsync(request, System.Threading.CancellationToken.None);

            Assert.IsTrue(addResponse.IsError);
            Assert.IsTrue(subResponse.IsError);
            Error addError = addResponse.Error.Deserialize();
            Error subError = subResponse.Error.Deserialize();

            Assert.AreEqual((int)ErrorCode.METHOD_NOT_FOUND, (int)addError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Add].", addError.message);
            Assert.AreEqual((int)ErrorCode.METHOD_NOT_FOUND, (int)subError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Subtract].", subError.message);
        }
        public async void ConnectionStateCycle()
        {
            await DefaultSetup(new CalculatorService(), 1);

            SimpleInMemConnection localConnection =
                (SimpleInMemConnection)await transport.ConnectToAsync(address, System.Threading.CancellationToken.None);

            Assert.AreEqual(localConnection.State, CnxState.Connected);

            await localConnection.StopAsync();

            Assert.AreEqual(localConnection.State, CnxState.Disconnected);
        }
Example #5
0
        public async void ConnectToAsync()
        {
            transport.MakeListener(address);
            Connection conn = await transport.ConnectToAsync(address, new System.Threading.CancellationToken());

            Assert.NotNull(conn);
            Assert.True(conn is SimpleInMemConnection);
            SimpleInMemConnection simpleConn = (SimpleInMemConnection)conn;

            Assert.True(simpleConn.ConnectionType == ConnectionType.Client);
            await transport.StopAsync();

            Assert.Null(transport.GetListener(address));
        }
Example #6
0
        internal QueueProcessor(SimpleInMemConnection connection, ServiceHost serviceHost, Logger logger)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (serviceHost == null)
            {
                throw new ArgumentNullException(nameof(serviceHost));
            }

            this.connection  = connection;
            this.serviceHost = serviceHost;
            this.logger      = logger;
        }
        public async void ConnectionStateCycle_CloseAlreadyClosedConnection()
        {
            await DefaultSetup(new CalculatorService(), 1);

            SimpleInMemConnection localConnection =
                (SimpleInMemConnection)await transport.ConnectToAsync(address, System.Threading.CancellationToken.None);

            Assert.AreEqual(localConnection.State, CnxState.Connected);

            // Ensure that closing an already closed connection is no-op
            for (int index = 0; index < 5; index++)
            {
                await localConnection.StopAsync();

                Assert.AreEqual(localConnection.State, CnxState.Disconnected);
            }
        }
Example #8
0
        public async Task ConnectedEvent_HasRightRemoteEndpointDetails()
        {
            SimpleInMemConnection listenerConnection = null;
            var connectedEventDone = new ManualResetEventSlim(initialState: false);

            SimpleInMemListener listener = (SimpleInMemListener)transport.MakeListener(address);

            listener.Connected += (sender, args) =>
            {
                Assert.AreSame(listener, sender);
                listenerConnection = (SimpleInMemConnection)args.Connection;
                connectedEventDone.Set();
            };

            await listener.StartAsync();

            var connection = (SimpleInMemConnection)await transport.ConnectToAsync(address);

            bool wasSignaled = connectedEventDone.Wait(TimeSpan.FromSeconds(30));

            Assert.IsTrue(wasSignaled, "Timed out waiting for Connected event to complete");

            Assert.AreEqual(connection.Id, listenerConnection.Id);
        }
        public async Task MultipleClientConnectionsMethodCalls()
        {
            Stopwatch sw = Stopwatch.StartNew();

            await DefaultSetup(new CalculatorService(), 10);

            Task[] connectionTasks = new Task[connections.Length];

            for (int connectionIndex = 0; connectionIndex < connections.Length; connectionIndex++)
            {
                SimpleInMemConnection conn = connections[connectionIndex];
                connectionTasks[connectionIndex] = Task.Run(() =>
                {
                    int taskCount = 25;
                    Task[] tasks  = new Task[taskCount];

                    for (int taskIndex = 0; taskIndex < taskCount; taskIndex++)
                    {
                        tasks[taskIndex] = Task.Run(async() =>
                        {
                            Random rand           = new Random(DateTime.UtcNow.Millisecond);
                            int first             = rand.Next(1, 100);
                            int second            = rand.Next(1, 50);
                            int expectedAddResult = first + second;
                            int expectedSubResult = first - second;
                            var addTraceId        = Guid.NewGuid().ToString();
                            var subTraceId        = Guid.NewGuid().ToString();
                            var calculatorProxy   = new CalculatorProxy <SimpleInMemConnection>(conn);

                            var addInput = new PairedInput
                            {
                                First   = first,
                                Second  = second,
                                TraceId = addTraceId
                            };

                            var subInput = new PairedInput
                            {
                                First   = first,
                                Second  = second,
                                TraceId = subTraceId
                            };

                            Message <PairedInput> addRequest = new Message <PairedInput>(addInput);
                            Message <PairedInput> subRequest = new Message <PairedInput>(subInput);
                            IMessage <Output> addResponse    = await calculatorProxy.AddAsync(addRequest, System.Threading.CancellationToken.None);
                            IMessage <Output> subResponse    = await calculatorProxy.SubtractAsync(subRequest, System.Threading.CancellationToken.None);
                            Output addOutput = addResponse.Payload.Deserialize();
                            Output subOutput = subResponse.Payload.Deserialize();

                            Assert.AreEqual(expectedAddResult, addOutput.Result);
                            Assert.AreEqual(addInput.TraceId, addOutput.TraceId);
                            Assert.AreEqual(expectedSubResult, subOutput.Result);
                            Assert.AreEqual(subInput.TraceId, subOutput.TraceId);
                        });
                    }

                    Task.WaitAll(tasks);
                });
            }

            Task.WaitAll(connectionTasks);
            sw.Stop();
            Console.WriteLine($"{nameof(MultipleClientConnectionsMethodCalls)} - test time: {sw.Elapsed.TotalSeconds}");
        }
Example #10
0
 internal BatchProcessor(SimpleInMemConnection connection, ServiceHost serviceHost, Logger logger) 
     : base(connection, serviceHost, logger)
 {
 }
Example #11
0
 public SimpleInMemSendContext(SimpleInMemConnection connection)
 {
     Connection = connection;
 }
Example #12
0
 public SimpleInMemReceiveContext(SimpleInMemConnection connection)
 {
     Connection = connection;
 }
Example #13
0
 public SimpleInMemSendContext(SimpleInMemConnection connection, ConnectionMetrics connectionMetrics, RequestMetrics requestMetrics)
     : base(connectionMetrics, requestMetrics)
 {
     Connection = connection;
 }
Example #14
0
 public SimpleInMemSendContext(SimpleInMemConnection connection)
 {
     Connection = connection;
 }
Example #15
0
 public SimpleInMemReceiveContext(SimpleInMemConnection connection)
 {
     Connection = connection;
 }
Example #16
0
 internal BatchProcessor(SimpleInMemConnection connection, ServiceHost serviceHost, SimpleInMemTransport transport, Logger logger)
     : base(connection, serviceHost, transport, logger)
 {
 }
 public SimpleInMemSendContext(SimpleInMemConnection connection, ConnectionMetrics connectionMetrics, RequestMetrics requestMetrics)
     : base(connectionMetrics, requestMetrics)
 {
     Connection = connection;
 }