private void CleanUp() { _currentQueue = null; _consumerTasks = null; _producerTasks = null; _cancelSource = null; }
public void CollectionChangeHandlerInvokedForLoadWithContravariantHandler() { IEnumerable <Item> loadedItems = new[] { new Item(), new Item() }; var loader = new AsyncLoader <Item>( seqFactory: Seq.ListBased, loadDataAsync: t => Task.FromResult(loadedItems), eventContext: new RunInlineSynchronizationContext()); // Simulate an external consumer of this collection IAsyncCollection <IItem> externalView = loader; var listener = Substitute.For <CollectionChangedHandler <IItem> >(); externalView.CollectionChanged += listener; loader.LoadAsync(); // --- Perform --- var expectedChanges = loadedItems.Select(item => new ItemChange <Item>(ChangeType.Added, item)); listener.Received().Invoke(loader, Fluent.Match <IEnumerable <IItemChange <Item> > >(coll => coll.Should().BeEquivalentTo(expectedChanges))); }
private static void RunProducer(IAsyncCollection <int> queue) { for (int i = 0; i < _itemsAddedPerThread; i++) { int item = 42; queue.Add(item); } }
public void CanAddCollectionChangeHandlersOfDifferentTypes() { var loader = new AsyncLoader <Item>(seqFactory: Seq.ListBased); // Simulate an external consumer of this collection IAsyncCollection <IItem> externalView = loader; externalView.CollectionChanged += DummyChangesListener; //externalView.CollectionChanges += DummyRootListener; // does not compile?? externalView.CollectionChanged += new CollectionChangedHandler <IRoot>(DummyRootListener); }
private void DdosQueue(IAsyncCollection <int> queue) { int itemsAddedTotal = ProducerTasks * _itemsAddedPerThread; IntHolder itemsTakenHolder = new IntHolder() { Value = 0 }; CancellationTokenSource consumerCancelSource = new CancellationTokenSource(); Task[] consumerTasks = Enumerable.Range(0, ConsumerTasks) .Select(_ => Task.Run(() => RunConsumerAsync(queue, itemsTakenHolder, itemsAddedTotal, consumerCancelSource))) .ToArray(); Task[] producerTasks = Enumerable.Range(0, ProducerTasks) .Select(_ => Task.Run(() => RunProducer(queue))) .ToArray(); Task.WaitAll(producerTasks); Task.WaitAll(consumerTasks); }
private static async Task RunConsumerAsync(IAsyncCollection <int> queue, IntHolder itemsTakeHolder, int itemsAddedTotal, CancellationTokenSource cancelSource) { try { CancellationToken cancelToken = cancelSource.Token; while (true) { int item = await queue.TakeAsync(cancelToken).ConfigureAwait(false); int itemsTakenLocal = Interlocked.Increment(ref itemsTakeHolder.Value); if (itemsTakenLocal >= itemsAddedTotal) { cancelSource.Cancel(); } } } catch (OperationCanceledException) { } }
/// <summary> /// Removes and returns an item from the collection in an asynchronous manner. /// </summary> public static Task <T> TakeAsync <T>(this IAsyncCollection <T> collection) { return(collection.TakeAsync(CancellationToken.None)); }
/// <summary> /// Count the number of items in the current collection. /// </summary> /// <typeparam name="T">The type of the items in the collection.</typeparam> /// <param name="collection">The current collection.</param> /// <returns>A value corresponding to the number of items in the current collection.</returns> public static Task <ulong> CountAsync <T>(this IAsyncCollection <T> collection) { return(collection.CountAsync(CancellationToken.None)); }
/// <summary> /// Determine whether the current collection contains a given item. /// </summary> /// <typeparam name="T">The type of the items in the collection.</typeparam> /// <param name="collection">The current collection.</param> /// <param name="item">An item to search for in the current collection.</param> /// <returns> /// A value indicating whether or not the current collection contains the given item. /// </returns> public static Task <bool> ContainsAsync <T>(this IAsyncCollection <T> collection, T item) { return(collection.ContainsAsync(item, CancellationToken.None)); }
private void Initialize( Func<IAsyncCollection<int>> factoryMethod ) { _currentQueue = factoryMethod(); _itemsTaken = 0; _cancelSource = new CancellationTokenSource(); }