Created
December 17, 2012 12:29
-
-
Save leeoades/4317948 to your computer and use it in GitHub Desktop.
Unit Tests for Pausable Observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Stuff | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reactive.Linq; | |
using System.Reactive.Subjects; | |
using Core.Extensions; | |
using Microsoft.Reactive.Testing; | |
using NUnit.Framework; | |
[TestFixture] | |
public class PausableObservableTests | |
{ | |
private TestScheduler _testScheduler; | |
private Subject<bool> _isPausedStream; | |
private IObservable<int> _numbersStream; | |
private List<int> _pumpedNumbers; | |
private List<int> _pausablePumpedNumbers; | |
[SetUp] | |
public void Setup() | |
{ | |
_testScheduler = new TestScheduler(); | |
_isPausedStream = new Subject<bool>(); | |
_numbersStream = Observable.Generate(1, x => x < 100, x => x + 1, x => x, x => TimeSpan.FromSeconds(1), | |
_testScheduler); | |
_pumpedNumbers = new List<int>(); | |
_pausablePumpedNumbers = new List<int>(); | |
_numbersStream.Subscribe(_pumpedNumbers.Add); | |
} | |
private void BuildPausable(bool startPaused) | |
{ | |
_numbersStream.Pausable(_isPausedStream, startPaused).Subscribe(_pausablePumpedNumbers.Add); | |
_testScheduler.AdvanceBy(TimeSpan.FromMilliseconds(10).Ticks); | |
} | |
[Test] | |
public void NumbersStream_should_pump_10_numbers_in_10_seconds() | |
{ | |
// ACT | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_testScheduler.AdvanceBy(TimeSpan.FromMilliseconds(10).Ticks); // a little bit more than 10 seconds | |
// ASSERT | |
Assert.That(_pumpedNumbers.Count(), Is.EqualTo(10)); | |
} | |
[Test] | |
public void When_Unpaused_should_receive_10_numbers_in_10_seconds() | |
{ | |
// ARRANGE | |
BuildPausable(false); | |
// ACT | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(10)); | |
CollectionAssert.AreEqual(_pumpedNumbers, _pausablePumpedNumbers); | |
} | |
[Test] | |
public void When_Paused_should_receive_0_numbers_in_10_seconds() | |
{ | |
// ACT | |
BuildPausable(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(0)); | |
} | |
[Test] | |
public void When_paused_for_10_seconds_then_unpaused_should_receive_10_numbers() | |
{ | |
// ACT | |
BuildPausable(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(false); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(10)); | |
CollectionAssert.AreEqual(_pumpedNumbers, _pausablePumpedNumbers); | |
} | |
[Test] | |
public void When_unpaused_for_10s_then_paused_for_10s_should_receive_only_10_numbers() | |
{ | |
// ACT | |
BuildPausable(false); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(10)); | |
CollectionAssert.AreEqual(_pumpedNumbers.Take(10), _pausablePumpedNumbers); | |
} | |
[Test] | |
public void When_unpaused_for_10s_then_paused_for_10s_then_unpaused_should_receive_20_numbers() | |
{ | |
// ACT | |
BuildPausable(false); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(false); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(20)); | |
CollectionAssert.AreEqual(_pumpedNumbers, _pausablePumpedNumbers); | |
} | |
[Test] | |
public void When_paused_10s_unpaused_10s_paused_10s_should_receive_20_numbers() | |
{ | |
// ACT | |
BuildPausable(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(false); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
_isPausedStream.OnNext(true); | |
_testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); | |
// ASSERT | |
Assert.That(_pausablePumpedNumbers.Count(), Is.EqualTo(20)); | |
CollectionAssert.AreEqual(_pumpedNumbers.Take(20), _pausablePumpedNumbers); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment