Beispiel #1
0
        public async Task Peek_Session(long?sequenceNumber, string partitionKey)
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true))
            {
                await using var sender = new QueueSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName);
                var messageCt = 10;
                var sessionId = Guid.NewGuid().ToString();

                // send the messages
                IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt, sessionId, partitionKey);
                await sender.SendRangeAsync(sentMessages);

                Dictionary <string, ServiceBusMessage> sentMessageIdToMsg = new Dictionary <string, ServiceBusMessage>();
                foreach (ServiceBusMessage message in sentMessages)
                {
                    sentMessageIdToMsg.Add(message.MessageId, message);
                }

                // peek the messages
                var sessionSettings = new SessionOptions()
                {
                    Connection = new ServiceBusConnection(
                        TestEnvironment.ServiceBusConnectionString,
                        scope.QueueName),
                    SessionId = sessionId
                };
                var receiver = new QueueReceiverClient(
                    TestEnvironment.ServiceBusConnectionString,
                    scope.QueueName,
                    sessionSettings);

                sequenceNumber ??= 1;
                IAsyncEnumerable <ServiceBusMessage> peekedMessages = receiver.PeekRangeBySequenceAsync(
                    fromSequenceNumber: (long)sequenceNumber,
                    maxMessages: messageCt);

                // verify peeked == send
                var ct = 0;
                await foreach (ServiceBusMessage peekedMessage in peekedMessages)
                {
                    var peekedText = Encoding.Default.GetString(peekedMessage.Body);
                    var sentMsg    = sentMessageIdToMsg[peekedMessage.MessageId];

                    sentMessageIdToMsg.Remove(peekedMessage.MessageId);
                    Assert.AreEqual(Encoding.Default.GetString(sentMsg.Body), peekedText);
                    Assert.AreEqual(sentMsg.PartitionKey, peekedMessage.PartitionKey);
                    Assert.IsTrue(peekedMessage.SystemProperties.SequenceNumber >= sequenceNumber);
                    TestContext.Progress.WriteLine($"{peekedMessage.Label}: {peekedText}");
                    ct++;
                }
                if (sequenceNumber == 1)
                {
                    Assert.AreEqual(messageCt, ct);
                }
            }
        }
Beispiel #2
0
        public async Task RoundRobinSessions()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true))
            {
                await using var sender = new QueueSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName);
                var messageCt             = 10;
                HashSet <string> sessions = new HashSet <string>()
                {
                    "1", "2", "3"
                };

                // send the messages
                foreach (string session in sessions)
                {
                    await sender.SendRangeAsync(GetMessages(messageCt, session));
                }

                // create receiver not scoped to a specific session
                for (int i = 0; i < 10; i++)
                {
                    var sessionSettings = new SessionOptions()
                    {
                        Connection = new ServiceBusConnection(
                            TestEnvironment.ServiceBusConnectionString,
                            scope.QueueName),
                        SessionId = null
                    };

                    var receiver = new QueueReceiverClient(
                        TestEnvironment.ServiceBusConnectionString,
                        scope.QueueName,
                        sessionSettings);
                    IAsyncEnumerable <ServiceBusMessage> peekedMessages = receiver.PeekRangeBySequenceAsync(
                        fromSequenceNumber: 1,
                        maxMessages: 10);

                    await foreach (ServiceBusMessage peekedMessage in peekedMessages)
                    {
                        var sessionId = await receiver.Session.GetSessionId();

                        Assert.AreEqual(sessionId, peekedMessage.SessionId);
                    }

                    // Close the receiver client when we are done with it. Since the sessionClient doesn't own the underlying connection, the connection remains open, but the session link will be closed.
                    await receiver.CloseAsync();
                }
            }
        }
Beispiel #3
0
        public async Task PeekMultipleSessions_ShouldThrow()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true))
            {
                await using var sender = new QueueSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName);
                var messageCt = 10;
                var sessionId = Guid.NewGuid().ToString();
                // send the messages
                IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt, sessionId);
                await sender.SendRangeAsync(sentMessages);

                var sessionSettings = new SessionOptions()
                {
                    Connection = new ServiceBusConnection(
                        TestEnvironment.ServiceBusConnectionString,
                        scope.QueueName),
                    SessionId = sessionId
                };
                var receiver1 = new QueueReceiverClient(
                    TestEnvironment.ServiceBusConnectionString,
                    scope.QueueName,
                    sessionSettings);
                var receiver2 = new QueueReceiverClient(
                    TestEnvironment.ServiceBusConnectionString,
                    scope.QueueName,
                    sessionSettings);
                Dictionary <string, ServiceBusMessage> sentMessageIdToMsg = new Dictionary <string, ServiceBusMessage>();

                // peek the messages
                IAsyncEnumerable <ServiceBusMessage> peekedMessages1 = receiver1.PeekRangeBySequenceAsync(
                    fromSequenceNumber: 1,
                    maxMessages: messageCt);
                IAsyncEnumerable <ServiceBusMessage> peekedMessages2 = receiver2.PeekRangeBySequenceAsync(
                    fromSequenceNumber: 1,
                    maxMessages: messageCt);
                await peekedMessages1.GetAsyncEnumerator().MoveNextAsync();

                Assert.That(async() => await peekedMessages2.GetAsyncEnumerator().MoveNextAsync(), Throws.Exception);
            }
        }