public void Open_with_events_traces_start_and_end_error()
        {
            ConnectionStub <IMyProxy> inner       = new ConnectionStub <IMyProxy>(true);
            ClientEventSource         eventSource = ClientEventSource.Instance;
            Guid id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5);
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, eventSource, id);

            using (ClientEventListener listener = new ClientEventListener(eventSource, EventLevel.Informational, ClientEventSource.Keywords.Connection))
            {
                Task task = outer.OpenAsync();

                Assert.False(task.IsCompleted);
                listener.VerifyEvent(ClientEventId.ConnectionOpening, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Start, id);
                listener.Events.Clear();

                InvalidTimeZoneException expectedException = new InvalidTimeZoneException("Expected.");
                inner.OpenCall.SetException(expectedException);
                Assert.True(task.IsFaulted);
                AggregateException ae = Assert.IsType <AggregateException>(task.Exception);
                Assert.Equal(1, ae.InnerExceptions.Count);
                Assert.Same(expectedException, ae.InnerException);

                listener.VerifyEvent(ClientEventId.ConnectionError, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Stop, id, "System.InvalidTimeZoneException", "Expected.");
                listener.Events.Clear();
            }
        }
        public void Invalidate_aborts_and_allows_subsequent_reconnection()
        {
            FactoryStub factoryStub = new FactoryStub();
            ConnectionManager <IMyChannel> manager        = new ConnectionManager <IMyChannel>(factoryStub);
            ConnectionStub <IMyChannel>    connectionStub = new ConnectionStub <IMyChannel>();

            factoryStub.Connections.Enqueue(connectionStub);

            Task task = manager.ConnectAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, connectionStub.OpenCount);

            manager.Invalidate();

            Assert.Equal(1, connectionStub.AbortCount);

            ConnectionStub <IMyChannel> connectionStub2 = new ConnectionStub <IMyChannel>();

            factoryStub.Connections.Enqueue(connectionStub2);
            task = manager.ConnectAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, connectionStub2.OpenCount);
        }
        public void Abort_with_events_calls_inner()
        {
            ConnectionStub <IMyProxy>       inner = new ConnectionStub <IMyProxy>();
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, ClientEventSource.Instance, Guid.Empty);

            outer.Abort();

            Assert.Equal(1, inner.AbortCount);
        }
        public void Get_instance_with_events_calls_inner()
        {
            ConnectionStub <IMyProxy> inner = new ConnectionStub <IMyProxy>();
            MyProxyStub proxyStub           = new MyProxyStub();

            inner.Instance = proxyStub;
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, ClientEventSource.Instance, Guid.Empty);

            Assert.Same(proxyStub, outer.Instance);
        }
        public void Open_with_events_calls_inner()
        {
            ConnectionStub <IMyProxy>       inner = new ConnectionStub <IMyProxy>();
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, ClientEventSource.Instance, Guid.Empty);

            Task task = outer.OpenAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, inner.OpenCount);
        }
        public void Abort_with_events_traces_event()
        {
            ConnectionStub <IMyProxy> inner       = new ConnectionStub <IMyProxy>();
            ClientEventSource         eventSource = ClientEventSource.Instance;
            Guid id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6);
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, eventSource, id);

            using (ClientEventListener listener = new ClientEventListener(eventSource, EventLevel.Informational, ClientEventSource.Keywords.Connection))
            {
                outer.Abort();
                listener.VerifyEvent(ClientEventId.ConnectionAborting, EventLevel.Informational, ClientEventSource.Keywords.Connection, id);
            }
        }
        public void Connect_when_already_connected_is_idempotent()
        {
            FactoryStub factoryStub = new FactoryStub();
            ConnectionManager <IMyChannel> manager        = new ConnectionManager <IMyChannel>(factoryStub);
            ConnectionStub <IMyChannel>    connectionStub = new ConnectionStub <IMyChannel>();

            factoryStub.Connections.Enqueue(connectionStub);

            Task task = manager.ConnectAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, connectionStub.OpenCount);

            task = manager.ConnectAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, connectionStub.OpenCount);
        }
        public void Connect_creates_and_opens_new_connection()
        {
            FactoryStub factoryStub = new FactoryStub();
            ConnectionManager <IMyChannel> manager        = new ConnectionManager <IMyChannel>(factoryStub);
            ConnectionStub <IMyChannel>    connectionStub = new ConnectionStub <IMyChannel>();

            factoryStub.Connections.Enqueue(connectionStub);

            Task task = manager.ConnectAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(1, connectionStub.OpenCount);

            MyChannelStub channelStub = new MyChannelStub();

            connectionStub.Instance = channelStub;
            Assert.Same(channelStub, manager.Proxy);
        }
        public void Open_with_events_traces_start_and_end()
        {
            ConnectionStub <IMyProxy> inner       = new ConnectionStub <IMyProxy>(true);
            ClientEventSource         eventSource = ClientEventSource.Instance;
            Guid id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4);
            ConnectionWithEvents <IMyProxy> outer = new ConnectionWithEvents <IMyProxy>(inner, eventSource, id);

            using (ClientEventListener listener = new ClientEventListener(eventSource, EventLevel.Informational, ClientEventSource.Keywords.Connection))
            {
                Task task = outer.OpenAsync();

                Assert.False(task.IsCompleted);
                listener.VerifyEvent(ClientEventId.ConnectionOpening, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Start, id);
                listener.Events.Clear();

                inner.OpenCall.SetResult(false);
                Assert.Equal(TaskStatus.RanToCompletion, task.Status);
                listener.VerifyEvent(ClientEventId.ConnectionOpened, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Stop, id);
                listener.Events.Clear();
            }
        }