Ejemplo n.º 1
0
        private void ExpectOpen(Symbol[] desiredCapabilities, Symbol[] serverCapabilities, Fields serverProperties)
        {
            var openMatcher = new FrameMatcher <Open>();

            if (desiredCapabilities != null)
            {
                openMatcher.WithAssertion(open => CollectionAssert.AreEquivalent(desiredCapabilities, open.DesiredCapabilities));
            }
            else
            {
                openMatcher.WithAssertion(open => Assert.IsNull(open.DesiredCapabilities));
            }

            openMatcher.WithOnComplete(context =>
            {
                var open = new Open
                {
                    ContainerId         = Guid.NewGuid().ToString(),
                    OfferedCapabilities = serverCapabilities,
                    Properties          = serverProperties
                };
                context.SendCommand(open);
            });

            AddMatcher(openMatcher);
        }
Ejemplo n.º 2
0
        public void ExpectDisposition(bool settled, Action <DeliveryState> stateMatcher, uint?firstDeliveryId = null, uint?lastDeliveryId = null)
        {
            var dispositionMatcher = new FrameMatcher <Dispose>()
                                     .WithAssertion(dispose => Assert.AreEqual(settled, dispose.Settled))
                                     .WithAssertion(dispose => stateMatcher(dispose.State));

            if (firstDeliveryId.HasValue)
            {
                dispositionMatcher.WithAssertion(dispose => Assert.AreEqual(firstDeliveryId.Value, dispose.First));
            }

            if (lastDeliveryId.HasValue)
            {
                dispositionMatcher.WithAssertion(dispose => Assert.AreEqual(lastDeliveryId.Value, dispose.Last));
            }

            AddMatcher(dispositionMatcher);
        }
Ejemplo n.º 3
0
        public void ExpectReceiverAttach(
            Action <string> linkNameMatcher,
            Action <Source> sourceMatcher,
            Action <Target> targetMatcher,
            bool settled                  = false,
            bool refuseLink               = false,
            Symbol errorType              = null,
            string errorMessage           = null,
            Source responseSourceOverride = null,
            Action <Symbol[]> desiredCapabilitiesMatcher = null)
        {
            var attachMatcher = new FrameMatcher <Attach>()
                                .WithAssertion(attach => linkNameMatcher(attach.LinkName))
                                .WithAssertion(attach => Assert.AreEqual(Role.RECEIVER, attach.Role))
                                .WithAssertion(attach => Assert.AreEqual(settled ? SenderSettleMode.Settled : SenderSettleMode.Unsettled, attach.SndSettleMode))
                                .WithAssertion(attach => Assert.AreEqual(ReceiverSettleMode.First, attach.RcvSettleMode))
                                .WithAssertion(attach => sourceMatcher(attach.Source as Source))
                                .WithAssertion(attach => targetMatcher(attach.Target as Target))
                                .WithOnComplete(context =>
            {
                var attach = new Attach()
                {
                    Role                 = Role.SENDER,
                    SndSettleMode        = SenderSettleMode.Unsettled,
                    RcvSettleMode        = ReceiverSettleMode.First,
                    InitialDeliveryCount = 0,
                    Handle               = context.Command.Handle,
                    LinkName             = context.Command.LinkName,
                    Target               = context.Command.Target,
                };

                if (refuseLink)
                {
                    attach.Source = null;
                }
                else if (responseSourceOverride != null)
                {
                    attach.Source = responseSourceOverride;
                }
                else
                {
                    attach.Source = context.Command.Source;
                }

                this.lastInitiatedLinkHandle = context.Command.Handle;

                context.SendCommand(attach);
            });

            if (desiredCapabilitiesMatcher != null)
            {
                attachMatcher.WithAssertion(attach => desiredCapabilitiesMatcher(attach.DesiredCapabilities));
            }

            if (refuseLink)
            {
                attachMatcher.WithOnComplete(context =>
                {
                    var detach = new Detach {
                        Closed = true, Handle = context.Command.Handle
                    };
                    context.SendCommand(detach);
                });
            }

            AddMatcher(attachMatcher);
        }
Ejemplo n.º 4
0
        public void ExpectLinkFlowRespondWithTransfer(
            Amqp.Message message,
            int count,
            bool drain,
            bool sendDrainFlowResponse,
            bool sendSettled,
            bool addMessageNumberProperty,
            Action <uint> creditMatcher,
            int?nextIncomingId)
        {
            if (nextIncomingId == null && count > 0)
            {
                Assert.Fail("The remote NextIncomingId must be specified if transfers have been requested");
            }

            FrameMatcher <Flow> flowMatcher = new FrameMatcher <Flow>()
                                              .WithAssertion(flow => Assert.AreEqual(drain, flow.Drain))
                                              .WithAssertion(flow => creditMatcher(flow.LinkCredit));

            if (nextIncomingId != null)
            {
                flowMatcher.WithAssertion(flow => Assert.AreEqual(nextIncomingId.Value, flow.NextIncomingId));
            }
            else
            {
                flowMatcher.WithAssertion(flow => Assert.GreaterOrEqual(flow.NextIncomingId, 0));
            }

            for (int i = 0; i < count; i++)
            {
                int    nextId      = nextIncomingId + i ?? i;
                byte[] deliveryTag = Encoding.UTF8.GetBytes("theDeliveryTag" + nextId);

                if (addMessageNumberProperty)
                {
                    if (message.ApplicationProperties == null)
                    {
                        message.ApplicationProperties = new ApplicationProperties();
                    }

                    message.ApplicationProperties[MESSAGE_NUMBER] = i;
                }

                if (message.Properties == null)
                {
                    message.Properties = new Properties();
                }

                message.Properties.MessageId = $"ID:{i.ToString()}";

                var messageId = message.Properties.MessageId;

                ByteBuffer payload = message.Encode();

                flowMatcher.WithOnComplete(context =>
                {
                    Logger.Debug($"Sending message {messageId}");

                    var transfer = new Transfer()
                    {
                        DeliveryId    = (uint)nextId,
                        DeliveryTag   = deliveryTag,
                        MessageFormat = 0,
                        Settled       = sendSettled,
                        Handle        = context.Command.Handle,
                    };

                    try
                    {
                        context.SendCommand(transfer, payload);
                    }
                    catch (Exception e)
                    {
                        Logger.Error($"Sending message {messageId} failed.");
                        throw;
                    }
                });
            }

            if (drain && sendDrainFlowResponse)
            {
                flowMatcher.WithOnComplete(context =>
                {
                    var flow = new Flow()
                    {
                        OutgoingWindow = 0,
                        IncomingWindow = uint.MaxValue,
                        LinkCredit     = 0,
                        Drain          = true,
                        Handle         = context.Command.Handle,
                        DeliveryCount  = context.Command.DeliveryCount + context.Command.LinkCredit,
                        NextOutgoingId = context.Command.NextIncomingId + (uint)count,
                        NextIncomingId = context.Command.NextOutgoingId
                    };

                    context.SendCommand(flow);
                });
            }

            AddMatcher(flowMatcher);
        }