Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 964605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:51:21+00:00 2026-05-16T01:51:21+00:00

I have to test a method which does a certain amount of work after

  • 0

I have to test a method which does a certain amount of work after an interval.

while (running)
{
    ...
    // Work
    ...
    Thread.Sleep(Interval);
}

Interval is passed in as a parameter to the class so I can just pass in 0 or 1 but I was interested as to how to mock the system clock if this wasn’t the case.

In my test I’d like to be able to simply set the time forward by TimeSpan Interval and have the thread wake up.

I’ve never written tests for code which acts upon the executing thread before and I’m sure there are some pitfalls to avoid – please feel free to elaborate on what approach you use.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:51:22+00:00Added an answer on May 16, 2026 at 1:51 am

    If you do not wish to test the fact that the thread actually sleeps, a more straightforward approach (and one that is possible) is to have an ISleepService. You can then mock this out, and then not sleep in your tests, but have an implementation that does cause a Thread.Sleep in your production code.

    ISleepService sleepService = Container.Resolve<ISleepService>();
    
    ..
    
    while (running)
    {
        ...
        // Work
        ...
        sleepService.Sleep(Interval);
    }
    

    Example using Moq:

        public interface ISleepService
        {
            void Sleep(int interval);
        }
    
        [Test]
        public void Test()
        {
            const int Interval = 1000;
    
            Mock<ISleepService> sleepService = new Mock<ISleepService>();
            sleepService.Setup(s => s.Sleep(It.IsAny<int>()));
            _container.RegisterInstance(sleepService.Object);
    
            SomeClass someClass = _container.Resolve<SomeClass>();
            someClass.DoSomething(interval: Interval);
    
            //Do some asserting.
    
            //Optionally assert that sleep service was called
            sleepService.Verify(s => s.Sleep(Interval));
        }
    
        private class SomeClass
        {
            private readonly ISleepService _sleepService;
    
            public SomeClass(IUnityContainer container)
            {
                _sleepService = container.Resolve<ISleepService>();
            }
    
            public void DoSomething(int interval)
            {
                while (true)
                {
                    _sleepService.Sleep(interval);
                    break;
                }
            }
        }
    

    Update

    On a design\maintenance note, if it is painful to change the constructor of “SomeClass”, or to add Dependency Injection points to the user of the class, then a service locator type pattern can help out here, e.g.:

    private class SomeClass
    {
        private readonly ISleepService _sleepService;
    
        public SomeClass()
        {
            _sleepService = ServiceLocator.Container.Resolve<ISleepService>();
        }
    
        public void DoSomething(int interval)
        {
            while (true)
            {
                _sleepService.Sleep(interval);
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.