Example #1
0
        public void Connect_returns_connection_instances_connected_to_each_other_for_every_call()
        {
            // Arrange
            var sut     = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();

            // Act
            var actual = new List <IConnection <Model> >(
                from _ in Enumerable.Range(0, 3)
                select sut.Connect(modelId));

            // Assert
            foreach (IConnection <Model> source in actual)
            {
                var observers     = new List <IObserver <Model> >();
                var subscriptions = new List <IDisposable>();
                foreach (IConnection <Model> target in actual)
                {
                    var         observer     = Mock.Of <IObserver <Model> >();
                    IDisposable subscription = target.Subscribe(observer);
                    observers.Add(observer);
                    subscriptions.Add(subscription);
                }
                var revision = new Model(modelId);
                source.Emit(revision);
                foreach (IObserver <Model> observer in observers)
                {
                    Mock.Get(observer).Verify(x => x.OnNext(revision), Once());
                }
            }
        }
Example #2
0
        public void Stream_does_not_emit_null_value()
        {
            // Arrange
            int modelId = _fixture.Create <int>();

            var factory = new StreamFactory <int, Model>(
                Mock.Of <IStreamFilter <Model> >());

            IConnection <Model> connection = factory.Connect(modelId);
            var observer = Mock.Of <IObserver <Model> >();

            connection.Subscribe(observer);

            // Act
            connection.Emit(Observable.Return(default(Model)));

            // Assert
            Mock.Get(observer).Verify(
                x => x.OnNext(IsAny <Model>()),
                Never());
            Mock.Get(observer).Verify(
                x => x.OnError(IsAny <Exception>()),
                Never());
            Mock.Get(factory.Filter).Verify(
                x => x.Execute(IsAny <Model>(), IsAny <Model>()),
                Never());
        }
Example #3
0
        public void Stream_sends_last_revision_to_filter()
        {
            // Arrange
            int modelId = _fixture.Create <int>();

            var firstRevision  = new Model(modelId);
            var secondRevision = new Model(modelId);

            var factory = new StreamFactory <int, Model>(
                Mock.Of <IStreamFilter <Model> >());

            Mock.Get(factory.Filter)
            .Setup(x => x.Execute(firstRevision, null))
            .Returns(firstRevision);

            IConnection <Model> connection = factory.Connect(modelId);

            connection.Emit(firstRevision);

            // Act
            connection.Emit(secondRevision);

            // Assert
            Mock.Get(factory.Filter).Verify(
                x => x.Execute(secondRevision, firstRevision), Once());
        }
Example #4
0
        public void Subscribe_connects_observer_with_stream()
        {
            // Arrange
            var factory             = new StreamFactory <int, Model>();
            var modelId             = _fixture.Create <int>();
            IConnection <Model> sut = factory.Connect(modelId);
            var observer            = Mock.Of <IObserver <Model> >();

            sut.Subscribe(observer);
            var revision = new Model(modelId);

            // Act
            factory.Connect(modelId).Emit(revision);

            // Assert
            Mock.Get(observer).Verify(x => x.OnNext(revision), Once());
        }
Example #5
0
        public void Connect_returns_connection_instance()
        {
            var sut     = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();

            IConnection <int, Model> actual = sut.Connect(modelId);

            actual.Should().NotBeNull();
            actual.ModelId.Should().Be(modelId);
        }
Example #6
0
        public void ExistsFor_returns_true_for_model_id_with_which_stream_created()
        {
            var sut     = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();
            IConnection <Model> connection = sut.Connect(modelId);

            bool actual = sut.ExistsFor(modelId);

            actual.Should().BeTrue();
        }
Example #7
0
        public void Connect_returns_new_instances_for_every_call()
        {
            var sut     = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();

            var actual = new List <IConnection <Model> >(
                from _ in Enumerable.Range(0, 3)
                select sut.Connect(modelId));

            actual.Should().OnlyContain(x => x != null);
            actual.ShouldAllBeEquivalentTo(actual.Distinct());
        }
