0

We've got a large C# solution with multiple APIs, SVCs and so on. Usual sort of enterprisy mess that you get after the same code has been worked on for years by multiple people.

Anyway! We have an ability to call an external service and we have some unit tests in place that use a Moq like stub implementation of the services interface.

It so happens that there can be a large delay in calling the external service and it's not anything that we can control (it's a GDS interface).

We've been working on a way to streamline the user experience for this part of our platform.

The problem is, the stub doesn't actually do much at all - and of course, is lightening fast, compared to the real thing.

We want to introduce a random delay into one of the stubbed methods, that will cause the call to take between 10 and 20 seconds to complete.

The naive approach is to do:

int sleepTimer = random.Next(10, 20);
Thread.Sleep(sleepTimer * 1000);

But something about this gives me a bad feeling. What other ways do people have of solving this kind of scenario, or is Thread.Sleep actually Ok to use in this context ?

Thanks for your time!

-Russ

Edit, To answer some of the comments:

Basically, we don't want to call the live external service from our test suite, because it costs money and other business problems.

However, we want to test that our new processes work well, even when there's a variable delay in this essential call to the external service.

I would love to explain the exact process, but I'm not allowed to.

But yeah, the summary is that our test needs to ensure that a long running call to an external service doesn't obstruct the rest of the flow; and we need to ensure that other tasks don't get into any kind of race conditions, as they depend on the result of this call.

I agree that calling it a unit-test is somewhat incorrect now!

9
  • well first: you would not call this an unit test any longer - why don't you instead try it once and find out what kind of exception/return you are getting (if you don't know already) and then use the stub to throw this at once?
    – Random Dev
    Apr 22, 2015 at 15:02
  • 1
    What are you trying to do? Why do you even need a delay in unit testing? I'm confused. Apr 22, 2015 at 15:04
  • btw: no I would avoid a sleep in my tests at all cost - even if this means to abstract System.DateTime and stuff away - if not it's a guaranteed brittle test that people are not willing to run often (a single test takes >20sek to run ... how often will you run it?)
    – Random Dev
    Apr 22, 2015 at 15:04
  • Definitely don't do random. Set up multiple tests (not really unit) and specify the seconds. You don't want to ever have your tests fail once, then magically work, and then fail again because of a random value.
    – TyCobb
    Apr 22, 2015 at 15:10
  • 3
    you cannot really test race-conditions with unit-tests
    – Random Dev
    Apr 22, 2015 at 15:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.