finished messaging system for ssh and pipes

This commit is contained in:
Nick Gauthier 2010-01-27 15:57:26 -05:00
parent 2fa15b735b
commit 3414ee5c20
4 changed files with 16 additions and 1 deletions

View File

@ -19,6 +19,11 @@ module Hydra #:nodoc:
end end
end end
def close
@reader.close if @reader
@writer.close if @writer
end
class UnprocessableMessage < RuntimeError class UnprocessableMessage < RuntimeError
attr_accessor :message attr_accessor :message
def initialize(message = "Message expected") def initialize(message = "Message expected")

View File

@ -5,6 +5,9 @@ class TestPipe < Test::Unit::TestCase
setup do setup do
@pipe = Hydra::Pipe.new @pipe = Hydra::Pipe.new
end end
teardown do
@pipe.close
end
should "be able to write messages" do should "be able to write messages" do
child = Process.fork do child = Process.fork do
@pipe.identify_as_child @pipe.identify_as_child
@ -16,6 +19,7 @@ class TestPipe < Test::Unit::TestCase
@pipe.write Hydra::Messages::TestMessage.new(:text => "Test Message") @pipe.write Hydra::Messages::TestMessage.new(:text => "Test Message")
assert_equal "Message Received", @pipe.gets.text assert_equal "Message Received", @pipe.gets.text
assert_equal "Second Message", @pipe.gets.text assert_equal "Second Message", @pipe.gets.text
Process.wait(child) #ensure it quits, so there is nothing to write to
assert_raise IOError do assert_raise IOError do
@pipe.write Hydra::Messages::TestMessage.new(:text => "anyone there?") @pipe.write Hydra::Messages::TestMessage.new(:text => "anyone there?")
end end

View File

@ -4,12 +4,15 @@ class TestRunner < Test::Unit::TestCase
context "a test runner" do context "a test runner" do
setup do setup do
@pipe = Hydra::Pipe.new @pipe = Hydra::Pipe.new
Process.fork do @child = Process.fork do
@pipe.identify_as_child @pipe.identify_as_child
Hydra::Runner.new(@pipe) Hydra::Runner.new(@pipe)
end end
@pipe.identify_as_parent @pipe.identify_as_parent
end end
teardown do
Process.wait(@child)
end
should "request a file on boot" do should "request a file on boot" do
assert @pipe.gets.is_a?(Hydra::Messages::RunnerRequestsFile) assert @pipe.gets.is_a?(Hydra::Messages::RunnerRequestsFile)
end end

View File

@ -10,6 +10,9 @@ class TestSSH < Test::Unit::TestCase
) )
@message = Hydra::Messages::TestMessage.new @message = Hydra::Messages::TestMessage.new
end end
teardown do
@ssh.close
end
should "be able to execute a command" do should "be able to execute a command" do
@ssh.write @message @ssh.write @message
assert_equal @message.text, @ssh.gets.text assert_equal @message.text, @ssh.gets.text