module Roby::Test::ExpectExecution

Test related to execution in a Roby context

This is the public interface of Roby's test swiss-army knife {#expect_execution}

The block given to {#expect_execution} will be executed in Roby's event propagation context, and then some expectations can be matched against the result using the .to call:

expect_execution { ...code to be executed... }.
   to { ... expectations ... }

See the 'Expectations' section of {ExecutionExpectations} for an exhaustive list of existing expectations. Additional setup regarding the processing loop is documented in the Setup section of the same page, and can be used like this:

expect_execution { ...code to be executed... }.
   timeout(10).
   scheduler(true).
   to { ... expectations ... }

The execution expectation object is actually executed when one of the to_run or to { } methods is called. The former runs the block without any expectation (and therefore runs it only once, or until all async work is finished). The latter defines expectations and verifies them.

@example emit an event and validate that it is emitted

plan.add(task = MyTask.new)
expect_execution { task.start! }.
   to { emit task.start_event }

Note that the heavy-lifting is done in {ExecutionExpectations}. This is really only the sugar-coating above the test harness itself.

Constants

Context

The context object that allows the expect_execution { }.to { } syntax

Public Instance Methods

add_expectations(&block) click to toggle source

Add an expectation from within an execute { } or expect_execution { } block

@raise [InvalidContext] if called outside of an

{#expect_execution} context
# File lib/roby/test/expect_execution.rb, line 142
def add_expectations(&block)
    if !@current_expect_execution
        raise InvalidContext, "#add_expectations not called within an expect_execution context"
    end
    @current_expect_execution.expectations.parse(ret: false, &block)
end
execute(plan: self.plan, garbage_collect: false) { || ... } click to toggle source

Execute a block within the event propagation context

# File lib/roby/test/expect_execution.rb, line 120
def execute(plan: self.plan, garbage_collect: false)
    result = nil
    expect_execution(plan: plan) { result = yield }.garbage_collect(garbage_collect).to_run
    result
rescue Minitest::Assertion => e
    raise e, e.message, caller(2)
end
execute_one_cycle(plan: self.plan, scheduler: false, garbage_collect: false) click to toggle source

Run exactly once cycle

# File lib/roby/test/expect_execution.rb, line 129
def execute_one_cycle(plan: self.plan, scheduler: false, garbage_collect: false)
    expect_execution(plan: plan).
        join_all_waiting_work(false).
        scheduler(scheduler).
        garbage_collect(garbage_collect).
        to_run
end
expect_execution(plan: self.plan, &block) click to toggle source

Declare expectations about the execution of a code block

See the documentation of {ExpectExecution} for more details

@raise [InvalidContext] if expect_execution is used from within an

expect_execution context, or within propagation context
# File lib/roby/test/expect_execution.rb, line 92
def expect_execution(plan: self.plan, &block)
    if plan.execution_engine.in_propagation_context?
        raise InvalidContext, "cannot recursively call #expect_execution"
    end
    expectations = ExecutionExpectations.new(self, plan)
    Context.new(self, expectations, block)
end
reset_current_expect_execution() click to toggle source

@api private

Reset the current expect_execution block. This is used to check for recursive calls to {#expect_execution}

# File lib/roby/test/expect_execution.rb, line 115
def reset_current_expect_execution
    @current_expect_execution = nil
end
setup_current_expect_execution(context) click to toggle source

@api private

Set the current expect_execution context. This is used to check for recursive calls to {#expect_execution}

# File lib/roby/test/expect_execution.rb, line 104
def setup_current_expect_execution(context)
    if @current_expect_execution
        raise InvalidContext, "cannot perform an expect_execution test within another one"
    end
    @current_expect_execution = context
end