© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
LazyGuard

How to test this code?

I am trying to refactor some code in a background service that looks like this:

public class MyBackgroundService : BackgroundService {
    public MyBackgroundService(IKafkaConsumer consumer, IOptionMonithor<WorkerConfig> config, NotificationSender sender) 
    {
        _consumer = consumer;
        _config = config;
        _sender = sender;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) 
    {
        while(!stoppingToken.IsCancellationRequested) 
        {
            await ConsumeKafkaAndSendNotificationAsync();
            await Task.Delay(TimeSpan.FromMilliseconds(_config.CurrentValue.Delay), stoppingToken);
        }
    }
    
    private async Task ConsumeKafkaAndSendNotificationAsyncc()
    {
        var timer = new StopWatch();
        timer.Start();
        Message? message;
        do 
        {
           message  = _consumer.ConsumeMessage(); 
          // .....
          // do some work here on the message
          // .....
           await _sender.SendNotificationAsync(message);
        } while (
            message != null && timer.Elapsed < TimeSpan.FromMilliseconds(_config.CurrentValue.Delay)
        )
    }
}
public class MyBackgroundService : BackgroundService {
    public MyBackgroundService(IKafkaConsumer consumer, IOptionMonithor<WorkerConfig> config, NotificationSender sender) 
    {
        _consumer = consumer;
        _config = config;
        _sender = sender;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) 
    {
        while(!stoppingToken.IsCancellationRequested) 
        {
            await ConsumeKafkaAndSendNotificationAsync();
            await Task.Delay(TimeSpan.FromMilliseconds(_config.CurrentValue.Delay), stoppingToken);
        }
    }
    
    private async Task ConsumeKafkaAndSendNotificationAsyncc()
    {
        var timer = new StopWatch();
        timer.Start();
        Message? message;
        do 
        {
           message  = _consumer.ConsumeMessage(); 
          // .....
          // do some work here on the message
          // .....
           await _sender.SendNotificationAsync(message);
        } while (
            message != null && timer.Elapsed < TimeSpan.FromMilliseconds(_config.CurrentValue.Delay)
        )
    }
}


Complex work should usually be kept somewhere else outside of the loop of the backgroundService. However, things get hairy enough that some tests will help.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

how to test untestable code
C#CC# / help
3y ago
✅ How to optimize this code?
C#CC# / help
16mo ago
✅ How to modernize this code ?
C#CC# / help
2y ago
❔ How to design this code ?
C#CC# / help
3y ago