public void TestVolatileAccess() { VolatileContainer container = new VolatileContainer(); MockActivator a = ActivatorFor(container); Assert.AreEqual(0, container.Foo); Assert.AreEqual(1, a.ReadCount); }
public void ResolveEnumerable() { var volatileObject = Create <List <IVolatileObject> >().Object; var componentRegistration = Create <Autofac.Core.IComponentRegistration>().Object; var componentRegistry = Create <Autofac.Core.IComponentRegistry>(); componentRegistry.Setup(o => o.TryGetRegistration(It.IsAny <Autofac.Core.TypedService>(), out componentRegistration)).Returns(true); var scope = Create <ILifetimeScope>(); scope.Setup(o => o.ComponentRegistry).Returns(componentRegistry.Object); scope.Setup(o => o.ResolveComponent(componentRegistration, It.IsAny <Autofac.Core.Parameter[]>())).Returns(volatileObject); var container = new VolatileContainer(scope.Object); Assert.AreEqual(volatileObject, container.ResolveEnumerable <IVolatileObject>()); }
public void ForEach <T>(T[] arguments, Action <TaskEntry, T> action) { ASSERT(arguments != null, "Missing parameter 'arguments'"); ASSERT(action != null, "Missing parameter 'action'"); // Create arguments.Length TaskEntries int count = arguments.Length; var resetEvent = new ManualResetEvent(false); var entries = new TaskEntry[count]; var exceptionContainer = new VolatileContainer <Exception> { Value = null }; for (int i = 0; i < arguments.Length; ++i) { var argument = arguments[i]; entries[i] = CreateTask((entry) => { try { action(entry, argument); } catch (System.Exception ex) { lock ( exceptionContainer ) { // If this is the first exception we catch ... if (exceptionContainer.Value == null) { // ... save it exceptionContainer.Value = ex; } } // Release the main thread now so it can 'Remove()' all remaining TaskEntries resetEvent.Set(); } finally { var newCount = Interlocked.Decrement(ref count); if (newCount == 0) { // The last TaskEntry has finished resetEvent.Set(); } } }); } // Wait for all TaskEntries to exit resetEvent.WaitOne(new TimeSpan(0, 0, 30)); // if the resetEvent exited because of an exception ... if (exceptionContainer.Value != null) { // ... discard all remaining TaskEntries (it is the responsibility of the caller to check 'entry.IsRemoved()') ... foreach (var entry in entries) { entry.Remove(); } // ... and rethrow the caught exception throw exceptionContainer.Value; } }