class Rx::TestScheduler

Virtual time scheduler used for testing applications and libraries built using Reactive Extensions.

Public Class Methods

new() click to toggle source
Calls superclass method Rx::VirtualTimeScheduler::new
# File lib/rx/testing/test_scheduler.rb, line 15
def initialize
  super(0)
end

Public Instance Methods

add(absolute, relative) click to toggle source

Adds a relative virtual time to an absolute virtual time value.

# File lib/rx/testing/test_scheduler.rb, line 29
def add(absolute, relative)
  absolute + relative
end
configure(options = {}) { || ... } click to toggle source

Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and unsubscribe the subscription.

# File lib/rx/testing/test_scheduler.rb, line 44
def configure(options = {})
  options.each {|key,_|
    unless [:created, :subscribed, :disposed].include? key
      raise ArgumentError, "Should be specified whether :created, :subscribed or :disposed, but the #{key.inspect}"
    end
  }
  o = {
    :created    => ReactiveTest::CREATED,
    :subscribed => ReactiveTest::SUBSCRIBED,
    :disposed   => ReactiveTest::DISPOSED
  }.merge(options)

  source = nil
  subscription = nil
  observer = create_observer

  schedule_at_absolute_with_state(nil, o[:created], lambda {|scheduler, state|
    source = yield
    Subscription.empty
  })

  schedule_at_absolute_with_state(nil, o[:subscribed], lambda {|scheduler, state|
    subscription = source.subscribe observer
    Subscription.empty
  })

   schedule_at_absolute_with_state(nil, o[:disposed], lambda {|scheduler, state|
    subscription.unsubscribe
    Subscription.empty
  })

  start
  
  observer           
end
create_cold_observable(*args) click to toggle source

Creates a cold observable using the specified timestamped notification messages.

# File lib/rx/testing/test_scheduler.rb, line 86
def create_cold_observable(*args)
  ColdObservable.new(self, *args)
end
create_hot_observable(*args) click to toggle source

Creates a hot observable using the specified timestamped notification messages.

# File lib/rx/testing/test_scheduler.rb, line 81
def create_hot_observable(*args)
  HotObservable.new(self, *args)
end
create_observer() click to toggle source

Creates an observer that records received notification messages and timestamps those.

# File lib/rx/testing/test_scheduler.rb, line 91
def create_observer
  MockObserver.new self
end
schedule_at_absolute_with_state(state, due_time, action) click to toggle source

Schedules an action to be executed at due_time.

# File lib/rx/testing/test_scheduler.rb, line 20
def schedule_at_absolute_with_state(state, due_time, action)
  raise 'action cannot be nil' unless action
  
  due_time = clock + 1 if due_time <= clock

  super(state, due_time, action)
end
to_relative(time_span) click to toggle source

Converts the time span value to a relative time value.

# File lib/rx/testing/test_scheduler.rb, line 39
def to_relative(time_span)
  time_span
end
to_time(absolute) click to toggle source

Converts the absolute time value to a Time value.

# File lib/rx/testing/test_scheduler.rb, line 34
def to_time(absolute)
  Time.at absolute
end