Module: Pocolog::TestHelpers

Defined in:
lib/pocolog/test_helpers.rb

Defined Under Namespace

Classes: RecordingReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#int32_tObject (readonly)

For convenience, the test helpers create a int32_t type and use it by default



10
11
12
# File 'lib/pocolog/test_helpers.rb', line 10

def int32_t
  @int32_t
end

Instance Method Details

#assert_report_emptyObject



59
60
61
# File 'lib/pocolog/test_helpers.rb', line 59

def assert_report_empty
    assert @__recording_reporter.messages.empty?
end

#assert_report_includes(kind, message) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pocolog/test_helpers.rb', line 63

def assert_report_includes(kind, message)
    return if @__recording_reporter.match?(kind, message)

    error_message =
        "reporter did not receive any #{kind.inspect} message "\
        "matching #{message}. Received messages:\n  "
    error_message += @__recording_reporter
                     .each_received_message
                     .map { |k, m| "#{k.inspect}: #{m}" }
                     .join("\n  ")

    flunk(error_message)
end

#close_logfileObject

Close the current logfile (either created or opened)



116
117
118
119
120
121
122
123
# File 'lib/pocolog/test_helpers.rb', line 116

def close_logfile
    return unless @__current_logfile

    @__current_logfile.close
    logfile = @__current_logfile
    @__current_logfile = nil
    logfile
end

#create_logfile(basename) ⇒ Object

Create a new logfile

Parameters:

  • filename (String)

    the path to the log file

  • a (Hash<String,Array<Integer>>)

    set of streams that should be created. All streams are created with a type of /int32_t (32 bit, integer, signed). A sample time is always 100 * sample_index + value



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pocolog/test_helpers.rb', line 94

def create_logfile(basename)
    path = logfile_path(basename)
    if File.exist?(index_path = Logfiles.default_index_filename(path))
        FileUtils.rm(index_path)
    end
    @__current_logfile = Pocolog::Logfiles.create(path)

    return path unless block_given?

    begin
        yield
        path
    ensure
        close_logfile
    end
end

#create_logfile_stream(name, samples_rt = [], samples_lg = [], samples_value = [], type: int32_t, metadata: {}) ⇒ Object

Create a stream on the last created logfile



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pocolog/test_helpers.rb', line 130

def create_logfile_stream(
    name, samples_rt = [], samples_lg = [], samples_value = [],
    type: int32_t, metadata: {}
)
    stream = @__current_logfile.create_stream(name, type, )
    samples_rt.zip(samples_lg, samples_value).each do |rt, lg, v|
        stream.write(rt, lg, v)
    end
    @__current_stream = stream
    stream
end

#flush_logfileObject



111
112
113
# File 'lib/pocolog/test_helpers.rb', line 111

def flush_logfile
    @__current_logfile.flush
end

#logfile_path(*basename) ⇒ Object



170
171
172
# File 'lib/pocolog/test_helpers.rb', line 170

def logfile_path(*basename)
    File.expand_path(File.join(*basename), @__logfiles_dir)
end

#logfile_tellObject



125
126
127
# File 'lib/pocolog/test_helpers.rb', line 125

def logfile_tell
    @__current_logfile.io.tell
end

#logfiles_dirObject



166
167
168
# File 'lib/pocolog/test_helpers.rb', line 166

def logfiles_dir
    @__logfiles_dir
end

#move_logfile_path(new_dir, delete_current: true) ⇒ Object



77
78
79
80
81
# File 'lib/pocolog/test_helpers.rb', line 77

def move_logfile_path(new_dir, delete_current: true)
    FileUtils.rm_rf(@__logfiles_dir) if delete_current
    FileUtils.mkdir_p(new_dir)
    @__logfiles_dir = new_dir
end

#open_logfile(path, index_dir: nil, close_current: true) ⇒ Object

Open an existing logfile



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pocolog/test_helpers.rb', line 147

def open_logfile(path, index_dir: nil, close_current: true)
    close_logfile if @__current_logfile && close_current

    logfile = Pocolog::Logfiles.open(logfile_path(path), index_dir: index_dir)
    if block_given?
        begin yield(logfile)
        ensure close_logfile
        end
    else
        logfile
    end
end

#open_logfile_stream(basename, stream_name, close_current: true, index_dir: nil) ⇒ Object



160
161
162
163
164
# File 'lib/pocolog/test_helpers.rb', line 160

def open_logfile_stream(basename, stream_name,
                        close_current: true, index_dir: nil)
    open_logfile(basename, close_current: close_current, index_dir: index_dir)
        .stream(stream_name)
end

#recording_reporterObject



55
56
57
# File 'lib/pocolog/test_helpers.rb', line 55

def recording_reporter
    @__recording_reporter
end

#setupObject



12
13
14
15
16
17
18
# File 'lib/pocolog/test_helpers.rb', line 12

def setup
    @__logfiles_dir = Dir.mktmpdir
    registry = Typelib::Registry.new
    @int32_t = registry.create_numeric '/int32_t', 4, :sint
    @__recording_reporter = RecordingReporter.new
    super
end

#teardownObject



83
84
85
86
# File 'lib/pocolog/test_helpers.rb', line 83

def teardown
    FileUtils.rm_rf @__logfiles_dir
    super
end

#write_logfile_sample(rt, lg, value) ⇒ Object



142
143
144
# File 'lib/pocolog/test_helpers.rb', line 142

def write_logfile_sample(rt, lg, value)
    @__current_stream.write(rt, lg, value)
end