public void ExistsFor_returns_false_for_nonexistent_model_id()
        {
            var sut     = new StreamFactory <int, Model>();
            var modelId = _fixture.Create <int>();

            bool actual = sut.ExistsFor(modelId);

            actual.Should().BeFalse();
        }
        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);
        }
        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();
        }
        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());
        }
Exemple #5
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();
        }
        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());
        }
Exemple #7
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());
        }
        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());
        }
Exemple #9
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());
        }
Exemple #10
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());
        }
Exemple #11
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());
        }
Exemple #12
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());
        }