public void RemotelyCloseConnection(bool expectCloseResponse, Symbol errorCondition = null, string errorMessage = null) { lock (matchersLock) { var matcher = GetLastMatcher(); matcher.WithOnComplete(context => { var close = new Close { Error = new Error(errorCondition) { Description = errorMessage } }; context.SendCommand(close); }); if (expectCloseResponse) { // Expect a response to our Close. var closeMatcher = new FrameMatcher <Close>(); AddMatcher(closeMatcher); } } }
public void RejectConnect(Symbol errorType, string errorMessage) { // Expect a connection, establish through the SASL negotiation and sending of the Open frame Fields serverProperties = new Fields { { SymbolUtil.CONNECTION_ESTABLISH_FAILED, true } }; ExpectSaslAnonymous(); ExpectOpen(serverProperties: serverProperties); // Now generate the Close frame with the supplied error and update the matcher to send the Close frame after the Open frame. IMatcher lastMatcher = GetLastMatcher(); lastMatcher.WithOnComplete(context => { var close = new Close { Error = new Error(errorType) { Description = errorMessage } }; context.SendCommand(CONNECTION_CHANNEL, close); }); AddMatcher(new FrameMatcher <Begin>()); var closeMatcher = new FrameMatcher <Close>() .WithAssertion(close => Assert.IsNull(close.Error)); AddMatcher(closeMatcher); }
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 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 RemotelyCloseLastCoordinatorLink(bool expectDetachResponse, bool closed, Error error) { lock (matchersLock) { var matcher = GetLastMatcher(); matcher.WithOnComplete(context => { var detach = new Detach { Closed = true, Handle = lastInitiatedCoordinatorLinkHandle }; if (error != null) { detach.Error = error; } context.SendCommand(detach); }); if (expectDetachResponse) { var detachMatcher = new FrameMatcher <Detach>().WithAssertion(detach => Assert.AreEqual(closed, detach.Closed)); AddMatcher(detachMatcher); } } }
public void ExpectClose() { var closeMatcher = new FrameMatcher <Close>() .WithAssertion(close => Assert.IsNull(close.Error)) .WithOnComplete(context => context.SendCommand(new Close())); AddMatcher(closeMatcher); }
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 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); }
private void ExpectSaslAuthentication(Symbol mechanism, Action <byte[]> initialResponseMatcher) { var headerMatcher = new HeaderMatcher(AmqpHeader.SASL_HEADER, AmqpHeader.SASL_HEADER) .WithOnComplete(context => context.SendCommand(new SaslMechanisms { SaslServerMechanisms = new[] { mechanism } })); AddMatcher(headerMatcher); var saslInitMatcher = new FrameMatcher <SaslInit>() .WithAssertion(saslInit => Assert.AreEqual(mechanism, saslInit.Mechanism)) .WithAssertion(saslInit => initialResponseMatcher(saslInit.InitialResponse)) .WithOnComplete(context => { context.SendCommand(new SaslOutcome { Code = SaslCode.Ok }, FrameType.Sasl); }) .WithShouldContinue(false); AddMatcher(saslInitMatcher); }
public void ExpectSenderAttach(Action <Source> sourceMatcher, Action <Target> targetMatcher, uint creditAmount = 100, bool senderSettled = false) { var attachMatcher = new FrameMatcher <Attach>() .WithAssertion(attach => Assert.IsNotNull(attach.LinkName)) .WithAssertion(attach => Assert.AreEqual(attach.Role, Role.SENDER)) .WithAssertion(attach => Assert.AreEqual(senderSettled ? SenderSettleMode.Settled : SenderSettleMode.Unsettled, attach.SndSettleMode)) .WithAssertion(attach => Assert.AreEqual(attach.RcvSettleMode, ReceiverSettleMode.First)) .WithAssertion(attach => sourceMatcher(attach.Source as Source)) .WithAssertion(attach => targetMatcher(attach.Target as Target)) .WithOnComplete(context => { var attach = new Attach() { Role = Role.RECEIVER, OfferedCapabilities = null, SndSettleMode = senderSettled ? SenderSettleMode.Settled : SenderSettleMode.Unsettled, RcvSettleMode = ReceiverSettleMode.First, Handle = context.Command.Handle, LinkName = context.Command.LinkName, Source = context.Command.Source, Target = context.Command.Target }; lastInitiatedLinkHandle = context.Command.Handle; context.SendCommand(attach); var flow = new Flow() { NextIncomingId = 1, IncomingWindow = 2048, NextOutgoingId = 1, OutgoingWindow = 2024, LinkCredit = creditAmount, Handle = context.Command.Handle, DeliveryCount = context.Command.InitialDeliveryCount, }; context.SendCommand(flow); }); AddMatcher(attachMatcher); }
public void RemotelyDetachLastOpenedLinkOnLastOpenedSession(bool expectDetachResponse, bool closed, Symbol errorType, string errorMessage, int delayBeforeSend = 0) { lock (matchers) { IMatcher lastMatcher = GetLastMatcher(); lastMatcher.WithOnComplete(context => { Detach detach = new Detach() { Closed = closed, Handle = lastInitiatedLinkHandle }; if (errorType != null) { detach.Error = new Error(errorType) { Description = errorMessage, }; } //Insert a delay if requested if (delayBeforeSend > 0) { Thread.Sleep(delayBeforeSend); } context.SendCommand(lastInitiatedChannel, detach); }); if (expectDetachResponse) { // Expect a response to our Detach. var detachMatcher = new FrameMatcher <Detach>() .WithAssertion(detach => Assert.AreEqual(closed, detach.Closed)); // TODO: enable matching on the channel number of the response. AddMatcher(detachMatcher); } } }
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 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); }
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 ExpectSenderAttach(Action <object> sourceMatcher, Action <object> targetMatcher, bool refuseLink = false, uint creditAmount = 100, bool senderSettled = false, Error error = null) { var attachMatcher = new FrameMatcher <Attach>() .WithAssertion(attach => Assert.IsNotNull(attach.LinkName)) .WithAssertion(attach => Assert.AreEqual(attach.Role, Role.SENDER)) .WithAssertion(attach => Assert.AreEqual(senderSettled ? SenderSettleMode.Settled : SenderSettleMode.Unsettled, attach.SndSettleMode)) .WithAssertion(attach => Assert.AreEqual(attach.RcvSettleMode, ReceiverSettleMode.First)) .WithAssertion(attach => sourceMatcher(attach.Source)) .WithAssertion(attach => targetMatcher(attach.Target)) .WithOnComplete(context => { var attach = new Attach() { Role = Role.RECEIVER, OfferedCapabilities = null, SndSettleMode = senderSettled ? SenderSettleMode.Settled : SenderSettleMode.Unsettled, RcvSettleMode = ReceiverSettleMode.First, Handle = context.Command.Handle, LinkName = context.Command.LinkName, Source = context.Command.Source }; if (refuseLink) { attach.Target = null; } else { attach.Target = context.Command.Target; } lastInitiatedLinkHandle = context.Command.Handle; if (context.Command.Target is Coordinator) { lastInitiatedCoordinatorLinkHandle = context.Command.Handle; } context.SendCommand(attach); if (refuseLink) { var detach = new Detach { Closed = true, Handle = context.Command.Handle }; if (error != null) { detach.Error = error; } context.SendCommand(detach); } else { var flow = new Flow { NextIncomingId = 1, IncomingWindow = 2048, NextOutgoingId = 1, OutgoingWindow = 2024, LinkCredit = creditAmount, Handle = context.Command.Handle, DeliveryCount = context.Command.InitialDeliveryCount, }; context.SendCommand(flow); } }); AddMatcher(attachMatcher); }