Virtual time scheduler used for testing applications and libraries built using Reactive Extensions. This inherits from the Rx.TestScheduler class.
Usage ##
The following shows an example of using the Rx.TestScheduler. In order to make the end comparisons work, you must implement a collection assert, for example here using QUnit.
functioncreateMessage(actual, expected) {return'Expected: ['+expected.toString() +']\r\nActual: ['+actual.toString() +']';}// Using QUnit testing for assertionsvar collectionAssert = {assertEqual:function (expected, actual) {var comparer =Rx.Internals.isEqual, isOk =true;if (expected.length!==actual.length) {ok(false,'Not equal length. Expected: '+expected.length+' Actual: '+actual.length);return; }for(var i =0, len =expected.length; i < len; i++) { isOk =comparer(expected[i], actual[i]);if (!isOk) {break; } }ok(isOk,createMessage(expected, actual)); }};var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted, subscribe =Rx.ReactiveTest.subscribe;var scheduler =newRx.TestScheduler();// Create hot observable which will start firingvar xs =scheduler.createHotObservable(onNext(150,1),onNext(210,2),onNext(220,3),onCompleted(230));// Note we'll start at 200 for subscribe, hence missing the 150 markvar res =scheduler.startWithCreate(function () {returnxs.map(function (x) { return x * x });});// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(210,4),onNext(220,9),onCompleted(230)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,230)]);
var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted;var scheduler =newRx.TestScheduler();// Create hot observable which will start firingvar xs =scheduler.createHotObservable(onNext(150,1),onNext(210,2),onNext(220,3),onCompleted(230));// Note we'll start at 200 for subscribe, hence missing the 150 markvar res =scheduler.startWithCreate(function () {returnxs.map(function (x) { return x * x });});// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(210,4),onNext(220,9),onCompleted(230)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,230)]);
Creates a hot observable using the specified timestamped notification messages.
Arguments
args(Arguments): An arguments array of Recorded objects from Rx.ReactiveTest.onNext, Rx.ReactiveTest.onError, and Rx.ReactiveTest.onCompleted methods.
Returns
(Observable): Hot observable sequence that can be used to assert the timing of subscriptions and notifications.
Example
var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted;var scheduler =newRx.TestScheduler();// Create hot observable which will start firingvar xs =scheduler.createHotObservable(onNext(150,1),onNext(210,2),onNext(220,3),onCompleted(230));// Note we'll start at 200 for subscribe, hence missing the 150 markvar res =scheduler.startWithCreate(function () {returnxs.map(function (x) { return x * x });});// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(210,4),onNext(220,9),onCompleted(230)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,230)]);
Starts the test scheduler and uses default virtual times to invoke the factory function, to subscribe to the resulting sequence, and to dispose the subscription.
Arguments
create(Function): Factory method to create an observable sequence.
Returns
(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
Example
var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted;var scheduler =newRx.TestScheduler();// Create cold observable with offset from subscribe timevar xs =scheduler.createColdObservable(onNext(150,1),onNext(200,2),onNext(250,3),onCompleted(300));// Note we'll start at 200 for subscribevar res =scheduler.startWithCreate(function () {returnxs.filter(function (x) { return x %2===0; });});// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(400,2),onCompleted(500)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,500)]);
Starts the test scheduler and uses the specified virtual time to dispose the subscription to the sequence obtained through the factory function. Default virtual times are used for factory invocation and sequence subscription.
Arguments
create(Function): Factory method to create an observable sequence.
disposed(Number): Virtual time at which to dispose the subscription.
Returns
(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
Example
var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted;var scheduler =newRx.TestScheduler();// Create hot observable which will start firingvar xs =scheduler.createHotObservable(onNext(150,1),onNext(210,2),onNext(220,3),onCompleted(230));// Note we'll start at 200 for subscribe, hence missing the 150 markvar res =scheduler.startWithDispose(function () {returnxs.map(function (x) { return x * x }); },215/* Dispose at 215 */);// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(210,4),onCompleted(215)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,215)]);
Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and dispose the subscription.
Arguments
create(Function): Factory method to create an observable sequence.
created(Number): Virtual time at which to invoke the factory to create an observable sequence.
subscribed(Number): Virtual time at which to subscribe to the created observable sequence.
disposed(Number): Virtual time at which to dispose the subscription.
Returns
(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
Example
var onNext =Rx.ReactiveTest.onNext, onCompleted =Rx.ReactiveTest.onCompleted;var scheduler =newRx.TestScheduler();// Create hot observable which will start firingvar xs =scheduler.createHotObservable(onNext(150,1),onNext(210,2),onNext(260,3),onNext(310,4),onCompleted(360));// Note we'll start at 200 for subscribe, hence missing the 150 markvar res =scheduler.startWithTiming(function () {returnxs.map(function (x) { return x * x }); },100,/* Create at 100 */200,/* Subscribe at 200 */300/* Dispose at 300 */);// Implement collection assertioncollectionAssert.assertEqual(res.messages, [onNext(210,4),onNext(260,9),onCompleted(300)]);// Check for subscribe/unsubscribecollectionAssert.assertEqual(xs.subscriptions, [subscribe(200,300)]);