public void OnFlow_CancelsPreviousTask_WhenCalledMultipleTimes()
        {
            ListenerLink   link        = Construct.Uninitialized <ListenerLink>();
            FlowContext    flowContext = Construct.ForPrivate <FlowContext>(link, 1, new Fields());
            IDeliveryQueue queue       = Substitute.For <IDeliveryQueue>();
            var            endpoint    = new OutgoingLinkEndpoint(queue);

            endpoint.OnFlow(flowContext);

            Should.NotThrow(() => endpoint.OnFlow(flowContext));
        }
        public void OnLinkClosed_CancelsSendingMessages()
        {
            var            backingQueue = new BlockingCollection <Message>(new ConcurrentQueue <Message>());
            ListenerLink   link         = Construct.Uninitialized <ListenerLink>();
            FlowContext    flowContext  = Construct.ForPrivate <FlowContext>(link, 1, new Fields());
            IDeliveryQueue queue        = Substitute.For <IDeliveryQueue>();

            queue.Dequeue(Arg.Any <CancellationToken>())
            .Returns(ci => backingQueue.Take(ci.Arg <CancellationToken>()));
            var endpoint = new OutgoingLinkEndpoint(queue);

            endpoint.OnFlow(flowContext);
            endpoint.OnLinkClosed(null, null);
            backingQueue.Add(new Message());
            Thread.Sleep(10);

            backingQueue.Count.ShouldBe(1);
        }
        public async Task OnFlow_StartsSendingMessagesAsynchronously()
        {
            var            backingQueue = new BlockingCollection <Message>(new ConcurrentQueue <Message>());
            ListenerLink   link         = Construct.Uninitialized <ListenerLink>();
            FlowContext    flowContext  = Construct.ForPrivate <FlowContext>(link, 1, new Fields());
            IDeliveryQueue queue        = Substitute.For <IDeliveryQueue>();

            queue.Dequeue(Arg.Any <CancellationToken>())
            .Returns(ci => backingQueue.Take(ci.Arg <CancellationToken>()));
            var endpoint = new OutgoingLinkEndpoint(queue);

            endpoint.OnFlow(flowContext);
            backingQueue.Add(new Message());

            await Task.Delay(500);

            queue.Received(1).Dequeue(Arg.Any <CancellationToken>());
            backingQueue.ShouldBeEmpty();
        }