Sha256: a1ee79db98e0509625693608cccabf62d6856737c51e8d797c6b2cdaa1e4ee44

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

require 'spec_helper'

describe BackgroundProcess::IOHelpers do
  describe "#detect" do
    it "iterates over the lines from each stream as they are available, and returns when a non-false value is reached" do
      process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.1; do a=$(($a + 1)); echo $a; echo $a 1>&2; done'")
      outputs = {:out => [], :err => []}

      result = BackgroundProcess::IOHelpers.detect([process.stderr, process.stdout], 1) do |io, line|
        outputs[io == process.stderr ? :err : :out] << line.to_i
        true if outputs.values.map {|o| o.length }.uniq == [5]
      end
      outputs[:out].should == (1..5).to_a
      outputs[:err].should == (1..5).to_a
      result.should be_true
    end

    it "gives up after the timeout is reached" do
      process = BackgroundProcess.run("bash -c 'sleep 2; echo done'")
      started_at = Time.now

      result = BackgroundProcess::IOHelpers.detect([process.stderr, process.stdout], 0.5) do |io, line|
        true if line.strip == "done"
      end
      result.should be_nil
      (Time.now - started_at).should < 0.6
      process.kill
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
background_process-1.3 spec/background_process/io_helpers_spec.rb
vim-jar-0.1.2.0001 bundler/ruby/1.8/gems/background_process-1.2/spec/background_process/io_helpers_spec.rb
vim-jar-0.1.2 bundler/ruby/1.8/gems/background_process-1.2/spec/background_process/io_helpers_spec.rb
vim-jar-0.1.1 bundler/ruby/1.8/gems/background_process-1.2/spec/background_process/io_helpers_spec.rb
vim-jar-0.1.0 bundler/ruby/1.8/gems/background_process-1.2/spec/background_process/io_helpers_spec.rb
background_process-1.2 spec/background_process/io_helpers_spec.rb
background_process-1.1 spec/background_process/io_helpers_spec.rb
background_process-1.0 spec/background_process/io_helpers_spec.rb