public void SetException_Should_RunContinuationIfOneIsPresent() { var taskSlim = new TaskSlim(null); taskSlim.IsCompleted.Should().BeFalse(); taskSlim.HasContinuation.Should().BeFalse(); taskSlim.HasException.Should().BeFalse(); taskSlim.RunContinuationAsync.Should().BeFalse(); var runCont = false; taskSlim.OnCompleted(() => { runCont = true; Assert.Throws <Exception>(() => { taskSlim.GetResult(); // throws the exception }); }); taskSlim.SetException(new Exception("nope"), runContinuationAsync: false); runCont.Should().BeTrue(); }
public void Add(TaskSlim tcs) { // happens past semaphore, from the frame writing thread, thus "safe" var success = false; // for (int i = 0; i < _tasks.Length; i++) { var id = GetNext(); var index = id % _max; if (Interlocked.CompareExchange(ref _tasks[index], tcs, null) == null) { success = true; // break; } else { // Spot in use, so continue up to sizeOf(_tasks) attempts } } if (!success) // probably will never happen due to the semaphore, but just in case. { // fail fast, better than leaving the task hanging forever tcs.SetException(new Exception("MessagesPendingConfirmationKeeper: Could not find free spot for the waiting task")); } }
internal static void SetException(TaskSlim tcs, AmqpError error, int classMethodId) { if (tcs == null) { return; } if (error != null) { tcs.SetException(new Exception("Error: " + error.ToErrorString())); } else if (classMethodId == 0) { tcs.SetException(new Exception("The server closed the connection")); } else { var classId = classMethodId >> 16; var methodId = classMethodId & 0x0000FFFF; Console.WriteLine("Unexpected situation: classId = " + classId + " method " + methodId + " and error = null"); tcs.SetException(new Exception("Unexpected reply from the server: classId = " + classId + " method " + methodId)); } }
public void OnCompleted_Should_RunContinuationIfCompletedWithException() { var taskSlim = new TaskSlim(null); taskSlim.IsCompleted.Should().BeFalse(); taskSlim.HasContinuation.Should().BeFalse(); taskSlim.HasException.Should().BeFalse(); taskSlim.RunContinuationAsync.Should().BeFalse(); taskSlim.SetException(new Exception("nope"), runContinuationAsync: false); var runCont = false; taskSlim.OnCompleted(() => { runCont = true; Assert.Throws<Exception>(() => { taskSlim.GetResult(); // throws the exception }); }); runCont.Should().BeTrue(); }