Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created November 3, 2010 20:28
Show Gist options
  • Save gamlerhart/661651 to your computer and use it in GitHub Desktop.
Save gamlerhart/661651 to your computer and use it in GitHub Desktop.
side-effect
public event Action<string> OperationFinishedNotification;
public string AwesomeBusinessOperation()
{
var firstPart = TakesALongTimeToProcess("Tons");
var secondPart = TakesALongTimeToProcess(firstPart + " of ");
var result = TakesALongTimeToProcess(secondPart + "money");
var eventToFire = OperationFinishedNotification;
if(null!=eventToFire)
{
OperationFinishedNotification(result);
}
return result;
}
private string TakesALongTimeToProcess(string word)
{
// This operation takes a while
Thread.Sleep(1000);
return word;
}
public event Action<string> OperationFinishedNotification;
public async Task<string> AwesomeBusinessOperationAsync()
{
var firstPart =await TakesALongTimeToProcess("Tons");
var secondPart = await TakesALongTimeToProcess(firstPart + " of ");
var result = await TakesALongTimeToProcess(secondPart + "money");
var eventToFire = OperationFinishedNotification;
if(null!=eventToFire)
{
OperationFinishedNotification(result);
}
return result;
}
private Task<string> TakesALongTimeToProcess(string word)
{
// Remember, this is just a simulation
// Usually you would use some other async API here
return TaskEx.Run(() =>
{
// This operation takes a while
Thread.Sleep(1000);
return word;
});
}
[Test]
public void ExpectEventToBeFired(){
var toTest = new MyBusinessLogic();
var eventFiredExpected = "";
toTest.OperationFinishedNotification
+= eventArgument =>
{
eventFiredExpected = eventArgument;
};
var result = toTest.AwesomeBusinessOperation();
Assert.AreEqual("Tons of money",eventFiredExpected);
}
[Test]
public void ExpectEventToBeFired(){
var toTest = new MyBusinessLogic();
var eventFiredExpected = "";
toTest.OperationFinishedNotification
+= eventArgument =>
{
eventFiredExpected = eventArgument;
};
var result = toTest.AwesomeBusinessOperationAsync();
// just wait for the asynchronous operation
result.Wait();
Assert.AreEqual("Tons of money",eventFiredExpected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment