Class: ParallelRSpec::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_rspec/channel.rb

Overview

Adapted from nitra. Copyright 2012-2013 Roger Nesbitt, Powershop Limited, YouDo Limited. MIT licence.

Constant Summary collapse

ProtocolInvalidError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rd, wr) ⇒ Channel

Returns a new instance of Channel.



10
11
12
13
14
15
# File 'lib/parallel_rspec/channel.rb', line 10

def initialize(rd, wr)
  @rd = rd
  @wr = wr
  @rd.binmode
  @wr.binmode
end

Instance Attribute Details

#raise_epipe_on_write_errorObject

Returns the value of attribute raise_epipe_on_write_error.



8
9
10
# File 'lib/parallel_rspec/channel.rb', line 8

def raise_epipe_on_write_error
  @raise_epipe_on_write_error
end

#rdObject (readonly)

Returns the value of attribute rd.



7
8
9
# File 'lib/parallel_rspec/channel.rb', line 7

def rd
  @rd
end

#wrObject (readonly)

Returns the value of attribute wr.



7
8
9
# File 'lib/parallel_rspec/channel.rb', line 7

def wr
  @wr
end

Class Method Details

.pipeObject



17
18
19
20
21
# File 'lib/parallel_rspec/channel.rb', line 17

def self.pipe
  c_rd, s_wr = IO.pipe("ASCII-8BIT", "ASCII-8BIT")
  s_rd, c_wr = IO.pipe("ASCII-8BIT", "ASCII-8BIT")
  [new(c_rd, c_wr), new(s_rd, s_wr)]
end

.read_select(channels) ⇒ Object



23
24
25
26
27
28
# File 'lib/parallel_rspec/channel.rb', line 23

def self.read_select(channels)
  fds = IO.select(channels.collect(&:rd))
  fds.first.collect do |fd|
    channels.detect {|c| c.rd == fd}
  end
end

Instance Method Details

#closeObject



30
31
32
33
# File 'lib/parallel_rspec/channel.rb', line 30

def close
  rd.close
  wr.close
end

#readObject



35
36
37
38
39
40
41
42
43
# File 'lib/parallel_rspec/channel.rb', line 35

def read
  return unless line = rd.gets
  if result = line.strip.match(/\ACOMMAND,(\d+)\z/)
    data = rd.read(result[1].to_i)
    Marshal.load(data)
  else
    raise ProtocolInvalidError, "Expected command length line, got #{line.inspect}"
  end
end

#write(data) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/parallel_rspec/channel.rb', line 45

def write(data)
  encoded = Marshal.dump(data)
  wr.write("COMMAND,#{encoded.bytesize}\n")
  wr.write(encoded)
  wr.flush
rescue Errno::EPIPE
  raise if raise_epipe_on_write_error
end