Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created July 26, 2010 17:12
Show Gist options
  • Save follesoe/490854 to your computer and use it in GitHub Desktop.
Save follesoe/490854 to your computer and use it in GitHub Desktop.
[TestClass]
public class SomeTest
{
[TestMethod]
public void Test_threaded_code()
{
var db = new SomeDbMock();
var bl = new SomeCode(db);
bl.DoSomething();
Assert.IsTrue(TestHelper.Run(() => db.TextSaved == "Hello World!", 500), "Text never became Hello World!");
}
}
public class TestHelper
{
public static bool Run(Func<bool> check, int timeout)
{
var sw = new Stopwatch();
sw.Start();
while(true)
{
if (sw.ElapsedMilliseconds > timeout)
return false;
if (check()) return true;
}
}
}
public class SomeDbMock
{
public string TextSaved;
public void SaveText(string text)
{
TextSaved = text;
}
}
public class SomeCode
{
private SomeDbMock _db;
public SomeCode(SomeDbMock mock)
{
_db = mock;
}
public void DoSomething()
{
var t = new Thread(DoSomethingOnThread);
t.Start();
}
private void DoSomethingOnThread()
{
Thread.Sleep(250);
_db.SaveText("Hello World!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment