Module: Roby::Test::RobyAppHelpers

Defined in:
lib/roby/test/roby_app_helpers.rb

Overview

Helpers to test a full Roby app started as a subprocess

Defined Under Namespace

Classes: SpawnedProcess

Constant Summary collapse

ROBY_PORT_COMMANDS =
%w[run].freeze
ROBY_NO_INTERFACE_COMMANDS =
%w[wait check test].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/roby/test/roby_app_helpers.rb', line 7

def app
  @app
end

#app_dirObject (readonly)

Returns the value of attribute app_dir.



7
8
9
# File 'lib/roby/test/roby_app_helpers.rb', line 7

def app_dir
  @app_dir
end

Instance Method Details

#app_helpers_source_dir(source_dir) ⇒ Object



483
484
485
# File 'lib/roby/test/roby_app_helpers.rb', line 483

def app_helpers_source_dir(source_dir)
    @helpers_source_dir = source_dir
end

#assert_process_exits(pid, timeout: 20) ⇒ Object

Wait for a subprocess to exit



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/roby/test/roby_app_helpers.rb', line 155

def assert_process_exits(pid, timeout: 20)
    deadline = Time.now + timeout
    while Time.now < deadline
        _, status = Process.waitpid2(pid, Process::WNOHANG)
        if status
            roby_app_join_capture_thread(pid)
            return status
        end

        sleep 0.01
    end

    if (output = roby_app_captured_output(pid))
        flunk(
            "process #{pid} did not quit within #{timeout} seconds\n" \
            "stdout=#{output[:out]}\n" \
            "stderr=#{output[:err]}"
        )
    else
        flunk("process #{pid} did not quit within #{timeout} seconds")
    end
end

#assert_roby_app_can_connect_to_log_server(timeout: 2, port: app.log_server_port) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/roby/test/roby_app_helpers.rb', line 461

def assert_roby_app_can_connect_to_log_server(
    timeout: 2, port: app.log_server_port
)
    client = roby_app_with_polling(
        timeout: timeout,
        message: "connecting to the log server on port #{port}"
    ) do
        begin
            DRoby::Logfile::Client.new("localhost", port)
        rescue Interface::ConnectionError # rubocop:disable Lint/SuppressedException
        end
    end
    client.read_and_process_pending until client.init_done?
rescue StandardError
    # Give time to the log server to report errors before we
    # terminate it with SIGINT
    sleep 0.1
    raise
ensure
    client&.close
end

#assert_roby_app_exits(cmd, timeout: 20) ⇒ Object

Wait for the app to exit

Unlike #assert_roby_app_quits, this method does not explicitly call quit or send a SIGINT signal. The app is expected to quit by itself



185
186
187
188
189
190
191
# File 'lib/roby/test/roby_app_helpers.rb', line 185

def assert_roby_app_exits(cmd, timeout: 20)
    if cmd.respond_to?(:stop) # Aruba command
        cmd.stop
    else
        assert_process_exits(cmd, timeout: timeout)
    end
end

#assert_roby_app_has_job(interface, action_name, timeout: 2, state: Interface::JOB_STARTED) ⇒ Object



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

def assert_roby_app_has_job(
    interface, action_name, timeout: 2, state: Interface::JOB_STARTED
)
    start_time = Time.now
    while (Time.now - start_time) < timeout
        jobs = interface.find_all_jobs_by_action_name(action_name)
        jobs = jobs.find_all { |j| j.state == state } if state
        if (j = jobs.first)
            return j
        end

        sleep 0.01
    end
    flunk "timed out while waiting for action #{action_name} on #{interface}"
end

#assert_roby_app_is_running(pid, timeout: 20, host: "localhost", port: roby_app_helpers_default_interface_port) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/roby/test/roby_app_helpers.rb', line 102

def assert_roby_app_is_running(
    pid, timeout: 20, host: "localhost",
    port: roby_app_helpers_default_interface_port
)
    start_time = Time.now
    while (Time.now - start_time) < timeout
        if ::Process.waitpid(pid, Process::WNOHANG)
            if (captured_output = roby_app_join_capture_thread(pid))
                flunk "Roby app unexpectedly quit\n" \
                      "stdout=#{captured_output[:out]}\n" \
                      "stderr=#{captured_output[:err]}"
            else
                flunk "Roby app unexpectedly quit"
            end
        end

        begin
            return roby_app_interface_module.connect_with_tcp_to(host, port)
        rescue Roby::Interface::ConnectionError # rubocop:disable Lint/SuppressedException
        end
        sleep 0.01
    end
    flunk "could not get a connection within #{timeout} seconds"
end

#assert_roby_app_quits(cmd, port: roby_app_helpers_default_interface_port, interface: nil) ⇒ Object

