public void Sync() { // Arrange Mock <SynchronizationContext> mockSyncContext = new Mock <SynchronizationContext>(); mockSyncContext .Setup(c => c.Send(It.IsAny <SendOrPostCallback>(), null)) .Callback( delegate(SendOrPostCallback d, object state) { d(state); } ); AsyncManager helper = new AsyncManager(mockSyncContext.Object); bool wasCalled = false; // Act helper.Sync( () => { wasCalled = true; } ); // Assert Assert.True(wasCalled); }
public void IndexAsync(int[] ids) { var orders = new Orders[ids.Length]; AsyncManager.Parameters["orders"] = orders; // tell the async manager there are X operations it needs to wait for AsyncManager.OutstandingOperations.Increment(ids.Length); for (int i = 0; i < ids.Length; i++) { var index = i; //<-- make sure we capture the value of i for the closure // create the query var query = _context.CreateQuery <Order>("Orders"); // run the operation async, supplying a completion routine query.BeginExecute(ar => { try { orders[index] = query.EndExecute(ar).First(o => o.id == ids[index]); } catch (Exception ex) { // make sure we send the exception to the controller (in case we want to handle it) AsyncManager.Sync(() => AsyncManager.Parameters["exception"] = ex); } // one more query has completed AsyncManager.OutstandingOperations.Decrement(); }, null); } }
public void Sync() { // Arrange Mock<SynchronizationContext> mockSyncContext = new Mock<SynchronizationContext>(); mockSyncContext .Setup(c => c.Send(It.IsAny<SendOrPostCallback>(), null)) .Callback( delegate(SendOrPostCallback d, object state) { d(state); }); AsyncManager helper = new AsyncManager(mockSyncContext.Object); bool wasCalled = false; // Act helper.Sync(() => { wasCalled = true; }); // Assert Assert.True(wasCalled); }
public void DateAsync() { AsyncManager.OutstandingOperations.Increment(); var random = new Random(); var sleep = random.Next(5, 20); var date = DateTime.Now.ToString(); AsyncManager.Sync(() => { Thread.Sleep(sleep * 1000); AsyncManager.Parameters["date"] = date; AsyncManager.OutstandingOperations.Decrement(); }); }