Skip to content

Commit

Permalink
BatchedEvents - SubscribeAndAddTwoThenDisposeAllAndComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Feb 19, 2020
1 parent 05cff38 commit b7cf55e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Expand Up @@ -23,6 +23,7 @@ public BatchedEvents(Func<Task> delay)

public void Dispose()
{
this.batches.Clear();
}

public IDisposable Subscribe(T item, Action<T> callback)
Expand Down
Expand Up @@ -180,5 +180,35 @@ public void SubscribeAndAddTwoThenDisposeOneAndComplete()

batches.Should().ContainSingle().Which.Should().Be("B:item2");
}

[TestMethod]
public void SubscribeAndAddTwoThenDisposeAllAndComplete()
{
Queue<TaskCompletionSource<bool>> pending = new Queue<TaskCompletionSource<bool>>();
BatchedEvents<string> events = new BatchedEvents<string>(() => pending.Dequeue().Task);
List<string> batches = new List<string>();
TaskCompletionSource<bool> pending1 = new TaskCompletionSource<bool>();
TaskCompletionSource<bool> pending2 = new TaskCompletionSource<bool>();
pending.Enqueue(pending1);
pending.Enqueue(pending2);

IDisposable sub1 = events.Subscribe("item1", i => batches.Add("A:" + i));
events.Subscribe("item2", i => batches.Add("B:" + i));

events.Add("item1", new TimePoint(1));
events.Add("item2", new TimePoint(1));

batches.Should().BeEmpty();

events.Dispose();

pending1.SetResult(true);

batches.Should().BeEmpty();

pending2.SetResult(true);

batches.Should().BeEmpty();
}
}
}

0 comments on commit b7cf55e

Please sign in to comment.