de li
de li

Reputation: 932

Unit Test event hub EventProcessor

I am currently learning the Microsoft event hub and I followed the sample code here. I changed the ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> events) method so that now after I get the EventData, I store it to a database. For now, I want to use unit test to test my code. In the test class I set up a sender and send the data to the event hub in a initialize method, and that part works perfectly. Then I create a new test method to test the receiver side. The code looks like this:

[TestMethod]
public void TestProcessEventsAsync()
{
    //Initialize the receiver, and run it
    Receiver receiver = new Receiver();
    Receiver r = new Receiver(eventHubName, connectionString);
    r.MessageProcessingWithPartitionDistribution();

    //check if the data is stored in the database
    ...
}

Here, I simply create a new instance of receiver and use it to process the data sent to the event hub. I only send three objectst to the event hub and two of them are using the same partition. The test failed and the output shows that not all partitions are initialized which means that the receiver is stopped before it has finished processing all the data.

I am guessing that, since the MessageProcessingWithPartitionDistribution() method divided the task into multiple threads, the unit test doesn't work in this way. So my question is how can I test this MessageProcessingWithPartitionDistribution() method with unit test?

Any help is appreciated!

Upvotes: 1

Views: 2538

Answers (1)

Dominic Betts
Dominic Betts

Reputation: 2331

To make sure that the EventProcessor shuts down cleanly before you check in the database in your test you should call:

r.UnregisterEventProcessor();

This should make sure that all pending messages are processed and the EventProcessor performs a controlled shutdown.

Upvotes: 1

Related Questions