Call the quit command and wait for the app to exit



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/roby/test/roby_app_helpers.rb', line 136

def assert_roby_app_quits(
    cmd, port: roby_app_helpers_default_interface_port, interface: nil
)
    pid =
        if cmd.respond_to?(:pid)
            cmd.pid
        else
            cmd
        end

    interface_owned = !interface
    interface ||= assert_roby_app_is_running(pid, port: port)
    interface.quit
    assert_roby_app_exits(cmd)
ensure
    interface&.close if interface_owned
end

#copy_into_app(template, target = template) ⇒ Object



487
488
489
490
491
# File 'lib/roby/test/roby_app_helpers.rb', line 487

def copy_into_app(template, target = template)
    FileUtils.mkdir_p File.join(app_dir, File.dirname(target))
    FileUtils.cp File.join(@helpers_source_dir, template),
                 File.join(app_dir, target)
end

#gen_app(app_dir = self.app_dir) ⇒ Object



62
63
64
65
# File 'lib/roby/test/roby_app_helpers.rb', line 62

def gen_app(app_dir = self.app_dir)
    require "roby/cli/gen_main"
    Dir.chdir(app_dir) { CLI::GenMain.start(["app", "--quiet"]) }
end

#kill_spawned_pids(pids = @spawned_pids.map(&:pid), signal: "INT", next_signal: "KILL", timeout: 5) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/roby/test/roby_app_helpers.rb', line 33

def kill_spawned_pids(
    pids = @spawned_pids.map(&:pid),
    signal: "INT", next_signal: "KILL", timeout: 5
)
    pending_children = pids.find_all do |pid|
        begin
            Process.kill signal, pid
            true
        rescue Errno::ESRCH # rubocop:disable Lint/SuppressedException
        end
    end

    deadline = Time.now + timeout
    while Time.now < deadline
        pending_children.delete_if do |pid|
            Process.waitpid2(pid, Process::WNOHANG)
        end
        return if pending_children.empty?

        sleep 0.01
    end

    return if pending_children.empty?

    flunk("failed to stop #{pending_children}") unless next_signal

    kill_spawned_pids(pending_children, signal: next_signal, next_signal: nil)
end

#register_pid(pid) ⇒ Object



385
386
387
# File 'lib/roby/test/roby_app_helpers.rb', line 385

def register_pid(pid)
    @spawned_pids << SpawnedProcess.new(pid: pid)
end

#register_roby_plugin(path) ⇒ Object



289
290
291
# File 'lib/roby/test/roby_app_helpers.rb', line 289

def register_roby_plugin(path)
    @roby_plugin_path << path
end

#roby_app_allocate_interface_portObject



282
283
284
# File 'lib/roby/test/roby_app_helpers.rb', line 282

def roby_app_allocate_interface_port
    roby_app_allocate_port
end

#roby_app_allocate_portObject



275
276
277
278
279
280
# File 'lib/roby/test/roby_app_helpers.rb', line 275

def roby_app_allocate_port
    server = TCPServer.new(0)
    server.local_address.ip_port
ensure
    server&.close
end

#roby_app_call_interface(version: @roby_app_interface_version, host: "localhost", port: Interface::DEFAULT_PORT) {|client| ... } ⇒ Object

Create a client to the interface running in the test's current app

The method disconnects from the interface before returning

Yield Parameters:

Yield Returns:

  • (Object)

    object returned by the method



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/roby/test/roby_app_helpers.rb', line 431

def roby_app_call_interface(
    version: @roby_app_interface_version,
    host: "localhost", port: Interface::DEFAULT_PORT
)
    client_thread = Thread.new do
        begin
            interface =
                roby_app_interface_module(version: version)
                .connect_with_tcp_to(host, port)
            result = yield(interface) if block_given?
        rescue Exception => e # rubocop:disable Lint/RescueException
            error = e
        end
        [interface, result, error]
    end
    while client_thread.alive?
        roby_app_shell_interface(version: version)
            .process_pending_requests
    end
    begin
        interface, result, error = client_thread.value
    rescue Exception => e # rubocop:disable Lint/RescueException
        raise e, e.message, e.backtrace + caller
    end
    interface.close
    raise error if error

    result
end

#roby_app_call_remote_interface(host: "localhost", port: Interface::DEFAULT_PORT) {|client| ... } ⇒ Object

Contact a remote interface and perform some action(s)

The method disconnects from the interface before returning

Yield Parameters:

Yield Returns:

  • (Object)

    object returned by the method



408
409
410
411
412
413
414
415
# File 'lib/roby/test/roby_app_helpers.rb', line 408

