public void ExpectDetach(bool expectClosed, bool sendResponse, bool replyClosed, Symbol errorType = null, String errorMessage = null) { var detachMatcher = new FrameMatcher <Detach>() .WithAssertion(detach => Assert.AreEqual(expectClosed, detach.Closed)); if (sendResponse) { detachMatcher.WithOnComplete(context => { var detach = new Detach { Closed = replyClosed, Handle = context.Command.Handle }; if (errorType != null) { detach.Error = new Error(errorType) { Description = errorMessage }; } context.SendCommand(detach); }); } AddMatcher(detachMatcher); }
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); }
public void ExpectEnd(bool sendResponse = true) { var endMatcher = new FrameMatcher <End>(); if (sendResponse) { endMatcher.WithOnComplete(context => context.SendCommand(new End())); } AddMatcher(endMatcher); }
public void ExpectTempNodeCreationAttach(string dynamicAddress, Symbol nodeTypeCapability, bool sendResponse) { Action <Target> targetMatcher = target => { Assert.NotNull(target); Assert.IsNull(target.Address); Assert.IsTrue(target.Dynamic); Assert.AreEqual((uint)TerminusDurability.NONE, target.Durable); Assert.AreEqual(TerminusExpiryPolicy.LINK_DETACH, target.ExpiryPolicy); Assert.IsTrue(target.DynamicNodeProperties.ContainsKey(SymbolUtil.ATTACH_DYNAMIC_NODE_PROPERTY_LIFETIME_POLICY)); CollectionAssert.Contains(target.Capabilities, nodeTypeCapability); }; var attachMatcher = new FrameMatcher <Attach>() .WithAssertion(attach => Assert.NotNull(attach.LinkName)) .WithAssertion(attach => Assert.AreEqual(Role.SENDER, attach.Role)) .WithAssertion(attach => Assert.AreEqual(SenderSettleMode.Unsettled, attach.SndSettleMode)) .WithAssertion(attach => Assert.AreEqual(ReceiverSettleMode.First, attach.RcvSettleMode)) .WithAssertion(attach => Assert.NotNull(attach.Source)) .WithAssertion(attach => targetMatcher(attach.Target as Target)); if (sendResponse) { attachMatcher.WithOnComplete(context => { lastInitiatedLinkHandle = context.Command.Handle; var target = CreateTargetObjectFromDescribedType(context.Command.Target); target.Address = dynamicAddress; var attach = new Attach() { Role = Role.RECEIVER, SndSettleMode = SenderSettleMode.Unsettled, RcvSettleMode = ReceiverSettleMode.First, Handle = context.Command.Handle, LinkName = context.Command.LinkName, Source = context.Command.Source, Target = target }; context.SendCommand(attach); }); Target CreateTargetObjectFromDescribedType(object o) => o is Target target ? target : new Target(); } AddMatcher(attachMatcher); }
public void ExpectTransfer(Action <Amqp.Message> messageMatcher, Action <DeliveryState> stateMatcher, bool settled, bool sendResponseDisposition, DeliveryState responseState, bool responseSettled, int dispositionDelay = 0, bool batchable = false ) { var transferMatcher = new FrameMatcher <Transfer>() .WithAssertion(transfer => Assert.AreEqual(settled, transfer.Settled)) .WithAssertion(transfer => stateMatcher(transfer.State)) .WithAssertion(transfer => Assert.AreEqual(batchable, transfer.Batchable)) .WithAssertion(messageMatcher); if (sendResponseDisposition) { transferMatcher.WithOnComplete(context => { if (dispositionDelay > 0) { Thread.Sleep(dispositionDelay); } var dispose = new Dispose() { Role = Role.RECEIVER, Settled = responseSettled, State = responseState, First = context.Command.DeliveryId, }; context.SendCommand(dispose); }); } AddMatcher(transferMatcher); }
public void ExpectBegin(int nextOutgoingId = 1, bool sendResponse = true) { var frameMatcher = new FrameMatcher <Begin>() .WithAssertion(begin => Assert.AreEqual(nextOutgoingId, begin.NextOutgoingId)) .WithAssertion(begin => Assert.NotNull(begin.IncomingWindow)); if (sendResponse) { frameMatcher.WithOnComplete(context => { var begin = new Begin { RemoteChannel = context.Channel, NextOutgoingId = 1, IncomingWindow = 0, OutgoingWindow = 0 }; context.SendCommand(begin); lastInitiatedChannel = context.Channel; }); } AddMatcher(frameMatcher); }
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); }
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); }