Module: Orocos::SelfTest

Includes:
FlexMock::ArgumentTypes, FlexMock::MockContainer, Test::Mocks, Test::RubyTasks
Included in:
Minitest::Test
Defined in:
lib/orocos/test.rb

Constant Summary collapse

TEST_MODES =

A set of “modes” that can be used to control how the tests will be performed

(ENV['OROCOS_TEST_MODES'] || "").split(',')
TEST_MODEL_LESS =

Test without models

TEST_MODES.include?('no_model')
TEST_MISSING_MODELS =

Test with models that cannot be loaded

TEST_MODES.include?('missing_model')
USE_MQUEUE =

Whether we should enable MQ support during tests

Orocos::Rake::USE_MQUEUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Test::RubyTasks

#helpers_dir, #new_external_ruby_task_context, #new_ruby_task_context, #register_allocated_ruby_tasks

Methods included from Test::Mocks

#mock_input_port, #mock_output_port, #mock_task_context, #mock_task_context_model

Instance Attribute Details

#processesObject (readonly)

Returns the value of attribute processes



135
136
137
# File 'lib/orocos/test.rb', line 135

def processes
  @processes
end

Instance Method Details

#assert_async_polls_until(period: 0.01, timeout: 5) ⇒ Object

Polls the async event loop until a condition is met

Parameters:

  • period (Float)

    the period in seconds

  • timeout (Float)

    the timeout in seconds. The test will flunk if the condition is not met within that many seconds

Yield Returns:

  • a falsy value if the condition is not met yet (i.e. false or nil), and a truthy value if the condition has been met. This value is returned by #async_poll_until



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/orocos/test.rb', line 216

def assert_async_polls_until(period: 0.01, timeout: 5)
    start = Time.now
    while true
        Orocos::Async.step
        if result = yield
            return result
        end
        if Time.now - start > timeout
            flunk("timed out while waiting for condition")
        end
        sleep period
    end
end

#assert_state_equals(state, task, timeout = 1) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/orocos/test.rb', line 176

def assert_state_equals(state, task, timeout = 1)
    expected_toplevel = task.toplevel_state(state)
    toplevel = task.rtt_state
    if expected_toplevel != toplevel
        flunk("#{task} was expected to be in toplevel state #{expected_toplevel} because of #{state} but is in #{toplevel}")
    end

    Integer(timeout / 0.01).times do
        if task.state == state
            return
        end
        sleep 0.01
    end
    flunk("#{task} was expected to be in state #{state} but is in #{task.state}")
end

#data_dirObject



64
# File 'lib/orocos/test.rb', line 64

def data_dir; File.join(@test_dir, 'data') end

#get(name) ⇒ Object



147
148
149
# File 'lib/orocos/test.rb', line 147

def get(name)
    Orocos.get name
end

#ior(name) ⇒ Object

helper for generating an ior from a name



201
202
203
204
205
# File 'lib/orocos/test.rb', line 201

def ior(name)
    name_service.ior(name)
rescue Orocos::NotFound
    "IOR:010000001f00000049444c3a5254542f636f7262612f435461736b436f6e746578743a312e300000010000000000000064000000010102000d00000031302e3235302e332e31363000002bc80e000000fe8a95a65000004d25000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100"
end

#make_tmpdirObject



137
138
139
140
141
# File 'lib/orocos/test.rb', line 137

def make_tmpdir
    dir = Dir.mktmpdir
    @__tmpdirs << dir
    dir
end

#name_serviceObject



196
197
198
# File 'lib/orocos/test.rb', line 196

def name_service
    Orocos.name_service
end

#read_one_sample(reader, timeout = 1) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/orocos/test.rb', line 165

def read_one_sample(reader, timeout = 1)
    Integer(timeout / 0.01).times do
        if value = reader.read_new
            return value
        end
        sleep 0.01

    end
    flunk("expected to receive one new sample on #{reader}, but got none (state: #{reader.port.task.rtt_state}")
