public void Enqueue_Transactions_Abort_Properly() { recreateQueue(); BinaryMessageEncodingBindingElement element = new BinaryMessageEncodingBindingElement(); MessageEncoder encoder = element.CreateMessageEncoderFactory().Encoder; MessageDeliveryFormatter formatter = new MessageDeliveryFormatter(new ConverterMessageDeliveryReaderFactory(encoder, typeof(IContract)), new ConverterMessageDeliveryWriterFactory(encoder, typeof(IContract))); MsmqMessageDeliveryQueue queue = new MsmqMessageDeliveryQueue(Config.TestQueuePath, formatter); SubscriptionEndpoint endpoint = new SubscriptionEndpoint(Guid.NewGuid(), "SubscriptionName", "http://localhost/test", "SubscriptionConfigName", typeof(IContract), new WcfProxyDispatcher(), new PassThroughMessageFilter()); MessageDelivery enqueued = new MessageDelivery(endpoint.Id, typeof(IContract), "PublishThis", "randomMessageData", 3, new MessageDeliveryContext()); Assert.IsNull(queue.Peek(TimeSpan.FromSeconds(1))); // make sure queue is null before starting // Enqueue, but abort transaction using (TransactionScope ts = new TransactionScope()) { queue.Enqueue(enqueued); } using (TransactionScope ts = new TransactionScope()) { MessageDelivery dequeued = queue.Dequeue(TimeSpan.FromSeconds(5)); Assert.IsNull(dequeued); } }
void testMessageDelivery <T>(string messageAction, object messageData) { Type interfaceType = typeof(T); BinaryMessageEncodingBindingElement element = new BinaryMessageEncodingBindingElement(); MessageEncoder encoder = element.CreateMessageEncoderFactory().Encoder; MessageDeliveryFormatter formatter = new MessageDeliveryFormatter(new ConverterMessageDeliveryReaderFactory(encoder, typeof(T)), new ConverterMessageDeliveryWriterFactory(encoder, typeof(T))); MsmqMessageDeliveryQueue queue = new MsmqMessageDeliveryQueue(Config.TestQueuePath, formatter); SubscriptionEndpoint endpoint = new SubscriptionEndpoint(Guid.NewGuid(), "SubscriptionName", "http://localhost/test", "SubscriptionConfigName", interfaceType, new WcfProxyDispatcher(), new PassThroughMessageFilter()); MessageDelivery enqueued = new MessageDelivery(endpoint.Id, interfaceType, messageAction, messageData, 3, new MessageDeliveryContext()); using (TransactionScope ts = new TransactionScope()) { queue.Enqueue(enqueued); ts.Complete(); } // Peek MessageDelivery dequeued = queue.Peek(TimeSpan.FromSeconds(30)); Assert.IsNotNull(dequeued); Assert.AreEqual(enqueued.Action, dequeued.Action); Assert.AreEqual(enqueued.SubscriptionEndpointId, dequeued.SubscriptionEndpointId); using (TransactionScope ts = new TransactionScope()) { // Pull for real dequeued = queue.Dequeue(TimeSpan.FromSeconds(30)); ts.Complete(); } Assert.IsNotNull(dequeued); Assert.AreEqual(enqueued.Action, dequeued.Action); Assert.AreEqual(enqueued.SubscriptionEndpointId, dequeued.SubscriptionEndpointId); // Should now be empty dequeued = queue.Peek(TimeSpan.FromSeconds(1)); Assert.IsNull(dequeued); }