public async Task LimitWithExceptionOnSourceWithBadFirst()
        {
            // Make sure an exception inside is propagated outside.
            var sl = new LINQHelpers.LimitGlobalCounter(1);

            try
            {
                var seq = Observable.Concat(Observable.Throw <int>(new InvalidOperationException()), Observable.Return(10))
                          .LimitGlobally(s => s.SelectMany(v => Observable.Throw <int>(new InvalidOperationException())), sl)
                          .FirstAsync();

                var r = await seq;
            }
            catch (InvalidOperationException)
            {
            }

            Assert.AreEqual(1, sl.CurrentCount);
        }
        public async Task LimitWithExceptionOnSourceWithGood()
        {
            // Make sure an exception inside is propagated outside.
            var sl = new LINQHelpers.LimitGlobalCounter(1);

            try
            {
                var seq = Observable.Concat(Observable.Return(10), Observable.Throw <int>(new InvalidOperationException()))
                          .LimitGlobally(s => s, sl)
                          .FirstAsync();

                var r = await seq;
            }
            catch (InvalidOperationException)
            {
            }

            // Delay is required b.c. Finally sometimes executes after sequence is done.
            await Task.Delay(10);

            Assert.AreEqual(1, sl.CurrentCount);
        }
        public async Task LimitWithExceptionResetsCount()
        {
            // Make sure an exception inside is propagated outside.
            var sl = new LINQHelpers.LimitGlobalCounter(1);

            try
            {
                var seq = Observable.Return(Observable.Return(10))
                          .LimitGlobally(s => s.SelectMany(v => Observable.Throw <int>(new InvalidOperationException())), sl)
                          .LastAsync();

                var r = await seq;
            }
            catch (InvalidOperationException)
            {
            }

            await Task.Delay(10);

            Debug.WriteLine("Doing count check now");
            Assert.AreEqual(1, sl.CurrentCount);
        }
        public async Task LimitWithExceptionOnSourceWithGoodAndErrorInternally()
        {
            // Make sure an exception inside is propagated outside.
            var sl = new LINQHelpers.LimitGlobalCounter(1);

            try
            {
                var seq = Observable.Concat(Observable.Return(10), Observable.Throw <int>(new InvalidOperationException()))
                          .LimitGlobally(s => s.SelectMany(v => Observable.Throw <int>(new InvalidOperationException())), sl)
                          .FirstAsync();

                var r = await seq;
            }
            catch (InvalidOperationException)
            {
            }

            await Task.Delay(10);

            Debug.WriteLine("Checking the semaphore");
            Assert.AreEqual(1, sl.CurrentCount);
            Debug.WriteLine("Done Checking the semaphore");
        }
 /// <summary>
 /// Reset for testing.
 /// </summary>
 /// <remarks>
 /// TODO: you could argue that we need this only because some tests seem to leave the limit counter non-zero.
 /// This really should not be possible, no matter what error occurs. This should be tested/fixed in the code!
 /// </remarks>
 public static void Reset()
 {
     _gLimit = new LINQHelpers.LimitGlobalCounter(1);
 }