public void Invalidate_before_connect_does_nothing()
        {
            FactoryStub factoryStub = new FactoryStub();
            ConnectionManager <IMyChannel> manager = new ConnectionManager <IMyChannel>(factoryStub);

            manager.Invalidate();
        }
        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 Get_proxy_before_connect_throws_InvalidOperation()
        {
            FactoryStub factoryStub = new FactoryStub();
            ConnectionManager <IMyChannel> manager = new ConnectionManager <IMyChannel>(factoryStub);

            Assert.Throws <InvalidOperationException>(() => manager.Proxy);
        }
        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);
        }