Skip to content

Instantly share code, notes, and snippets.

@thefringeninja
Created February 3, 2015 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefringeninja/b736fa772b94b43d845d to your computer and use it in GitHub Desktop.
Save thefringeninja/b736fa772b94b43d845d to your computer and use it in GitHub Desktop.
Raven Async and Tests
public class RavenTestBase {
Task<IDocumentStore> _createDocumentStore;
ctor() {
var source = new TaskCompletionSource<IDocumentStore>();
_createDocumentStore = source.Task;
Task.Run(async () => {
var store = InitializeDocumentStore();
await CreateIndices(documentStore);
await Seed(documentStore);
// or, await Task.WhenAll(CreateIndices(documentStore), Seed(documentStore)); if there are nodependencies
try {
source.SetResult(documentStore);
}
catch (Exception ex){
source.SetException(ex);
}
});
}
virtual protected IDocumentStore InitializeDocumentStore() {
return new EmbeddedDocumentStore{RunInMemory=true}.Initialize();
}
Task CreateIndices(IDocumentStore documentStore){
}
Task Seed(IDocumentStore documentStore) {
}
}
public class NewFacts {
[Fact]
public async Task should_be_orwellian() {
var store = await _createDocumentStore;
Assert.IsTrue(false);
}
}
@PureKrome
Copy link

quick question :) I don't understand the connection between this :

var source = new TaskCompletionSource<IDocumentStore>();
_createDocumentStore = source.Task;

and this

var store = await _createDocumentStore;

variables that are private are usually prepended with an _ .. so in the test/fact, you're trying to access a private variable (which is out of scope). I also don't understand why you would expose a Task as the result, instead of the IDocumentStore. Otherwise, we can just expose a method that does all the above, instead of the RESULT of the method (which is what I'm trying to expose).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment