Skip to content

Instantly share code, notes, and snippets.

@tancnle
Last active December 10, 2015 13:08
Show Gist options
  • Save tancnle/4438701 to your computer and use it in GitHub Desktop.
Save tancnle/4438701 to your computer and use it in GitHub Desktop.
Testing Parallel block in RSpec

Problem:

We have the following Parallel block which spin up multiple parallel processes

  Parallel.map(
    servers,
    :in_processes     => servers.size,
    :preserve_results => false,
  ) do |server|
    downtime(server)
    server.destroy
    server.create
    server.init
    cancel_downtime(server)
  end

It turns out to be impossible to verify the order of these steps in our spec

Solution:

Added the following lines in before(:each) block

  Parallel.stub(:work_in_threads) { |arr, opts, &block| Parallel.send(:work_direct, arr, opts, &block) }
  Parallel.stub(:work_in_processes) { |arr, opts, &block| Parallel.send(:work_direct, arr, opts, &block) }

Then happily proceed with our test

  server.should_receive(:disable_monitoring).ordered
  server.should_receive(:destroy).ordered
  server.should_receive(:create).ordered
  server.should_receive(:enable_monitoring).ordered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment