Class: Rx::TestScheduler

Inherits:
VirtualTimeScheduler show all
Defined in:
lib/rx/testing/test_scheduler.rb

Overview

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

Instance Attribute Summary

Attributes inherited from VirtualTimeScheduler

#clock

Instance Method Summary collapse

Methods inherited from VirtualTimeScheduler

#advance_by, #advance_to, #enabled?, #get_next, #invoke, #now, #schedule_at_absolute, #schedule_at_relative, #schedule_at_relative_with_state, #schedule_with_state, #sleep, #start, #stop

Methods included from Scheduler

normalize, now, #schedule, #schedule_absolute, #schedule_recursive, #schedule_recursive_absolute, #schedule_recursive_absolute_with_state, #schedule_recursive_relative, #schedule_recursive_relative_with_state, #schedule_recursive_with_state, #schedule_relative

Constructor Details

#initializeTestScheduler

Returns a new instance of TestScheduler.



15
16
17
# File 'lib/rx/testing/test_scheduler.rb', line 15

def initialize
  super(0)
end

Instance Method Details

#add(absolute, relative) ⇒ Object

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



29
30
31
# File 'lib/rx/testing/test_scheduler.rb', line 29

def add(absolute, relative)
  absolute + relative
end

#configure(options = {}) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object

Creates a cold observable using the specified timestamped notification messages.



86
87
88
# File 'lib/rx/testing/test_scheduler.rb', line 86

def create_cold_observable(*args)
  ColdObservable.new(self, *args)
end

#create_hot_observable(*args) ⇒ Object

Creates a hot observable using the specified timestamped notification messages.



81
82
83
# File 'lib/rx/testing/test_scheduler.rb', line 81

def create_hot_observable(*args)
  HotObservable.new(self, *args)
end

#create_observerObject

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



91
92
93
# 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) ⇒ Object

Schedules an action to be executed at due_time.



20
21
22
23
24
25
26
# 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) ⇒ Object

Converts the time span value to a relative time value.



39
40
41
# File 'lib/rx/testing/test_scheduler.rb', line 39

def to_relative(time_span)
  time_span
end

#to_time(absolute) ⇒ Object

Converts the absolute time value to a Time value.



34
35
36
# File 'lib/rx/testing/test_scheduler.rb', line 34

def to_time(absolute)
  Time.at absolute
end