Class: Roby::Test::Spec

Inherits:
Minitest::Spec
  • Object
show all
Extended by:
DSL
Includes:
Assertions, MinitestHelpers, RobotTestHelpers, RunPlanners, TeardownPlans, Utilrb::Timepoints
Defined in:
lib/roby/test/spec.rb

Constant Summary

Constants included from ExpectExecution

ExpectExecution::SETUP_METHODS

Instance Attribute Summary collapse

Attributes included from ExpectExecution

#expect_execution_default_timeout

Attributes included from TeardownPlans

#default_teardown_poll, #registered_plans

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

describe, extended, included, roby_should_run, run_if, run_interactive, run_live, run_on_robot, run_simulated, run_single

Methods included from RobotTestHelpers

#execute_robot_controller

Methods included from RunPlanners

deregister_planning_handler, planner_handler_for, #run_planners, setup_planning_handlers

Methods included from MinitestHelpers

#assert_raises, #register_failure, #roby_exception_to_string, #roby_find_matching_exception, #sanitize_exception

Methods included from ExpectExecution

#add_expectations, #execute, #execute_one_cycle, #expect_execution, #reset_current_expect_execution, #setup_current_expect_execution

Methods included from TeardownPlans

#clear_registered_plans, #initialize, #register_plan, #teardown_clear, #teardown_forced_killall, #teardown_killall, #teardown_registered_plans, #teardown_warn

Methods included from Assertions

#__capture_log, #assert_adds_error, #assert_adds_framework_error, #assert_child_of, #assert_droby_compatible, #assert_event_becomes_unreachable, #assert_event_command_failed, #assert_event_emission, #assert_event_emission_failed, #assert_event_exception, #assert_event_is_unreachable, #assert_exception_can_be_pretty_printed, #assert_fatal_exception, #assert_free_event_command_failed, #assert_free_event_emission_failed, #assert_free_event_exception, #assert_free_event_exception_warning, #assert_handled_exception, #assert_logs_event, #assert_logs_exception_with_backtrace, #assert_nonfatal_exception, #assert_notifies_free_event_exception, #assert_pp, #assert_relative_error, #assert_same_position, #assert_sets_equal, #assert_state_machine_transition, #assert_task_fails_to_start, #assert_task_quarantined, #capture_log, #create_exception_matcher, #droby_local_marshaller, #droby_remote_marshaller, #droby_to_remote, #droby_transfer, #find_state_machine_capture, #refute_child_of, #roby_make_flexmock_exception_matcher, #roby_normalize_exception_message, #run_state_machine_capture, #validate_state_machine

Instance Attribute Details

#models_present_in_setupObject (readonly)

Set of models present during #setup

This is used to clear all the models created during the test in #teardown



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

def models_present_in_setup
  @models_present_in_setup
end

Class Method Details

.roby_plan_with(matcher, handler) ⇒ Object

Declare what #roby_run_planner should use to develop a given task during a test

The latest handler registered wins

Parameters:



226
227
228
# File 'lib/roby/test/spec.rb', line 226

def self.roby_plan_with(matcher, handler)
    RunPlanners.roby_plan_with(matcher, handler)
end

.test_methodsObject



46
47
48
49
50
51
52
# File 'lib/roby/test/spec.rb', line 46

def self.test_methods
    methods = super
    # Duplicate each method 'repeat' times
    methods.inject([]) do |list, m|
        list.concat([m] * Roby.app.test_repeat)
    end
end

Instance Method Details

#__full_name__Object



54
55
56
# File 'lib/roby/test/spec.rb', line 54

def __full_name__
    "#{self.class}##{name}"
end

#appObject



28
29
30
# File 'lib/roby/test/spec.rb', line 28

def app
    Roby.app
end

#clear_newly_defined_modelsObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/roby/test/spec.rb', line 130

def clear_newly_defined_models
    app.root_models.each do |root_model|
        ([root_model] + root_model.each_submodel.to_a).each do |m|
            unless models_present_in_setup.include?(m)
                m.permanent_model = false
                m.clear_model
            end
        end
    end
end

#disable_event_reportingObject



89
90
91
# File 'lib/roby/test/spec.rb', line 89

def disable_event_reporting
    plan.event_logger.enabled = false
end

#enable_event_reportingObject



85
86
87
# File 'lib/roby/test/spec.rb', line 85

def enable_event_reporting
    plan.event_logger.enabled = true
end

#engineObject



36
37
38
39
40
# File 'lib/roby/test/spec.rb', line 36

def engine
    Roby.warn_deprecated "#engine is deprecated, "\
                         "use #execution_engine instead"
    execution_engine
end

#ensure_timecop_not_activeObject

Fails the test if Timecop is active

This is called in #setup to ensure that any test suite that does not use the Roby harness would not have forgotten to call Timecop.return

