Module: RSMP::Validator::Helpers::Handshake

Defined in:
lib/rsmp/validator/helpers/handshake.rb

Overview

Helpers for validating the sequence of messages during RSMP connection establishment.

Constant Summary collapse

EXPECTED_VERSION_EXCHANGE_MESSAGES =
[
  'in:Version',
  'out:MessageAck',
  'out:Version',
  'in:MessageAck'
].freeze
EXPECTED_WATCHDOG_EXCHANGE_MESSAGES =
[
  'in:Watchdog',
  'out:MessageAck',
  'out:Watchdog',
  'in:MessageAck'
].freeze
EXPECTED_COMPONENT_LIST_MESSAGES =
[
  'in:ComponentList',
  'out:MessageAck'
].freeze

Instance Method Summary collapse

Instance Method Details

#check_sequence(version) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rsmp/validator/helpers/handshake.rb', line 95

def check_sequence(version)
  case version
  when '3.1.1', '3.1.2', '3.1.3'
    check_sequence_v311_to_v313 version
  when '3.1.4', '3.1.5', '3.2', '3.2.1', '3.2.2'
    check_sequence_v314_or_later version
  when '3.3.0'
    check_sequence_v330 version
  else
    raise "Unknown rsmp version #{version}"
  end
end

#check_sequence_v311_to_v313(core_version) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rsmp/validator/helpers/handshake.rb', line 44

def check_sequence_v311_to_v313(core_version)
  expected_version_messages = EXPECTED_VERSION_EXCHANGE_MESSAGES
  expected_watchdog_messages = EXPECTED_WATCHDOG_EXCHANGE_MESSAGES

  length = expected_version_messages.length + expected_watchdog_messages.length
  got = get_connection_message core_version, length

  expect_sequence_part!(
    got[0..3],
    expected: expected_version_messages,
    forbidden: ['in:AggregatedStatus', 'in:Watchdog', 'in:Alarm'],
    context: 'version exchange'
  )

  expect_sequence_part!(
    got[4..7],
    expected: expected_watchdog_messages,
    forbidden: ['in:AggregatedStatus', 'in:Alarm'],
    context: 'watchdog exchange'
  )
end

#check_sequence_v314_or_later(version) ⇒ Object



81
82
83
84
85
# File 'lib/rsmp/validator/helpers/handshake.rb', line 81

def check_sequence_v314_or_later(version)
  expected = EXPECTED_VERSION_EXCHANGE_MESSAGES + EXPECTED_WATCHDOG_EXCHANGE_MESSAGES
  got = get_connection_message version, expected.length
  assert(got == expected, "Expected connection sequence #{expected.inspect}, got #{got.inspect}")
end

#check_sequence_v330(version) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rsmp/validator/helpers/handshake.rb', line 87

def check_sequence_v330(version)
  expected = EXPECTED_VERSION_EXCHANGE_MESSAGES +
             EXPECTED_WATCHDOG_EXCHANGE_MESSAGES +
             EXPECTED_COMPONENT_LIST_MESSAGES
  got = get_connection_message version, expected.length
  assert(got == expected, "Expected connection sequence #{expected.inspect}, got #{got.inspect}")
end

#expect_sequence_part!(got_part, expected:, forbidden:, context:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rsmp/validator/helpers/handshake.rb', line 66

def expect_sequence_part!(got_part, expected:, forbidden:, context:)
  forbidden.each do |message|
    type = message.split(':').last
    assert(
      !got_part.include?(message),
      "#{type} not allowed during #{context}: #{got_part}"
    )
  end

  assert(
    got_part.tally == expected.tally,
    "Wrong #{context} part, must contain #{expected}, got #{got_part}"
  )
end

#get_connection_message(core_version, length) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rsmp/validator/helpers/handshake.rb', line 25

def get_connection_message(core_version, length)
  timeout = RSMP::Validator.get_config('timeouts', 'ready')
  got = nil

  RSMP::Validator::SiteTester.isolated(
    'collect' => { timeout: timeout, num: length, ingoing: true, outgoing: true },
    'sites' => { 'default' => { 'rsmp_versions' => [core_version] } }
  ) do |task, _supervisor, site|
    assert(site.ready?, 'expected site to be ready')
    collector = site.collector
    collector.use_task task
    collector.wait!
    got = collector.messages.map { |message| "#{message.direction}:#{message.type}" }
  end
  got
rescue Async::TimeoutError
  raise "Did not collect #{length} messages within #{timeout}s"
end