end

#setupObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/orocos/test.rb', line 66

def setup
    @old_pkg_config_path = ENV['PKG_CONFIG_PATH'].dup
    @__warn_for_missing_default_loggers = Orocos.warn_for_missing_default_loggers?
    Orocos.warn_for_missing_default_loggers = false
    Orocos::MQueue.auto = USE_MQUEUE

    @test_dir = File.expand_path(File.join("..", "..", 'test'), File.dirname(__FILE__))
    @__tmpdirs = Array.new

    # Since we are loading typekits over and over again, we need to
    # disable type export
    Orocos.export_types = false
    if File.directory?(work_dir)
        Orocos.default_working_directory = work_dir
        ENV['PKG_CONFIG_PATH'] += ":#{File.join(work_dir, "prefix", 'lib', 'pkgconfig')}"
        Orocos.default_pkgconfig_loader.clear
    end

    if defined?(Orocos::Async)
        Orocos::Async::NameServiceBase.default_period = 0
        Orocos::Async::TaskContextBase.default_period = 0
        Orocos::Async::CORBA::Attribute.default_period = 0
        Orocos::Async::CORBA::Property.default_period = 0
        Orocos::Async::CORBA::OutputReader.default_period = 0
    end

    @processes = Array.new

    Orocos.initialize
    @__orocos_corba_timeouts = [Orocos::CORBA.call_timeout, Orocos::CORBA.connect_timeout]
    Orocos::CORBA.call_timeout = 10000
    Orocos::CORBA.connect_timeout = 10000

	    if TEST_MODEL_LESS
		flexmock(Orocos::TaskContext).new_instances(:get_from_ior).should_receive('model').and_return(nil)
		flexmock(Orocos::TaskContext).new_instances(:do_get).should_receive('model').and_return(nil)
	    elsif TEST_MISSING_MODELS
		flexmock(Orocos).should_receive(:task_model_from_name).and_raise(Orocos::NotFound)
	    end
    super
end

#spawn_and_get(component, task = component) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/orocos/test.rb', line 151

def spawn_and_get(component, task = component)
    begin
        process = Orocos::Process.new component
        process.spawn
        process.wait_running(0.5)
    rescue Exception
        process.kill if process
        raise
    end

    processes << process
    Orocos::TaskContext.get "#{component}.#{task}"
end

#start(*spec) ⇒ Object



143
144
145
# File 'lib/orocos/test.rb', line 143

def start(*spec)
    processes.concat Orocos.run(*spec)
end

#teardownObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/orocos/test.rb', line 108

def teardown
    if defined? FlexMock
        flexmock_teardown
    end
    @__tmpdirs.each do |dir|
        FileUtils.rm_rf dir
    end
    processes.each do |p|
        begin p.kill
        rescue Exception => e
            Orocos.warn "failed, in teardown, to stop process #{p}: #{e}"
        end
    end
    processes.clear
    super
    if @__orocos_corba_timeouts # can be nil if setup failed
        Orocos::CORBA.call_timeout, Orocos::CORBA.connect_timeout = *@__orocos_corba_timeouts
    end
    if @old_pkg_config_path # can be nil if setup failed
        ENV['PKG_CONFIG_PATH'] = @old_pkg_config_path
    end
    Orocos::CORBA.instance_variable_set :@loaded_typekits, []
    Orocos.warn_for_missing_default_loggers = @__warn_for_missing_default_loggers
ensure
    Orocos.clear
end

#wait_for(timeout = 5, &block) ⇒ Object



192
193
194
# File 'lib/orocos/test.rb', line 192

def wait_for(timeout = 5, &block)
    Orocos::Async.wait_for(0.005, timeout, &block)
end

#work_dirObject



63
# File 'lib/orocos/test.rb', line 63

def work_dir; File.join(@test_dir, 'working_copy') end