This helps with debugging these issues, which can be pretty hard to find



99
100
101
102
103
104
105
106
107
108
# File 'lib/roby/test/spec.rb', line 99

def ensure_timecop_not_active
    return unless Timecop.frozen? || Timecop.travelled? || Timecop.scaled?

    flunk(
        "timecop left frozen from previous tests. The Roby " \
        "test harness takes care of calling Timecop.return, but " \
        "you probably have some tests that do not use that harness " \
        "and use Timecop. Remember calling Timecop.return during teardown"
    )
end

#execution_engineObject



42
43
44
# File 'lib/roby/test/spec.rb', line 42

def execution_engine
    app.execution_engine
end

#inhibit_fatal_messages(&block) ⇒ Object

Deprecated.

use capture_log instead



194
195
196
197
198
# File 'lib/roby/test/spec.rb', line 194

def inhibit_fatal_messages(&block)
    Roby.warn_deprecated "#{__method__} is deprecated, "\
                         "use capture_log instead"
    with_log_level(Roby, Logger::FATAL, &block)
end

#planObject



32
33
34
# File 'lib/roby/test/spec.rb', line 32

def plan
    app.plan
end

#process_events(timeout: 10, **options, &caller_block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/roby/test/spec.rb', line 141

def process_events(timeout: 10, **options, &caller_block)
    Roby.warn_deprecated "do not use #process_events. Use the "\
                         "expect_execution infrastructure instead"

    exceptions = []
    first_pass = true
    while first_pass || execution_engine.has_waiting_work?
        first_pass = false

        execution_engine.join_all_waiting_work(timeout: timeout)
        execution_engine.start_new_cycle
        errors = execution_engine.process_events(
            **options, &caller_block
        )
        caller_block = nil
        exceptions.concat(errors.exceptions)
        execution_engine.cycle_end({})
    end

    unless exceptions.empty?
        if exceptions.size == 1
            raise exceptions.first.exception
        else
            raise SynchronousEventProcessingMultipleErrors.new(
                exceptions.map(&:exception)
            )
        end
    end
end

#process_events_until(timeout: 5, **options) ⇒ Object

Repeatedly process events until a condition is met

Yield Returns:

  • (Boolean)

    true if the condition is met, false otherwise



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

def process_events_until(timeout: 5, **options)
    Roby.warn_deprecated "do not use #process_events. Use the "\
                         "expect_execution infrastructure with "\
                         "the 'achieve' expectation instead"

    start = Time.now
    until yield
        now = Time.now
        remaining = timeout - (now - start)
        if remaining < 0
            flunk("failed to reach expected condition "\
                  "within #{timeout} seconds")
        end
        process_events(timeout: remaining, **options)
        sleep 0.01
    end
end

#roby_run_planner(root_task, recursive: true, **options) ⇒ Object

Deprecated.


214
215
216
217
218
# File 'lib/roby/test/spec.rb', line 214

def roby_run_planner(root_task, recursive: true, **options)
    Roby.warn_deprecated "#{__method__} is deprecated, "\
                         "use run_planners instead"
    run_planners(root_task, recursive: recursive, **options)
end

#runObject

Filters out the test suites that are not enabled by the current Roby configuration



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/roby/test/spec.rb', line 232

def run
    begin
        time_it do
            self.class.roby_should_run(self, app)
        end
    rescue Minitest::Skip
        return Minitest::Result.from(self)
    end

    super
end

#setupObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/roby/test/spec.rb', line 64

def setup
    ensure_timecop_not_active

    plan.execution_engine.display_exceptions = false
    # Mark every app-defined model as permanent, so that the tests
    # can define their own and get cleanup up properly on teardown
    @models_present_in_setup = Set.new
    app.root_models.each do |root_model|
        models_present_in_setup << root_model
        root_model.each_submodel do |m|
            models_present_in_setup << m
        end
    end
    register_plan(plan)

    @plan_original_event_logger = plan.event_logger
    plan.event_logger = Roby::Test::EventReporter.new(STDOUT)

    super
end

#teardownObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/roby/test/spec.rb', line 110

def teardown
    Timecop.return

    begin
        super
    rescue ::Exception => e
        teardown_failure = e
    end

    teardown_registered_plans
    app.run_shutdown_blocks
ensure
    plan.event_logger = @plan_original_event_logger

    clear_registered_plans
    if teardown_failure
        raise teardown_failure
    end
end

#with_log_level(log_object, level) ⇒ Object

Deprecated.

use capture_log instead



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

def with_log_level(log_object, level)
    Roby.warn_deprecated "#{__method__} is deprecated, "\
                         "use capture_log instead"
    log_object = log_object.logger if log_object.respond_to?(:logger)
    current_level = log_object.level
    log_object.level = level

    yield
ensure
    log_object.level = current_level if current_level
end