def roby_app_call_remote_interface(
    host: "localhost", port: Interface::DEFAULT_PORT
)
    interface = roby_app_interface_module.connect_with_tcp_to(host, port)
    yield(interface) if block_given?
ensure
    interface&.close
end

#roby_app_captured_output(pid) ⇒ nil, {out: String, err: String}

Return the output captured so far for the given PID

If the process has stopped and #roby_app_quit or #assert_roby_app_exits was called, the output is complete. Otherwise it might be partial

Returns:

  • (nil, {out: String, err: String})

    nil if the PID does not exist, or if roby_app_spawn was not configured to capture the output. Otherwise, a hash with the stdout and stderr strings



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/roby/test/roby_app_helpers.rb', line 201

def roby_app_captured_output(pid)
    return unless (spawned = @spawned_pids.find { |p| p.pid == pid })
    return unless (queue = spawned.capture_queue)

    outputs = spawned.captured_output
    until queue.empty?
        output, string = queue.pop
        outputs[output] << string
    end

    outputs.transform_values! { |arr| [arr.join] }
    outputs.transform_values(&:first)
end

#roby_app_create_logfileObject



394
395
396
397
398
399
400
# File 'lib/roby/test/roby_app_helpers.rb', line 394

def roby_app_create_logfile
    require "roby/droby/logfile/writer"
    logfile_dir = make_tmpdir
    logfile_path = File.join(logfile_dir, "logfile")
    writer = DRoby::Logfile::Writer.open(logfile_path)
    [logfile_path, writer]
end

#roby_app_fixture_pathObject

Path to the app test fixtures, that is test/app/fixtures



249
250
251
252
253
254
# File 'lib/roby/test/roby_app_helpers.rb', line 249

def roby_app_fixture_path
    File.expand_path(
        File.join("..", "..", "..", "test", "app", "fixtures"),
        __dir__
    )
end

#roby_app_helpers_default_interface_portObject



127
128
129
130
131
# File 'lib/roby/test/roby_app_helpers.rb', line 127

def roby_app_helpers_default_interface_port
    return roby_interface_port if @interface_server

    Interface::DEFAULT_PORT
end

#roby_app_interface_module(version: @roby_app_interface_version) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/roby/test/roby_app_helpers.rb', line 90

def roby_app_interface_module(version: @roby_app_interface_version)
    require "roby/interface/v#{version}"

    if version == 1
        Roby::Interface::V1
    elsif version == 2
        Roby::Interface::V2
    else
        raise ArgumentError, "invalid interface version #{version}"
    end
end

#roby_app_join_capture_thread(pid) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/roby/test/roby_app_helpers.rb', line 231

def roby_app_join_capture_thread(pid)
    return unless (spawned = @spawned_pids.find { |p| p.pid == pid })
    return unless (thread = spawned.capture_thread)

    thread.join
    roby_app_captured_output(pid)
end

#roby_app_quit(interface, timeout: 2) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/roby/test/roby_app_helpers.rb', line 239

def roby_app_quit(interface, timeout: 2)
    _, status = Process.waitpid2(pid)
    roby_app_join_capture_thread(pid)

    return if status.success?

    raise "roby app with PID #{pid} exited with nonzero status"
end

#roby_app_run(*args, port: nil, silent: false, **options) ⇒ Object



389
390
391
392
# File 'lib/roby/test/roby_app_helpers.rb', line 389

def roby_app_run(*args, port: nil, silent: false, **options)
    pid = roby_app_spawn(*args, port: port, silent: silent, **options)
    assert_roby_app_exits(pid)
end

#roby_app_setup_single_script(*scripts) ⇒ String

Create a minimal Roby application with a given list of scripts copied in scripts/

Parameters:

Returns:

  • (String)

    the path to the app root



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/roby/test/roby_app_helpers.rb', line 262

def roby_app_setup_single_script(*scripts)
    dir = make_tmpdir
    FileUtils.mkdir_p File.join(dir, "config", "robots")
    FileUtils.mkdir_p File.join(dir, "scripts")
    FileUtils.touch File.join(dir, "config", "app.yml")
    FileUtils.touch File.join(dir, "config", "robots", "default.rb")
    scripts.each do |p|
        p = File.expand_path(p, roby_app_fixture_path)
        FileUtils.cp p, File.join(dir, "scripts")
    end
    dir
end

#roby_app_shell_interface(version: @roby_app_interface_version) ⇒ Object



417
418
419
420
421
422
423
# File 'lib/roby/test/roby_app_helpers.rb', line 417

def roby_app_shell_interface(version: @roby_app_interface_version)
    if version == 1
        app.shell_interface
    else
        app.shell_interface_v2
    end
end

