Skip to content

Instantly share code, notes, and snippets.

@jasonsirota
Created March 6, 2011 04:43
Show Gist options
  • Save jasonsirota/857038 to your computer and use it in GitHub Desktop.
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
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;
}
@jasonsirota
Copy link
Author

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