public void PerformTest() { // Arrange int count = 0; TriggerListener listener = new TriggerListener(); Trigger trigger = listener.CreateTrigger(); // Act & assert (hasn't fired yet) listener.SetContinuation( () => { count++; } ); listener.Activate(); Assert.Equal(0, count); // Act & assert (fire it, get the callback) trigger.Fire(); Assert.Equal(1, count); // Act & assert (fire again, but no callback since it already fired) Trigger trigger2 = listener.CreateTrigger(); trigger2.Fire(); Assert.Equal(1, count); }
public override IAsyncResult BeginExecute(ControllerContext context, IDictionary <string, object> parameters, AsyncCallback callback, object state) { Precondition.Require(context, () => Error.ArgumentNull("context")); Precondition.Require(parameters, () => Error.ArgumentNull("parameters")); AsyncManager asyncManager = GetAsyncManager(context.Controller); BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { object[] parameterValues = _entryMethod.GetParameters() .Select(p => ExtractParameter(p, parameters, _entryMethod)) .ToArray(); TriggerListener listener = new TriggerListener(); MvcAsyncResult asyncResult = new MvcAsyncResult(asyncState); Trigger finishTrigger = listener.CreateTrigger(); asyncManager.Finished += delegate { finishTrigger.Fire(); }; asyncManager.OutstandingOperations.Increment(); listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ => asyncResult.MarkCompleted(false, asyncCallback))); DispatcherCache.GetDispatcher(_entryMethod) .Execute(context.Controller, parameterValues); asyncManager.OutstandingOperations.Decrement(); listener.Activate(); return(asyncResult); }; EndInvokeDelegate <object> endDelegate = delegate(IAsyncResult asyncResult) { object[] parameterValues = _completedMethod.GetParameters() .Select(p => ExtractParameter(p, parameters, _completedMethod)) .ToArray(); return(DispatcherCache.GetDispatcher(_completedMethod) .Execute(context.Controller, parameterValues)); }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeTag, asyncManager.Timeout)); }
public void PerformTest() { // Arrange int count = 0; TriggerListener listener = new TriggerListener(); Trigger trigger = listener.CreateTrigger(); // Act & assert 1 listener.SetContinuation(() => { count++; }); listener.Activate(); Assert.AreEqual(0, count, "Callback shouldn't have been executed."); // Act & assert 2 trigger.Fire(); Assert.AreEqual(1, count, "Callback should've been called once."); // Act & assert 3 Trigger trigger2 = listener.CreateTrigger(); trigger2.Fire(); Assert.AreEqual(1, count, "Callback should only be called once."); }
public void PerformTest() { // Arrange int count = 0; TriggerListener listener = new TriggerListener(); Trigger trigger = listener.CreateTrigger(); // Act & assert (hasn't fired yet) listener.SetContinuation(() => { count++; }); listener.Activate(); Assert.Equal(0, count); // Act & assert (fire it, get the callback) trigger.Fire(); Assert.Equal(1, count); // Act & assert (fire again, but no callback since it already fired) Trigger trigger2 = listener.CreateTrigger(); trigger2.Fire(); Assert.Equal(1, count); }
public void PerformTest() { // Arrange int count = 0; TriggerListener listener = new TriggerListener(); Trigger trigger = listener.CreateTrigger(); // Act & assert 1 listener.SetContinuation(() => { count++; }); listener.Activate(); Assert.AreEqual(0, count, "Callback shouldn't have been executed."); // Act & assert 2 trigger.Fire(); Assert.AreEqual(1, count, "Callback should've been called once."); // Act & assert 3 Trigger trigger2 = listener.CreateTrigger(); trigger2.Fire(); Assert.AreEqual(1, count, "Callback should only be called once."); }