#roby_app_spawn(command, *args, port: nil, capture_output: false, silent: false, env: {}, **options) ⇒ Integer

Spawn the roby app process

Returns:

  • (Integer)

    the app PID



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/roby/test/roby_app_helpers.rb', line 340

def roby_app_spawn( # rubocop:disable Metrics/ParameterLists
    command, *args,
    port: nil, capture_output: false, silent: false, env: {}, **options
)
    if capture_output
        out_r, out_w = IO.pipe
        err_r, err_w = IO.pipe
        capture_queue = Queue.new
        capture_thread = roby_app_spawn_output_capture_thread(
            out_r, err_r, capture_queue
        )
        options[:out] = out_w
        options[:err] = err_w
    elsif silent
        options[:out] ||= "/dev/null"
        options[:err] ||= "/dev/null"
    end

    port_args = roby_app_spawn_interface_args(command, port)
    pid = spawn(
        { "ROBY_PLUGIN_PATH" => @roby_plugin_path.join(":") }.merge(env),
        roby_bin, command, *port_args, *args, chdir: app_dir, **options
    )
    out_w&.close
    err_w&.close
    @spawned_pids << SpawnedProcess.new(
        pid: pid,
        capture_thread: capture_thread,
        capture_queue: capture_queue,
        captured_output: { out: [], err: [] }
    )
    pid
end

#roby_app_spawn_interface_args(command, port) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper to determine the "right" interface-related arguments in #roby_app_spawn



326
327
328
329
330
331
332
333
334
335
# File 'lib/roby/test/roby_app_helpers.rb', line 326

def roby_app_spawn_interface_args(command, port)
    port ||= roby_app_allocate_port
    if ROBY_PORT_COMMANDS.include?(command)
        ["--interface-versions=#{@roby_app_interface_version}",
         "--port-v#{@roby_app_interface_version}", port.to_s]
    elsif !ROBY_NO_INTERFACE_COMMANDS.include?(command)
        ["--interface-version=#{@roby_app_interface_version}",
         "--host", "localhost:#{port}"]
    end
end

#roby_app_spawn_output_capture_thread(out_r, err_r, queue) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Start thread that pull data out of a process output pipes



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/roby/test/roby_app_helpers.rb', line 301

def roby_app_spawn_output_capture_thread(out_r, err_r, queue)
    ios = [out_r, err_r]
    Thread.new do
        until ios.empty?
            with_events, = select(ios, [], [])
            with_events.each do |io|
                unless (data = io.read_nonblock(4096))
                    raise EOFError
                end

                queue.push([io == out_r ? :out : :err, data])
            rescue EOFError
                ios.delete(io)
                io.close
            rescue IO::WaitReadable
                # Wait for more data
            end
        end
    end
end

#roby_app_start(*args, port: nil, silent: false, **options) ⇒ (Integer,Roby::Interface::Client)

Start the roby app, and wait for it to be ready

Returns:



378
379
380
381
382
383
# File 'lib/roby/test/roby_app_helpers.rb', line 378

def roby_app_start(*args, port: nil, silent: false, **options)
    port ||= roby_app_allocate_port
    pid = roby_app_spawn(*args, port: port, silent: silent, **options)
    interface = assert_roby_app_is_running(pid, port: port)
    [pid, interface]
end

#roby_app_with_polling(timeout: 2, period: 0.01, message: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/roby/test/roby_app_helpers.rb', line 74

def roby_app_with_polling(timeout: 2, period: 0.01, message: nil)
    start_time = Time.now
    while (Time.now - start_time) < timeout
        if (result = yield)
            return result
        end

        sleep period
    end
    if message
        flunk "#{message} did not happen within #{timeout} seconds"
    else
        flunk "failed to reach expected result within #{timeout} seconds"
    end
end

#roby_binObject



67
68
69
70
71
72
# File 'lib/roby/test/roby_app_helpers.rb', line 67

def roby_bin
    File.expand_path(
        File.join("..", "..", "..", "bin", "roby"),
        __dir__
    )
end

#setupObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/roby/test/roby_app_helpers.rb', line 9

def setup
    @roby_app_interface_version ||= 1
    @roby_plugin_path = []

    require "roby/interface/v#{@roby_app_interface_version}"

    @spawned_pids = []
    super
    @app = Roby::Application.new
    app.public_logs = false
    app.plugins_enabled = false
    @app_dir = make_tmpdir
    app.app_dir = app_dir
    register_plan(@app.plan)
end

#teardownObject



25
26
27
28
29
30
31
# File 'lib/roby/test/roby_app_helpers.rb', line 25

def teardown
    app.stop_log_server
    app.stop_shell_interface
    app.cleanup if app.base_setup_done?
    kill_spawned_pids
    super
end