Created
March 6, 2011 04:43
-
-
Save jasonsirota/857038 to your computer and use it in GitHub Desktop.
use ManualResetEvent to block a current thread while waiting for a callback unit test
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
private const string key = "hello"; | |
private const string stringValue = "world!"; | |
private Exception exb; | |
private readonly KetchupClient cli = new KetchupClient(BuildConfiguration(), "default"); | |
private readonly ManualResetEvent mre = new ManualResetEvent(false); | |
[Fact] | |
public void SetWithSuccess() { | |
//have to do this for async operations in unit test framework | |
Reset(); | |
cli.Set(key, stringValue, | |
() => { | |
mre.Set(); | |
}, | |
ex => { | |
exb = ex; | |
mre.Set(); | |
}); | |
//have to block to wait for the async op to complete, otherwise method returns pass; | |
Wait(); | |
} | |
private void Reset() { | |
mre.Reset(); | |
exb = null; | |
} | |
private void Wait() { | |
if(!mre.WaitOne(5*1000)) | |
throw new TimeoutException("Operation has timed out with no response"); | |
if (exb != null) | |
throw exb; | |
} |
That would work too :)
Jason
here's an example test from my project with the call to AsyncTest, sexy.
public void IncrWithSuccess() {
DeleteWithSuccess();
TestAsync((success, fail) => {
long initial = 20;
long step = 8;
var result = initial + step;
cli.IncrDecr(key: key, initial: initial, expiration: new TimeSpan(1, 0, 0), success: v => {
Assert.Equal(initial, v);
}, error: fail)
.IncrDecr(key: key, step: step, success: v1 => {
Assert.Equal(result, v1);
success();
}, error: fail);
});
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about splitting the asynchronous part into a helper method (you could put it in an
AsynchronousTest
base class)?Usage:
The usage in your code will be even simpler, as your own method consumes an
Action
and anAction<Exception>
directly.