public void Dispatcher_OnRegister_GivesValidToken() { Dispatcher dispatcher = new Dispatcher(); DispatchToken dispatchToken = dispatcher.Register(p => { }); Assert.IsTrue(dispatcher.HasRegistered(dispatchToken)); }
public void Dispatcher_WhileNotDispatching_RejectsWaitForRequests() { Dispatcher dispatcher = new Dispatcher(); DispatchToken dispatchToken = dispatcher.Register(p => { }); dispatcher.WaitFor(dispatchToken); }
/// <summary> /// Registers a callback to receive dispatched messages. /// </summary> /// <param name="callback"></param> /// <returns></returns> public DispatchToken Register(Action <IPayload> callback) { DispatchToken dispatchToken = new DispatchToken(); callbackRegistry.Add(dispatchToken, callback); return(dispatchToken); }
public void WaitFor(DispatchToken dispatchToken) { WaitFor(new List <DispatchToken>() { dispatchToken }); }
public void Dispatcher_OnDeregister_RemovesCallback() { Dispatcher dispatcher = new Dispatcher(); DispatchToken dispatchToken = dispatcher.Register(p => { }); dispatcher.Deregister(dispatchToken); Assert.IsFalse(dispatcher.HasRegistered(dispatchToken)); }
/// <summary> /// Removes a callback from the registry. /// </summary> /// <param name="dispatchToken"></param> public void Deregister(DispatchToken dispatchToken) { if (!HasRegistered(dispatchToken)) { throw new DispatcherExceptions.InvalidCallbackException(); } callbackRegistry.Remove(dispatchToken); }
public void Dispatcher_OnDispatch_DisablesDispatch() { Dispatcher dispatcher = new Dispatcher(); DispatchToken dispatchToken = dispatcher.Register(p => { dispatcher.Dispatch(new Payload("", 349)); }); dispatcher.Dispatch(new Payload("", 5)); }
public void Dispatcher_OnFinishDispatch_EnablesDispatch() { int dispatchCounter = 0; Dispatcher dispatcher = new Dispatcher(); DispatchToken dispatchToken = dispatcher.Register(p => { dispatchCounter++; }); dispatcher.Dispatch(new Payload("", 0)); dispatcher.Dispatch(new Payload("", 1)); Assert.AreEqual(dispatchCounter, 2); }
public void Dispatcher_OnDispatch_RejectsCircularWaitForRequests() { Dispatcher dispatcher = new Dispatcher(); DispatchToken second = null; DispatchToken first = dispatcher.Register(p => { dispatcher.WaitFor(second); }); second = dispatcher.Register(p => { dispatcher.WaitFor(first); }); dispatcher.Dispatch(new Payload("", 4)); }
public void Dispatcher_OnDispatch_AcceptsWaitForRequests() { bool hasWaited = false; Dispatcher dispatcher = new Dispatcher(); DispatchToken second = null; DispatchToken first = dispatcher.Register(p => { dispatcher.WaitFor(second); Assert.IsTrue(hasWaited); }); second = dispatcher.Register(p => { hasWaited = true; }); dispatcher.Dispatch(new Payload("", 4)); }
public DispatchToken Register(Action <DispatcherMessage> callback) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } if (_isDispatching) { throw new InvalidOperationException("Cannot register a dispatch token while dispatching"); } var token = new DispatchToken(); _callbacks.Add(token, callback); return(token); }
/// <summary> /// Unregisters the callback associated with the given token. /// </summary> /// <param name="token">The dispatch token to unregister; may not be null.</param> /// <remarks> /// This method cannot be called while a dispatch is in progress. /// </remarks> public void Unregister(DispatchToken token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (!_callbacks.ContainsKey(token)) { throw new ArgumentException("", nameof(token)); } if (_isDispatching) { throw new InvalidOperationException("Cannot unregister a dispatch token while dispatching"); } if (!_callbacks.ContainsKey(token)) { throw new InvalidOperationException($"Invalid {nameof(token)} specified - not currently registered"); } _callbacks.Remove(token); }
/// <summary> /// Checks if the given dispatch token exists in the callback registry. /// </summary> /// <param name="dispatchToken"></param> /// <returns></returns> public bool HasRegistered(DispatchToken dispatchToken) { return(dispatchToken != null && callbackRegistry.ContainsKey(dispatchToken)); }
/// <summary> /// Invokes a given callback by its dispatch token. /// </summary> /// <param name="dispatchToken"></param> protected void invokeCallback(DispatchToken dispatchToken) { isPending.Add(dispatchToken); callbackRegistry[dispatchToken](currentPayload); isHandled.Add(dispatchToken); }
public static void RunTests() { Module("AppDispatcher"); Test("Broadcast action to single receiver", assert => { var dispatcher = new AppDispatcher(); dispatcher.Receive(action => { assert.Equal(action.GetType(), typeof(SomethingHappened)); }); dispatcher.Dispatch(new SomethingHappened()); }); Test("Broadcast action to two receivers, where the second to subscribe waits for the first to complete", assert => { var dispatcher = new AppDispatcher(); var firstReceiverProcessed = false; var token = dispatcher.Receive(action => { firstReceiverProcessed = true; }); dispatcher.Receive(action => { dispatcher.WaitFor(token); assert.Equal(firstReceiverProcessed, true); }); dispatcher.Dispatch(new SomethingHappened()); }); Test("Broadcast action to two receivers, where the first to subscribe waits for the second to complete", assert => { var dispatcher = new AppDispatcher(); DispatchToken token = null; var secondReceiverProcessed = false; dispatcher.Receive(action => { dispatcher.WaitFor(token); assert.Equal(secondReceiverProcessed, true); }); token = dispatcher.Receive(action => { secondReceiverProcessed = true; }); dispatcher.Dispatch(new SomethingHappened()); }); Test("Broadcast action to two receivers that WaitFor each other - expect circular dependency failure", assert => { var dispatcher = new AppDispatcher(); DispatchToken token2 = null; var token1 = dispatcher.Receive(action => { dispatcher.WaitFor(token2); }); token2 = dispatcher.Receive(action => { dispatcher.WaitFor(token1); }); assert.Throws(() => dispatcher.Dispatch(new SomethingHappened()), "Dispatch should throw in this case"); }); Test("Broadcast action to three receivers where the first waits on the third, the second waits on the first and the third waits on the second - expect circular dependency failure", assert => { var dispatcher = new AppDispatcher(); DispatchToken token3 = null; var token1 = dispatcher.Receive(action => { dispatcher.WaitFor(token3); }); var token2 = dispatcher.Receive(action => { dispatcher.WaitFor(token1); }); token3 = dispatcher.Receive(action => { dispatcher.WaitFor(token2); }); assert.Throws(() => dispatcher.Dispatch(new SomethingHappened()), "Dispatch should throw in this case"); }); }