title: integration tests with kafka in c#
body: <p>I have a library to publish and consume messages from kafka and I am trying to do integration tests on it, but I have a big issue as the consumer &quot;does not&quot; connect properly.</p>
  <p>So I reduced (for the test) to the next consumer:</p>
  <pre><code>private void FakeConsumer(string kafkaServer)
  {
      var config = GetSettings(kafkaServer);
      using (var c = new ConsumerBuilder&lt;Ignore, string&gt;(config).Build())
      {
          c.Subscribe(Topic);
  
          CancellationTokenSource cts = new CancellationTokenSource();
          Console.CancelKeyPress += (_, e) =&gt;
          {
              e.Cancel = true; 
              cts.Cancel();
          };
  
          try
          {
              while (true)
              {
                  try
                  {
                      var cr = c.Consume(cts.Token);
  
                      Console.WriteLine($&quot;Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.&quot;);
                  }
                  catch (ConsumeException e)
                  {
                      Console.WriteLine($&quot;Error occurred: {e.Error.Reason}&quot;);
  
                  }
              }
          }
          catch (OperationCanceledException)
          {
              // Ensure the consumer leaves the group cleanly and final offsets are committed.
              c.Close();
          }
      }
  }
  </code></pre>
  <p>then in my integration test I ran it as background thread as you can see:</p>
  <pre><code>[Fact]
  public async Task Test()
  {
      new Thread(() =&gt; FakeConsumer()) { IsBackground = true }.Start();
  
      Thread.Sleep(5000);
  
      Publisher&lt;Event&gt; publisher = new Publisher&lt;Event&gt;(args);
  
      Event ev = new Event();
      await publisher.PublishAsync(ev, Topic);
      Thread.Sleep(10000);
      
      Assert.Single(MessageHandler.Messages);
  }
  </code></pre>
  <p>Note: I removed it for the post, but the code has a &quot;messagehandler&quot; which Stores in memory the read message.</p>
  <p>I also added some <code>Thread.Sleep</code> on the main test so the topic gets created and there is no &quot;overlaping&quot; between both actions.</p>
  <p>the publish code <code>publisher.PublishAsync</code> works perfectly as I can see the message in kafdrop.</p>
  <p>The issue is that the code never passes <code>var cr = c.Consume(cts.Token);</code> the thread is stuck in there.</p>
  <p>What should I do to receive the information or to allow the BackgroundTrhead to receive the information in <code>var cr = c.Consume(cts.Token);</code>?</p>
  <p>Final note: if I split the code into two different console applications it works.</p>
  <p>Thanks.</p>