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 7398367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:51:40+00:00 2026-05-29T03:51:40+00:00

If I have an subject under test with a timer that causes some action

  • 0

If I have an subject under test with a timer that causes some action to be taken at a timed interval, what’s a good way to test that?

One method is to wrap the timer in an interface and inject it as a dependency.

However, I’d like to avoid creating yet another abstraction. It seems I can avoid that by injecting the update interval rather than the timer. Then in my test (assuming the AAA style of testing), I put a Thread.Sleep after Act and before Assert, using a very small time value so the test doesn’t take long to run.

Is that a bad idea? I know it probably doesn’t fully follow the principles of TDD, but it seems like there has to be a line where you stop surrounding everything with a contract and injecting it.

  • 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-29T03:51:41+00:00Added an answer on May 29, 2026 at 3:51 am

    If the amount you sleep doesn’t have any significance on the test and you can set i to 1 millisecond then is should be fine to simply sleep for 1 millisecond in your test.

    However, if you want to test complex timing behavior with timeouts and specific actions being taken at specific points in time it quickly becomes easier to abstract the concept of time and inject it as a dependency. Then your tests can operate in virtual time and execute without delay even though the code operates as if real time was passing.

    A simple way to virtualize time is to use something like this:

    interface ITimeService {
    
      DateTime Now { get; }
    
      void Sleep(TimeSpan delay);
    
    }
    
    class TimeService : ITimeService {
    
      public DateTime Now { get { return DateTime.UtcNow; } }
    
      public void Sleep(TimeSpan delay) { Thread.Sleep(delay); }
    
    }
    
    class TimeServiceStub : ITimeService {
    
      DateTime now;
    
      public TimeServiceStub() {
        this.now = DateTime.UtcNow;
      }
    
      public DateTime Now { get { return this.now; } }
    
      public void Sleep(TimeSpan delay) {
        this.now += delay;
      }
    
    }
    

    You will have to extend this idea if you require more reactive behavior like timers firing etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some VSTO (2003) code that sucessfully applies some mandatory subject line
I have found some info on the subject ( like this link) , but
I have a simple contact form with Subject and Message that I want to
I have a problem finding references to this subject and would appreciate some help.
I have a datatable. I am populating some values into that. e.g. DataTable dt
We have an old (written in CakePHP 1.1!) application that has started groaning under
Sometimes I feel the desire of mocking the subject under test itself, I know
i wish to have a simple Action in my controller that accepts a few
I have an email subject of the form: =?utf-8?B?T3.....?= The body of the email
I have two tables: subject and student. I'm trying to count the number of

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.