public async Task ServiceBusSessionQueue_OrderGuaranteed()
        {
            using (var host = ServiceBusSessionsTestHelper.CreateHost <ServiceBusSessionsTestJobs1>(_nameResolver))
            {
                await host.StartAsync();

                _waitHandle1 = new ManualResetEvent(initialState: false);

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message1", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message2", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message3", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message4", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message5", "test-session1");

                Assert.True(_waitHandle1.WaitOne(SBTimeout));

                IEnumerable <LogMessage> logMessages = host.GetTestLoggerProvider().GetAllLogMessages();

                // filter out anything from the custom processor for easier validation.
                List <LogMessage> consoleOutput = logMessages.Where(m => m.Category == "Function.SBQueue1Trigger.User").ToList();

                Assert.True(consoleOutput.Count() == 5, ServiceBusSessionsTestHelper.GetLogsAsString(consoleOutput));

                int i = 1;
                foreach (LogMessage logMessage in consoleOutput)
                {
                    Assert.True(logMessage.FormattedMessage.StartsWith("message" + i++));
                }
            }
        }
        public async Task ServiceBusSessionQueue_DifferentHosts_DifferentSessions()
        {
            using (var host1 = ServiceBusSessionsTestHelper.CreateHost <ServiceBusSessionsTestJobs1>(_nameResolver, true))
                using (var host2 = ServiceBusSessionsTestHelper.CreateHost <ServiceBusSessionsTestJobs2>(_nameResolver, true))
                {
                    await host1.StartAsync();

                    await host2.StartAsync();

                    _waitHandle1 = new ManualResetEvent(initialState: false);
                    _waitHandle2 = new ManualResetEvent(initialState: false);

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message1", "test-session1");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message1", "test-session2");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message2", "test-session1");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message2", "test-session2");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message3", "test-session1");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message3", "test-session2");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message4", "test-session1");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message4", "test-session2");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message5", "test-session1");

                    await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message5", "test-session2");

                    Assert.True(_waitHandle1.WaitOne(SBTimeout));
                    Assert.True(_waitHandle2.WaitOne(SBTimeout));

                    IEnumerable <LogMessage> logMessages1   = host1.GetTestLoggerProvider().GetAllLogMessages();
                    List <LogMessage>        consoleOutput1 = logMessages1.Where(m => m.Category == "Function.SBQueue1Trigger.User").ToList();
                    Assert.NotEmpty(logMessages1.Where(m => m.Category == "CustomMessagingProvider" && m.FormattedMessage.StartsWith("Custom processor Begin called!")));
                    Assert.NotEmpty(logMessages1.Where(m => m.Category == "CustomMessagingProvider" && m.FormattedMessage.StartsWith("Custom processor End called!")));
                    IEnumerable <LogMessage> logMessages2   = host2.GetTestLoggerProvider().GetAllLogMessages();
                    List <LogMessage>        consoleOutput2 = logMessages2.Where(m => m.Category == "Function.SBQueue2Trigger.User").ToList();
                    Assert.NotEmpty(logMessages2.Where(m => m.Category == "CustomMessagingProvider" && m.FormattedMessage.StartsWith("Custom processor Begin called!")));
                    Assert.NotEmpty(logMessages2.Where(m => m.Category == "CustomMessagingProvider" && m.FormattedMessage.StartsWith("Custom processor End called!")));
                    char sessionId1 = consoleOutput1[0].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1];
                    foreach (LogMessage m in consoleOutput1)
                    {
                        Assert.Equal(sessionId1, m.FormattedMessage[m.FormattedMessage.Length - 1]);
                    }

                    char sessionId2 = consoleOutput2[0].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1];
                    foreach (LogMessage m in consoleOutput2)
                    {
                        Assert.Equal(sessionId2, m.FormattedMessage[m.FormattedMessage.Length - 1]);
                    }
                }
        }
        public async Task ServiceBusSessionQueue_SessionLocks()
        {
            using (var host = ServiceBusSessionsTestHelper.CreateHost <ServiceBusSessionsTestJobs1>(_nameResolver, true))
            {
                await host.StartAsync();

                _waitHandle1 = new ManualResetEvent(initialState: false);
                _waitHandle2 = new ManualResetEvent(initialState: false);

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message1", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message1", "test-session2");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message2", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message2", "test-session2");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message3", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message3", "test-session2");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message4", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message4", "test-session2");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message5", "test-session1");

                await ServiceBusSessionsTestHelper.WriteQueueMessage(_connectionString, _queueName, "message5", "test-session2");

                Assert.True(_waitHandle1.WaitOne(SBTimeout));
                Assert.True(_waitHandle2.WaitOne(SBTimeout));

                IEnumerable <LogMessage> logMessages1 = host.GetTestLoggerProvider().GetAllLogMessages();

                // filter out anything from the custom processor for easier validation.
                List <LogMessage> consoleOutput1 = logMessages1.Where(m => m.Category == "Function.SBQueue1Trigger.User").ToList();
                Assert.True(consoleOutput1.Count() == 10, ServiceBusSessionsTestHelper.GetLogsAsString(consoleOutput1));
                double seconsds = (consoleOutput1[5].Timestamp - consoleOutput1[4].Timestamp).TotalSeconds;
                Assert.True(seconsds > 90 && seconsds < 110, seconsds.ToString());
                for (int i = 0; i < consoleOutput1.Count(); i++)
                {
                    if (i < 5)
                    {
                        Assert.Equal(consoleOutput1[i].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1],
                                     consoleOutput1[0].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1]);
                    }
                    else
                    {
                        Assert.Equal(consoleOutput1[i].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1],
                                     consoleOutput1[5].FormattedMessage[consoleOutput1[0].FormattedMessage.Length - 1]);
                    }
                }
            }
        }