public async Task Publish_MessageStreamProvidePlusBehavior_MessagesReceived() { var receivedStreams = 0; var receivedEvents = 0; var testBehavior = new TestBehavior(); var serviceProvider = ServiceProviderHelper.GetServiceProvider( services => services .AddFakeLogger() .AddSilverback() .AddDelegateSubscriber( (IMessageStreamEnumerable <IEvent> enumerable) => { Interlocked.Increment(ref receivedStreams); foreach (var dummy in enumerable) { Interlocked.Increment(ref receivedEvents); } }) .AddSingletonBehavior(testBehavior)); var streamPublisher = serviceProvider.GetRequiredService <IStreamPublisher>(); var streamProvider = new MessageStreamProvider <IEvent>(); await streamPublisher.PublishAsync(streamProvider); await streamProvider.PushAsync(new TestEventOne()); await streamProvider.PushAsync(new TestEventTwo()); await AsyncTestingUtil.WaitAsync(() => receivedEvents >= 2); receivedStreams.Should().Be(1); receivedEvents.Should().Be(2); }
public void GetValue_CallingGetValueWithinProvideValue_ThrowsException() { PropertyStub <object> property = null; var provideValueWasAlreadyCalled = false; Func <IBehaviorContext, object> provideValueFunction = ctx => { Assert.IsFalse(provideValueWasAlreadyCalled); provideValueWasAlreadyCalled = true; property.Behaviors.GetValueNext <object>(ctx); return(new Object()); }; var behavior = new TestBehavior(provideValueFunction); property = PropertyStub .WithBehaviors(behavior) .Build(); var context = ViewModelStub .WithProperties(property) .BuildContext(); AssertHelper.Throws <InvalidOperationException>(() => behavior.GetValue(context) ).WithMessage(EViewModels.ValueAccessedWithinProvideValue); Assert.IsTrue(provideValueWasAlreadyCalled); }
public void GetValue_CallingGetValueWithinValueInitializer_DoesNotCallValueInitializerAgain() { PropertyStub <object> property = null; Func <IBehaviorContext, object> provideValueFunction = ctx => { return(new Object()); }; var behavior = new TestBehavior(provideValueFunction); var valueInitializerWasAlreadyInvoked = false; Action <IBehaviorContext> initializeValueAction = ctx => { Assert.IsFalse(valueInitializerWasAlreadyInvoked); valueInitializerWasAlreadyInvoked = true; property.Behaviors.GetValueNext <object>(ctx); }; property = PropertyStub .WithBehaviors(behavior, new TestValueInitializerBehavior(initializeValueAction)) .Build(); var context = ViewModelStub .WithProperties(property) .BuildContext(); behavior.GetValue(context); Assert.IsTrue(valueInitializerWasAlreadyInvoked); }
public async Task Publish_WithSortedBehaviors_BehaviorsExecutedInExpectedOrder() { var callsSequence = new List <string>(); var behavior1 = new TestSortedBehavior(100, callsSequence); var behavior2 = new TestSortedBehavior(50, callsSequence); var behavior3 = new TestSortedBehavior(-50, callsSequence); var behavior4 = new TestSortedBehavior(-100, callsSequence); var behavior5 = new TestBehavior(callsSequence); var serviceProvider = ServiceProviderHelper.GetServiceProvider( services => services .AddFakeLogger() .AddSilverback() .AddSingletonBehavior(behavior1) .AddScopedBehavior(_ => behavior2) .AddTransientBehavior(_ => behavior3) .AddSingletonBehavior(behavior4) .AddSingletonBehavior(behavior5)); var publisher = serviceProvider.GetRequiredService <IPublisher>(); publisher.Publish(new TestCommandOne()); await publisher.PublishAsync(new TestCommandOne()); callsSequence.Should().BeEquivalentTo( new[] { "-100", "-50", "unsorted", "50", "100", "-100", "-50", "unsorted", "50", "100" }, options => options.WithStrictOrdering()); }
public async Task Publish_MessageStreamProvidePlusBehavior_StreamedMessagesReceived() { var receivedStreams = 0; var receivedEvents = 0; var testBehavior = new TestBehavior(); var streamPublisher = new StreamPublisher( PublisherTestsHelper.GetPublisher( builder => builder .AddDelegateSubscriber( (IMessageStreamEnumerable <IEvent> enumerable) => { Interlocked.Increment(ref receivedStreams); foreach (var dummy in enumerable) { Interlocked.Increment(ref receivedEvents); } }), new IBehavior[] { testBehavior })); var streamProvider = new MessageStreamProvider <IEvent>(); await streamPublisher.PublishAsync(streamProvider); await streamProvider.PushAsync(new TestEventOne()); await streamProvider.PushAsync(new TestEventTwo()); await AsyncTestingUtil.WaitAsync(() => receivedEvents >= 2); receivedStreams.Should().Be(1); receivedEvents.Should().Be(2); }
public void Attaching_a_behavior_returns_the_same_behavior(IOperation operation) { var sut = new TestBehavior(); var result = sut.AttachTo(operation); Assert.Equal(sut, result); }
public void The_decorator_forwards_the_executed_child_operations_to_the_decorated_operation(IOperation operation) { var sut = new TestBehavior().AttachTo(operation); sut.Execute(); Assert.Equal(operation.ExecutedChildOperations, sut.ExecutedChildOperations); }
protected override void GetDepsFromImpl(object obj, GetDepsFromContext context) { base.GetDepsFromImpl(obj, context); TestBehavior uo = (TestBehavior)obj; AddDep(uo.Values3, context); AddSurrogateDeps(uo.Event, v_ => (PersistentUnityEvent)v_, context); }
public void Attaching_a_behavior_sets_the_inner_operation_property(IOperation operation) { var sut = new TestBehavior(); sut.AttachTo(operation); Assert.Equal(operation, sut.InnerOperation); }
public void Setup() { var behavior = new TestBehavior(ValueStage.Value); VM = ViewModelStub.WithBehaviors(behavior).Build(); Manager = behavior.Manager; Context = new BehaviorContextStub(VM); }
public void When_not_initialized_the_behavior_does_not_log_behavior_applications(IOperation operation) { var sut = new TestBehavior(); sut.AttachTo(operation); sut.ApplyBahviorLog(Description); }
public void The_decorated_operation_provides_the_child_operations(IOperation op1, IOperation op2) { var operation = new FakeOperation(op1, op2); var sut = new TestBehavior().AttachTo(operation); var result = sut.GetChildOperations(); Assert.Equal(operation.GetChildOperations(), result); }
private void SetupForProperty(ValueStage stage) { var behavior = new TestBehavior(stage); Property = PropertyStub.WithBehaviors(behavior).Of <object>(); VM = ViewModelStub.WithProperties(Property).Build(); Manager = behavior.Manager; Context = new BehaviorContextStub(VM); }
public void The_decorator_forwards_the_execution_to_the_decorated_operation() { var operation = new FakeOperation(); var sut = new TestBehavior().AttachTo(operation); sut.Execute(); Assert.True(operation.HasExecuted); }
public void When_initialized_without_a_logger_the_behavior_does_not_logs_behavior_applications(IOperation operation, WorkflowConfiguration configuration) { var sut = new TestBehavior(); sut.AttachTo(operation); sut.Initialize(configuration); sut.ApplyBahviorLog(Description); }
public void The_decorator_forwards_the_initialization_to_the_decorated_operation(FakeOperation operation) { var sut = new TestBehavior().AttachTo(operation); var configuration = new FakeWorkflowConfiguration(); sut.Initialize(configuration); Assert.Equal(configuration, operation.InitializedConfiguration); }
protected override void ReadFromImpl(object obj) { base.ReadFromImpl(obj); TestBehavior uo = (TestBehavior)obj; Values = uo.Values; Values2 = Assign(uo.Values2, v_ => (PersistentTest)v_); Values25 = Assign(uo.Values25, v_ => (PersistentTest)v_); Values26 = Assign(uo.Values26, v_ => (PersistentVector3)v_); Values3 = ToID(uo.Values3); Event = uo.Event; }
public void Publish_SomeMessagesWithBehaviors_BehaviorsExecuted() { var behavior1 = new TestBehavior(); var behavior2 = new TestBehavior(); var publisher = GetPublisher(null, new[] { behavior1, behavior2 }, _asyncSubscriber); publisher.Publish(new TestCommandOne()); publisher.Publish(new TestCommandTwo()); behavior1.EnterCount.Should().Be(2); behavior2.EnterCount.Should().Be(2); }
protected override object WriteToImpl(object obj) { obj = base.WriteToImpl(obj); TestBehavior uo = (TestBehavior)obj; uo.Values = Values; uo.Values2 = Assign(Values2, v_ => (Test)v_); uo.Values25 = Assign(Values25, v_ => (Test)v_); uo.Values26 = Assign(Values26, v_ => (Vector3)v_); uo.Values3 = FromID(Values3, uo.Values3); uo.Event = Event; return(uo); }
public void Get_BehaviorReplaced( ) { IBehaviors behaviors = new BehaviorsBase( ); TestBehavior behavior1 = new TestBehavior( ); TestBehavior behavior2 = new TestBehavior( ); behaviors.Set(behavior1); behaviors.Set(behavior2); TestBehavior behavior = behaviors.Get <TestBehavior>( ); Assert.IsNotNull(behavior); Assert.AreSame(behavior2, behavior); }
public void AssociatedObject_should_be_parent_instance(object bindingContext) { //Arrange var sut = new TestBehavior(); var control = new Button { BindingContext = bindingContext }; //Act control.Behaviors.Add(sut); //Assert sut.AssociatedObject.Should().Be(control); }
public void Applied_behaviors_are_logged_with_the_innermost_operation(IOperation operation, FakeWorkflowLogger log) { var behavior = new FakeOperationBehavior().AttachTo(operation); var sut = new TestBehavior(); sut.AttachTo(behavior); sut.Initialize(new FakeWorkflowConfiguration { Logger = log }); sut.ApplyBahviorLog(Description); Assert.Equal(operation, log.AppliedBehaviors[0].Operation); }
public void Get_BehaviorNotContained( ) { IBehaviors behaviors = new BehaviorsBase( ); Exception exc = null; TestBehavior behavior = null; try { behavior = behaviors.Get <TestBehavior>( ); } catch (Exception exc2) { exc = exc2; } Assert.IsNull(behavior); Assert.IsNotNull(exc); Assert.IsInstanceOf <ArgumentException>(exc); }
public void When_initialized_with_a_logger_the_behavior_log_behavior_applications(IOperation operation, FakeWorkflowLogger log) { var sut = new TestBehavior(); sut.AttachTo(operation); sut.Initialize(new FakeWorkflowConfiguration { Logger = log }); sut.ApplyBahviorLog(Description); Assert.Equal(1, log.AppliedBehaviors.Count); Assert.Equal(sut, log.AppliedBehaviors[0].Behavior); Assert.Equal(operation, log.AppliedBehaviors[0].Operation); Assert.Equal(Description, log.AppliedBehaviors[0].Description); }
public void GetBehaviorsTest() { var behavior = new TestBehavior(); var behaviors = Interaction.GetBehaviors(testDependencyObject); Assert.IsNotNull(behaviors); Assert.AreEqual(behaviors.Count, 0); behaviors = Interaction.GetBehaviors(testDependencyObject); // for code coverage behaviors.Add(behavior); Assert.AreEqual(behaviors.Count, 1); Assert.AreEqual(behavior.AssociatedObject, testDependencyObject); behaviors.Remove(behavior); Assert.AreEqual(behaviors.Count, 0); }
public void Get_BehaviorContained( ) { IBehaviors behaviors = new BehaviorsBase( ); behaviors.Set(new TestBehavior( )); Exception exc = null; TestBehavior behavior = null; try { behavior = behaviors.Get <TestBehavior>( ); } catch (Exception exc2) { exc = exc2; } Assert.IsNotNull(behavior); Assert.IsNull(exc); }
public async Task Publish_WithBehaviors_BehaviorsExecuted() { var behavior1 = new TestBehavior(); var behavior2 = new TestBehavior(); var behavior3 = new TestBehavior(); var serviceProvider = ServiceProviderHelper.GetServiceProvider( services => services .AddFakeLogger() .AddSilverback() .AddSingletonBehavior(behavior1) .AddScopedBehavior(_ => behavior2) .AddTransientBehavior(_ => behavior3)); var publisher = serviceProvider.GetRequiredService <IPublisher>(); publisher.Publish(new TestCommandOne()); await publisher.PublishAsync(new TestCommandOne()); behavior1.EnterCount.Should().Be(2); behavior2.EnterCount.Should().Be(2); behavior3.EnterCount.Should().Be(2); }