Created
July 26, 2010 17:12
-
-
Save follesoe/490854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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