public void Removing()
    {
        var source = new Subject<Thing>();

        var col = new TrackingCollection<Thing>(
            source,
            OrderedComparer<Thing>.OrderBy(x => x.UpdatedAt).Compare,
            (item, position, list) => (position > 2 && position < 5) || (position > 6 && position < 8))
            { ProcessingDelay = TimeSpan.Zero };

        var count = 0;
        var expectedCount = 0;
        var evt = new ManualResetEvent(false);

        col.Subscribe(t =>
        {
            if (++count == expectedCount)
                evt.Set();
        }, () => { });

        expectedCount = 11;
        Add(source, GetThing(0, 0));
        Add(source, GetThing(1, 1));
        Add(source, GetThing(2, 2));
        Add(source, GetThing(3, 3));
        Add(source, GetThing(4, 4));
        Add(source, GetThing(5, 5));
        Add(source, GetThing(6, 6));
        Add(source, GetThing(7, 7));
        Add(source, GetThing(8, 8));
        Add(source, GetThing(9, 9));
        Add(source, GetThing(10, 10));
        evt.WaitOne();
        evt.Reset();
        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(3, 3),
            GetThing(4, 4),
            GetThing(7, 7),
        });

        expectedCount = 12;
        col.RemoveItem(GetThing(2));
        evt.WaitOne();
        evt.Reset();
        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(4, 4),
            GetThing(5, 5),
            GetThing(8, 8),
        });

        expectedCount = 13;
        col.RemoveItem(GetThing(5));
        evt.WaitOne();
        evt.Reset();
        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(4, 4),
            GetThing(6, 6),
            GetThing(9, 9),
        });

        col.SetFilter(null);

        expectedCount = 14;
        col.RemoveItem(GetThing(100)); // this one won't result in a new element from the observable
        col.RemoveItem(GetThing(10));
        evt.WaitOne();
        evt.Reset();

        Assert.AreEqual(8, col.Count);
        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(0, 0),
            GetThing(1, 1),
            GetThing(3, 3),
            GetThing(4, 4),
            GetThing(6, 6),
            GetThing(7, 7),
            GetThing(8, 8),
            GetThing(9, 9),
        });
        col.Dispose();
    }
 public void DisposingThrows()
 {
     var col = new TrackingCollection<Thing>(Observable.Empty<Thing>());
     col.Dispose();
     Assert.Throws<ObjectDisposedException>(() => col.SetFilter(null));
     Assert.Throws<ObjectDisposedException>(() => col.SetComparer(null));
     Assert.Throws<ObjectDisposedException>(() => col.Subscribe());
     Assert.Throws<ObjectDisposedException>(() => col.AddItem(GetThing(1)));
     Assert.Throws<ObjectDisposedException>(() => col.RemoveItem(GetThing(1)));
 }
    public void ChangingFilterUpdatesCollection()
    {
        var source = new Subject<Thing>();
        var col = new TrackingCollection<Thing>(
            source,
            OrderedComparer<Thing>.OrderBy(x => x.UpdatedAt).Compare,
            (item, position, list) => item.UpdatedAt < Now + TimeSpan.FromMinutes(10))
            { ProcessingDelay = TimeSpan.Zero };


        var count = 0;
        var expectedCount = 0;
        var evt = new ManualResetEvent(false);

        col.Subscribe(t =>
        {
            if (++count == expectedCount)
                evt.Set();
        }, () => { });

        expectedCount = 9;
        Add(source, GetThing(1, 1));
        Add(source, GetThing(2, 2));
        Add(source, GetThing(3, 3));
        Add(source, GetThing(4, 4));
        Add(source, GetThing(5, 5));
        Add(source, GetThing(6, 6));
        Add(source, GetThing(7, 7));
        Add(source, GetThing(8, 8));
        Add(source, GetThing(9, 9));
        evt.WaitOne();
        evt.Reset();
        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(1, 1),
            GetThing(2, 2),
            GetThing(3, 3),
            GetThing(4, 4),
            GetThing(5, 5),
            GetThing(6, 6),
            GetThing(7, 7),
            GetThing(8, 8),
            GetThing(9, 9),
        });

        col.SetFilter((item, position, list) => item.UpdatedAt < Now + TimeSpan.FromMinutes(8));

        CollectionAssert.AreEqual(col, new List<Thing> {
            GetThing(1, 1),
            GetThing(2, 2),
            GetThing(3, 3),
            GetThing(4, 4),
            GetThing(5, 5),
            GetThing(6, 6),
            GetThing(7, 7),
        });
        col.Dispose();
    }