/// <summary> /// Subscribes a message handler to the bus, which is disconnected after the message /// is received. /// </summary> /// <typeparam name="T">The message type</typeparam> /// <param name="filter">A filter that only completes the task if filter is true</param> /// <returns>An awaitable task completed when the message is received</returns> public Task <ConsumeContext <T> > SubscribeHandler <T>(Func <ConsumeContext <T>, bool> filter) where T : class { TaskCompletionSource <ConsumeContext <T> > source = TaskUtil.GetTask <ConsumeContext <T> >(); ConnectHandle handler = null; handler = Bus.ConnectHandler <T>(async context => { if (filter(context)) { handler.Disconnect(); source.SetResult(context); } }); TestCancelledTask.ContinueWith(x => { handler.Disconnect(); source.TrySetCanceled(); }, TaskContinuationOptions.OnlyOnCanceled); return(source.Task); }
/// <summary> /// Returns a task completion that is automatically canceled when the test is canceled /// </summary> /// <typeparam name="T">The task type</typeparam> /// <returns></returns> protected TaskCompletionSource <T> GetTask <T>() { var source = new TaskCompletionSource <T>(); TestCancelledTask.ContinueWith(x => source.TrySetCanceled(), TaskContinuationOptions.OnlyOnCanceled); return(source); }
/// <summary> /// Returns a task completion that is automatically canceled when the test is canceled /// </summary> /// <typeparam name="T">The task type</typeparam> /// <returns></returns> public TaskCompletionSource <T> GetTask <T>() { TaskCompletionSource <T> source = TaskUtil.GetTask <T>(); TestCancelledTask.ContinueWith(x => source.TrySetCanceled(), TaskContinuationOptions.OnlyOnCanceled); return(source); }
/// <summary> /// Subscribes a message handler to the bus, which is disconnected after the message /// is received. /// </summary> /// <typeparam name="T">The message type</typeparam> /// <returns>An awaitable task completed when the message is received</returns> protected Task <ConsumeContext <T> > SubscribeHandler <T>() where T : class { var source = new TaskCompletionSource <ConsumeContext <T> >(); ConnectHandle handler = null; handler = Bus.ConnectHandler <T>(async context => { source.SetResult(context); handler.Disconnect(); }); TestCancelledTask.ContinueWith(x => { source.TrySetCanceled(); handler.Disconnect(); }, TaskContinuationOptions.OnlyOnCanceled); return(source.Task); }