Module: Yes::Core::TestSupport::SubscriptionsHelper

Includes:
RSpec::Support::InSubProcess
Defined in:
lib/yes/core/test_support/subscriptions_helper.rb

Overview

RSpec helper for asserting that PgEventstore subscriptions start correctly.

Examples:

RSpec.describe 'Subscriptions' do
  include Yes::Core::TestSupport::SubscriptionsHelper

  it 'starts all subscriptions' do
    assert_running_subscriptions('eventstore/subscriptions.rb', 5)
  end
end

Instance Method Summary collapse

Instance Method Details

#assert_running_subscriptions(*subscriptions_paths, number_of_subscriptions, root: './lib/tasks', timeout: 5) ⇒ void

This method returns an undefined value.

Asserts that the expected number of subscriptions are running.

Parameters:

  • subscriptions_paths (Array<String>)

    relative paths to subscription files

  • number_of_subscriptions (Integer)

    expected number of running subscriptions

  • root (String) (defaults to: './lib/tasks')

    root directory of subscription files

  • timeout (Integer) (defaults to: 5)

    timeout in seconds for subscriptions to start



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yes/core/test_support/subscriptions_helper.rb', line 31

def assert_running_subscriptions(*subscriptions_paths, number_of_subscriptions, root: './lib/tasks', timeout: 5)
  GRPC.prefork if defined?(GRPC)
  in_sub_process do
    GRPC.postfork_child if defined?(GRPC)
    setup_eventstore_cli
    require_options = subscriptions_paths.flat_map { |path| ['-r', "#{root}/#{path}"] }
    runner = Thread.new { PgEventstore::CLI.execute(['subscriptions', 'start', *require_options]) }
    subscriptions_count = poll_subscriptions(number_of_subscriptions, timeout)

    runner.exit
    aggregate_failures do
      expect(subscriptions_count['count_running']).to eq(number_of_subscriptions)
      expect(subscriptions_count['count_all']).to eq(number_of_subscriptions)
    end
  rescue StandardError => e
    Rails.logger.debug e.message
    Rails.logger.debug e.backtrace
    raise e
  end
  nil
ensure
  GRPC.postfork_parent if defined?(GRPC)
end