Beispiel #1
0
        public void ResponseMap_AddTwice_Throws()
        {
            var respMap = new ResponseMap();
            respMap.Add(1);

            Assert.Throws<ArgumentException>(() => respMap.Add(1));
        }
Beispiel #2
0
        public async Task ResponseMap_Shutdown_SubsequentAddHasErrorResult()
        {
            var respMap = new ResponseMap();
            respMap.Shutdown();

            Task<IMessage> responseTask = respMap.Add(1);

            Assert.AreEqual(0, respMap.OutstandingCount);
            IMessage response = await responseTask;
            Assert.IsTrue(response.IsError);
            Error err = response.Error.Deserialize();
            Assert.AreEqual((int)ErrorCode.CONNECTION_SHUT_DOWN, err.error_code);
        }
Beispiel #3
0
        public async Task ResponseMap_AddComplete_ReturnedTaskIsCompleted()
        {
            var respMap = new ResponseMap();
            Task<IMessage> responseTask = respMap.Add(1);
            Assert.AreEqual(1, respMap.OutstandingCount);

            bool wasCompleted = respMap.Complete(1, anyResponse);

            Assert.IsTrue(wasCompleted);
            Assert.AreEqual(0, respMap.OutstandingCount);
            var response = await responseTask;
            Assert.AreSame(anyResponse, response);
        }
Beispiel #4
0
        public async Task ResponseMap_CompleteTwice_OnlyFirstRegistered()
        {
            var respMap = new ResponseMap();
            Task<IMessage> responseTask = respMap.Add(1);

            bool wasCompleted = respMap.Complete(1, anyResponse);
            Assert.IsTrue(wasCompleted);
            IMessage anotherResponse = Message.FromPayload(new SomePayload());
            wasCompleted = respMap.Complete(1, anotherResponse);
            Assert.IsFalse(wasCompleted);

            var response = await responseTask;
            Assert.AreSame(anyResponse, response);
        }
Beispiel #5
0
        public async Task ResponseMap_Shutdown_PendingResponsesCompletedWithError()
        {
            var respMap = new ResponseMap();
            Task<IMessage> responseTask = respMap.Add(1);

            respMap.Shutdown();
            respMap.Complete(1, anyResponse);

            Assert.AreEqual(0, respMap.OutstandingCount);

            var response = await responseTask;
            Assert.IsTrue(response.IsError);
            Error err = response.Error.Deserialize();
            Assert.AreEqual((int)ErrorCode.CONNECTION_SHUT_DOWN, err.error_code);
        }
Beispiel #6
0
        private EpoxyConnection(
            ConnectionType connectionType,
            EpoxyTransport parentTransport,
            EpoxyListener parentListener,
            ServiceHost serviceHost,
            Socket socket,
            Logger logger,
            Metrics metrics)
        {
            Debug.Assert(parentTransport != null);
            Debug.Assert(connectionType != ConnectionType.Server || parentListener != null, "Server connections must have a listener");
            Debug.Assert(serviceHost != null);
            Debug.Assert(socket != null);

            this.connectionType = connectionType;

            this.parentTransport = parentTransport;
            this.parentListener  = parentListener;
            this.serviceHost     = serviceHost;

            netSocket = new EpoxySocket(socket, logger);

            // cache these so we can use them after the socket has been shutdown
            LocalEndPoint  = (IPEndPoint)socket.LocalEndPoint;
            RemoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;

            responseMap = new ResponseMap();

            state               = State.Created;
            startTask           = new TaskCompletionSource <bool>();
            stopTask            = new TaskCompletionSource <bool>();
            shutdownTokenSource = new CancellationTokenSource();

            // start at -1 or 0 so the first conversation ID is 1 or 2.
            prevConversationId = (connectionType == ConnectionType.Client) ? -1 : 0;

            connectionMetrics.connection_id   = Guid.NewGuid().ToString();
            connectionMetrics.local_endpoint  = LocalEndPoint.ToString();
            connectionMetrics.remote_endpoint = RemoteEndPoint.ToString();

            this.logger  = logger;
            this.metrics = metrics;
        }
Beispiel #7
0
        private EpoxyConnection(
            ConnectionType connectionType,
            EpoxyTransport parentTransport,
            EpoxyListener parentListener,
            ServiceHost serviceHost,
            EpoxyNetworkStream networkStream,
            Logger logger,
            Metrics metrics)
        {
            Debug.Assert(parentTransport != null);
            Debug.Assert(connectionType != ConnectionType.Server || parentListener != null, "Server connections must have a listener");
            Debug.Assert(serviceHost != null);
            Debug.Assert(networkStream != null);

            this.connectionType = connectionType;

            this.parentTransport = parentTransport;
            this.parentListener  = parentListener;
            this.serviceHost     = serviceHost;

            this.networkStream = networkStream;

            responseMap = new ResponseMap();

            state               = State.Created;
            startTask           = new TaskCompletionSource <bool>();
            stopTask            = new TaskCompletionSource <bool>();
            shutdownTokenSource = new CancellationTokenSource();

            // start at -1 or 0 so the first conversation ID is 1 or 2.
            prevConversationId = (connectionType == ConnectionType.Client) ? -1 : 0;

            ConnectionMetrics.local_endpoint  = LocalEndPoint.ToString();
            ConnectionMetrics.remote_endpoint = RemoteEndPoint.ToString();

            this.logger  = logger;
            this.metrics = metrics;
        }
Beispiel #8
0
        public async Task ResponseMap_TakeWorks()
        {
            var respMap = new ResponseMap();
            Task<IMessage> responseTask = respMap.Add(1);
            Task<IMessage> otherTask = respMap.Add(2);

            Assert.AreEqual(2, respMap.OutstandingCount);

            TaskCompletionSource<IMessage> tcs = respMap.TakeTaskCompletionSource(1);
            Assert.AreSame(responseTask, tcs.Task);
            Assert.AreNotSame(otherTask, tcs.Task);

            Assert.AreEqual(1, respMap.OutstandingCount);

            tcs.SetResult(anyResponse);
            var response = await responseTask;
            Assert.AreSame(anyResponse, response);
        }
Beispiel #9
0
        public void ResponseMap_CompleteAfterTakeIsFalse()
        {
            var respMap = new ResponseMap();
            respMap.Add(1);

            respMap.TakeTaskCompletionSource(1);
            bool result = respMap.Complete(1, anyResponse);

            Assert.IsFalse(result);
        }
Beispiel #10
0
        public void ResponseMap_TakeAfterCompleteIsNull()
        {
            var respMap = new ResponseMap();
            respMap.Add(1);

            respMap.Complete(1, anyResponse);
            TaskCompletionSource<IMessage> tcs = respMap.TakeTaskCompletionSource(1);

            Assert.IsNull(tcs);
        }