Skip to content

Instantly share code, notes, and snippets.

@AnthonyMastrean
Created May 11, 2012 14:23
Show Gist options
  • Save AnthonyMastrean/2660000 to your computer and use it in GitHub Desktop.
Save AnthonyMastrean/2660000 to your computer and use it in GitHub Desktop.
Public static funcs wrapping system calls.
namespace System
{
public static class SystemDate
{
public static Func<DateTime> UtcNow { get; private set; }
static SystemDate()
{
Reset();
}
private static void Reset()
{
UtcNow = () => DateTime.UtcNow;
}
public static IDisposable SetUtcNow(Func<DateTime> replacement)
{
UtcNow = replacement;
return new DisposableAction(Reset);
}
}
}
namespace System.Threading
{
public static class SystemThread
{
public static Action<TimeSpan> Sleep { get; private set; }
static SystemThread()
{
Reset();
}
private static void Reset()
{
Sleep = timespan => Thread.Sleep(timespan);
}
public static IDisposable SetSleep(Action<TimeSpan> replacement)
{
Sleep = replacement;
return new DisposableAction(Reset);
}
}
}
using System;
using System.Threading;
using Machine.Specifications;
namespace Omnyx.Scanner.Specifications.Infrastructure
{
public class TestSystemThread : IAssemblyContext
{
public static TimeSpan Slept { get; private set; }
private IDisposable _reset;
public void OnAssemblyStart()
{
// Disable sleeping and stash off the timespan for easy assertions.
_reset = SystemThread.SetSleep(timespan => { Slept = timespan; });
}
public void OnAssemblyComplete()
{
_reset.Dispose();
Slept = TimeSpan.MinValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment