title: Integration Test to publish to a topic and subscribe to another in Azure Service Bus is unreliable is there a race condition?
body: <p>I am trying to write an integration / acceptance test to test some code in azure, the code in the question ATM simply subscribes to one topic and publishes to another.</p>
  
  <p>I have written the test but it doesn't always pass, seems as though there could be a race condition in place.  I've tried writing it a couple of ways including using OnMessage and also using Receive (example I show here).</p>
  
  <p>When using OnMessage the test seemed to always exit prematurely (around 30 seconds), which I guess perhaps means its inappropriate for this test anyway.</p>
  
  <p>My query concerning my example specifically, I assumed that once I created the subscription to the target topic, that any message sent to it I would be able to pickup using Receive(), whatever point in time that message arrived meaning, if the message arrives at the target topic before I call Receive(), I would still be able to read the message afterward by calling Receive().  Could anyone please shed any light on this?</p>
  
  <pre><code>    namespace somenamespace {
      [TestClass]
      public class SampleTopicTest
      {
          private static TopicClient topicClient;
          private static SubscriptionClient subClientKoEligible;
          private static SubscriptionClient subClientKoIneligible;
  
          private static OnMessageOptions options;
          public const string TEST_MESSAGE_SUB = "TestMessageSub";
          private static NamespaceManager namespaceManager;
  
          private static string topicFleKoEligible;
          private static string topicFleKoIneligible;
  
          private BrokeredMessage message;
  
          [ClassInitialize]
          public static void BeforeClass(TestContext testContext)
          {
              //client for publishing messages
              string connectionString = ConfigurationManager.AppSettings["ServiceBusConnectionString"];
              string topicDataReady = ConfigurationManager.AppSettings["DataReadyTopicName"];
              topicClient = TopicClient.CreateFromConnectionString(connectionString, topicDataReady);
  
              topicFleKoEligible = ConfigurationManager.AppSettings["KnockOutEligibleTopicName"];
              topicFleKoIneligible = ConfigurationManager.AppSettings["KnockOutIneligibleTopicName"];
  
              //create test subscription to receive messages
              namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
  
  
              if (!namespaceManager.SubscriptionExists(topicFleKoEligible, TEST_MESSAGE_SUB))
              {
                  namespaceManager.CreateSubscription(topicFleKoEligible, TEST_MESSAGE_SUB);
              }
  
              if (!namespaceManager.SubscriptionExists(topicFleKoIneligible, TEST_MESSAGE_SUB))
              {
                  namespaceManager.CreateSubscription(topicFleKoIneligible, TEST_MESSAGE_SUB);
              }
  
              //subscriber client koeligible
              subClientKoEligible = SubscriptionClient.CreateFromConnectionString(connectionString, topicFleKoEligible, TEST_MESSAGE_SUB);
  
              subClientKoIneligible = SubscriptionClient.CreateFromConnectionString(connectionString, topicFleKoIneligible, TEST_MESSAGE_SUB);
  
              options = new OnMessageOptions()
              {
                  AutoComplete = false,
                  AutoRenewTimeout = TimeSpan.FromMinutes(1),
  
              };
          }
  
            [TestMethod]
          public void E2EPOCTopicTestLT50()
          {
              Random rnd = new Random();
              string customerId = rnd.Next(1, 49).ToString();
  
              FurtherLendingCustomer sentCustomer = new FurtherLendingCustomer { CustomerId = customerId };
              BrokeredMessage sentMessage = new BrokeredMessage(sentCustomer.ToJson());           
              sentMessage.CorrelationId = Guid.NewGuid().ToString();
              string messageId = sentMessage.MessageId;
              topicClient.Send(sentMessage);
  
              Boolean messageRead = false;
  
              //wait for message to arrive on the ko eligible queue
              while((message = subClientKoEligible.Receive(TimeSpan.FromMinutes(2))) != null){
  
                  //read message
                  string messageString = message.GetBody&lt;String&gt;();
  
                  //Serialize
                  FurtherLendingCustomer receivedCustomer =  JsonConvert.DeserializeObject&lt;FurtherLendingCustomer&gt;(messageString.Substring(messageString.IndexOf("{")));
  
                  //assertion
                  Assert.AreEqual(sentCustomer.CustomerId, receivedCustomer.CustomerId,"verify customer id");
  
                  //pop message
                  message.Complete();
                  messageRead = true;
  
                  //leave loop after processing one message
                  break;
              }
              if (!messageRead)
                  Assert.Fail("Didn't receive any message after 2 mins");
  
          }
      }
  }
  </code></pre>