vendredi 16 juin 2017

How to stop a process from within the tests, when testing a never-ending process?

I am developing a long-running program in Ruby. I am writing some integration tests for this. These tests need to kill or stop the program after starting it; otherwise the tests hang.

For example, with a file bin/runner

#!/usr/bin/env ruby
while true do
  puts "Hello World"
  sleep 10
end

The (integration) test would be:

class RunReflectorTest < TestCase
  test "it prints a welcome message over and over" do
    out, err = capture_subprocess_io do
      system "bin/runner"
    end
    assert_empty err
    assert_includes out, "Hello World"
  end
end

Only, obviously, this will not work; the test starts and never stops, because the system call never ends.

How should I tackle this? Is the problem in system itself, and would Kernel#spawn provide a solution? If so, how?

Aucun commentaire:

Enregistrer un commentaire