Example #8
0
        public void Subscribe_connects_observer_with_stream_weakly()
        {
            // Arrange
            var factory = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();
            IConnection <Model> connection = factory.Connect(modelId);
            var reference = new WeakReference(connection);
            var observer  = Mock.Of <IObserver <Model> >();

            connection.Subscribe(observer);

            // Act
            connection = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            factory.Connect(modelId).Emit(new Model(modelId));

            // Assert
            reference.IsAlive.Should().BeFalse();
            Mock.Get(observer).Verify(x => x.OnNext(IsAny <Model>()), Never());
        }
Example #9
0
        public void sut_subscribes_only_models_having_specified_model_id()
        {
            // Arrange
            var factory     = new StreamFactory <int, Model>();
            var idGenerator = new Generator <int>(_fixture);

            var modelId             = idGenerator.First();
            IConnection <Model> sut = factory.Connect(modelId);
            var observer            = Mock.Of <IObserver <Model> >();

            sut.Subscribe(observer);

            var otherModelId = idGenerator.First(x => x != modelId);
            IConnection <Model> otherConnection =
                factory.Connect(otherModelId);

            // Act
            otherConnection.Emit(new Model(otherModelId));

            // Assert
            Mock.Get(observer).Verify(x => x.OnNext(IsAny <Model>()), Never());
        }
Example #10
0
        public void sut_removes_unused_stream()
        {
            // Arrange
            var factory = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();

            factory.Connect(modelId);

            // Act
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Assert
            factory.ExistsFor(modelId).Should().BeFalse();
        }
Example #11
0
        public void Stream_sends_new_revision_to_filter()
        {
            // Arrange
            int modelId  = _fixture.Create <int>();
            var revision = new Model(modelId);
            var factory  = new StreamFactory <int, Model>(
                Mock.Of <IStreamFilter <Model> >());
            IConnection <Model> connection = factory.Connect(modelId);

            // Act
            connection.Emit(revision);

            // Assert
            Mock.Get(factory.Filter).Verify(
                x => x.Execute(revision, null), Once());
        }
Example #12
0
        public void Stream_fails_if_model_id_invalid()
        {
            // Arrange
            var idGenerator    = new Generator <int>(_fixture);
            int modelId        = idGenerator.First();
            int invalidModelId = idGenerator.First(x => x != modelId);

            var factory = new StreamFactory <int, Model>();

            IConnection <Model> connection = factory.Connect(modelId);
            var observer = Mock.Of <IObserver <Model> >();

            connection.Subscribe(observer);

            // Act
            connection.Emit(new Model(invalidModelId));

            // Assert
            Mock.Get(observer).Verify(
                x => x.OnError(IsAny <InvalidOperationException>()), Once());
        }
Example #13
0
        public void Stream_does_not_send_value_to_connections_if_filter_returns_null()
        {
            // Arrange
            int modelId = _fixture.Create <int>();

            var   revision = new Model(modelId);
            Model filtered = null;

            var factory = new StreamFactory <int, Model>(
                Mock.Of <IStreamFilter <Model> >(x =>
                                                 x.Execute(revision, null) == filtered));

            IConnection <Model> connection = factory.Connect(modelId);
            var observer = Mock.Of <IObserver <Model> >();

            connection.Subscribe(observer);

            // Act
            connection.Emit(revision);

            // Assert
            Mock.Get(observer).Verify(x => x.OnNext(IsAny <Model>()), Never());
        }
Example #14
0
        public void Stream_sends_filter_result_to_connections()
        {
            // Arrange
            int modelId = _fixture.Create <int>();

            var revision = new Model(modelId);
            var filtered = new Model(modelId);

            var factory = new StreamFactory <int, Model>(
                Mock.Of <IStreamFilter <Model> >(x =>
                                                 x.Execute(revision, null) == filtered));

            IConnection <Model> connection = factory.Connect(modelId);
            var observer = Mock.Of <IObserver <Model> >();

            connection.Subscribe(observer);

            // Act
            connection.Emit(revision);

            // Assert
            Mock.Get(observer).Verify(x => x.OnNext(filtered), Once());
        }