<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -4,7 +4,7 @@ module DeepTest
 
     def initialize(options)
       super(0, options, ListenerList.new([]))
-      @thread = Thread.new { execute }
+      @thread = Thread.new { execute(StringIO.new, StringIO.new) }
       @work_done = 0
     end
 </diff>
      <filename>infrastructure/thread_agent.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,12 +9,17 @@ module DeepTest
       @options = options
     end
 
-    def connect
+    def connect(stream_to_parent_process)
+      DeepTest.logger.debug { &quot;Agent: Connecting to #{@options.origin_hostname}:#{@options.server_port}&quot; }
       @wire = Telegraph::Wire.connect(@options.origin_hostname, @options.server_port)
+      stream_to_parent_process.puts &quot;Connected&quot;
+    ensure
+      stream_to_parent_process.close rescue nil
     end
 
-    def execute
-      connect
+    def execute(stream_from_child_process, stream_to_parent_process)
+      stream_from_child_process.close
+      connect stream_to_parent_process
 
       reseed_random_numbers
       reconnect_to_database</diff>
      <filename>lib/deep_test/agent.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,12 +1,5 @@
 require 'set'
 
-Signal.trap(&quot;USR2&quot;) do
-  caller.each do |line|
-    puts line
-  end
-  raise
-end
-
 module DeepTest
   class CentralCommand
     attr_reader :operator
@@ -39,8 +32,8 @@ module DeepTest
           if @results.any?
             return @results.shift
           else
-            raise NoAgentsRunningError unless @switchboard.any_live_wires?
             @results_condvar.wait @results_mutex
+            raise NoAgentsRunningError unless @results.any? || @switchboard.any_live_wires?
           end
         end
       end</diff>
      <filename>lib/deep_test/central_command.rb</filename>
    </modified>
    <modified>
      <diff>@@ -63,18 +63,17 @@ module DeepTest
 
         loop do
           begin
-            message, wire = switchboard.next_message :timeout =&gt; 1
-
-            case message.body
-            when LoadFiles
-              load_files message.body.files
-            when DeployAgents
-              deploy_agents
-              wire.send_message Done
-              operator.shutdown
+            switchboard.process_messages :timeout =&gt; 1 do |message, wire|
+              case message.body
+              when LoadFiles
+                load_files message.body.files
+              when DeployAgents
+                deploy_agents
+                wire.send_message Done
+                operator.shutdown
+                break
+              end
             end
-          rescue Telegraph::NoMessageAvailable
-            retry
           end
         end
       end</diff>
      <filename>lib/deep_test/distributed/beachhead.rb</filename>
    </modified>
    <modified>
      <diff>@@ -34,6 +34,7 @@ module DeepTest
       end
 
       def deploy_agents
+        DeepTest.logger.debug { &quot;RemoteDeployment deploying agents with #{@landing_fleet}&quot; }
         @landing_fleet.deploy_agents
       rescue =&gt; e
         raise if failed_over?
@@ -42,6 +43,7 @@ module DeepTest
       end
 
       def fail_over(method, exception)
+        DeepTest.logger.debug { &quot;RemoteDeployment failing over on #{method}.&quot; }
         @options.ui_instance.distributed_failover_to_local(method, exception)
         @landing_fleet = @failover_deployment
       end</diff>
      <filename>lib/deep_test/distributed/remote_deployment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,11 +1,19 @@
 module DeepTest
   module FailureMessage
     def self.show(title, message, width = 70)
-      puts &quot; #{title} &quot;.center(width, '*')
+      lines = [&quot; #{title} &quot;.center(width, '*')]
       message.each do |line|
-        puts &quot;* #{line.strip}&quot;.ljust(width - 1) + &quot;*&quot;
+        lines &lt;&lt; &quot;* #{line.strip}&quot;.ljust(width - 1) + &quot;*&quot;
+      end
+      lines &lt;&lt;  &quot;*&quot; * width
+      string = lines.join(&quot;\n&quot;)
+      begin
+        puts string
+      rescue
+        IO.new(2) do |err|
+          err.puts string
+        end
       end
-      puts &quot;*&quot; * width
     end
   end
 end</diff>
      <filename>lib/deep_test/failure_message.rb</filename>
    </modified>
    <modified>
      <diff>@@ -17,9 +17,20 @@ module DeepTest
 
     def deploy_agents
       DeepTest.logger.debug { &quot;Deploying #{number_of_agents} #{@agent_class}s&quot; }
+      wait_for_connect_threads = []
       each_agent do |agent_num|
-        warlock.start &quot;agent #{agent_num}&quot;, @agent_class.new(agent_num, @options, @options.new_listener_list)
+        stream_from_child_process, stream_to_parent_process = IO.pipe
+        warlock.start &quot;agent #{agent_num}&quot;, @agent_class.new(agent_num, @options, @options.new_listener_list), 
+                      stream_from_child_process, stream_to_parent_process
+        wait_for_connect_threads &lt;&lt; Thread.new do
+          stream_to_parent_process.close
+          message = stream_from_child_process.read
+          stream_from_child_process.close
+          raise &quot;Agent was not able to connect: #{message}&quot; unless message == &quot;Connected\n&quot;
+        end
       end        
+
+      wait_for_connect_threads.each { |t| t.join }
     end
 
     def number_of_agents</diff>
      <filename>lib/deep_test/local_deployment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -20,7 +20,7 @@ module DeepTest
         @options.new_listener_list.before_starting_agents
         @deployment.deploy_agents
         begin
-          DeepTest.logger.debug { &quot;Loader Starting (#{$$})&quot; }
+          DeepTest.logger.debug { &quot;Main: About to process work units (#{$$})&quot; }
           passed = @runner.process_work_units(central_command)
         ensure
           shutdown</diff>
      <filename>lib/deep_test/main.rb</filename>
    </modified>
    <modified>
      <diff>@@ -25,6 +25,7 @@ module DeepTest
             #
             @demons_semaphore.unlock if @demons_semaphore.locked?
 
+            close_open_network_connections
             demon.forked name, @options, demon_args
 
             exit
@@ -42,6 +43,15 @@ module DeepTest
       end
     end
 
+    def close_open_network_connections
+      ObjectSpace.each_object(BasicSocket) do |sock|
+        begin
+          sock.close 
+        rescue IOError
+        end
+      end
+    end
+
     def demon_count
       @demons_semaphore.synchronize do
         @demons.size</diff>
      <filename>lib/deep_test/warlock.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,9 +5,9 @@ module Telegraph
     def self.logger
       @logger ||= begin
         l = Logger.new($stdout)
-        l.level = Logger::INFO
+        l.level = Logger.const_get((ENV['TELEGRAPH_LOG_LEVEL'] || 'info').upcase)
         l.formatter = proc do |sev, time, progmane, msg|
-          &quot;[#{time.strftime &quot;%T&quot;}] #{msg}\n&quot;
+          &quot;[#{time.strftime &quot;%T&quot;}] (pid #{Process.pid}) #{msg}\n&quot;
         end
         l
       end</diff>
      <filename>lib/telegraph/logging.rb</filename>
    </modified>
    <modified>
      <diff>@@ -14,10 +14,22 @@ module Telegraph
       @socket = socket
       @switchboard = switchboard
       @accept_thread = Thread.new do
+        @socket.listen 100
         loop do
-          client = @socket.accept
-          debug { &quot;Accepted connection: #{client.inspect}&quot; }
-          @switchboard.add_wire Wire.new(client)
+          if @should_shutdown
+            @socket.close
+            @switchboard.close_all_wires
+            break
+          end
+
+          begin
+            client = @socket.accept_nonblock
+            debug { &quot;Accepted connection: #{client.inspect}&quot; }
+            @switchboard.add_wire Wire.new(client)
+          rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
+            connection_ready, = IO.select([@socket], nil, nil, 0.25)
+            retry if connection_ready
+          end
         end
       end
     end
@@ -28,11 +40,8 @@ module Telegraph
 
     def shutdown
       debug { &quot;Shutting down&quot; }
-      begin
-        @socket.close
-      ensure
-        @switchboard.close_all_wires
-      end
+      @should_shutdown = true
+      @accept_thread.join
     end
   end
 end</diff>
      <filename>lib/telegraph/operator.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,9 +9,10 @@ module Telegraph
     end
 
     def next_message(options = {:timeout =&gt; 0})
-      debug { &quot;Waiting for next message on any wire&quot; }
+      debug { &quot;Waiting for next message on any of #{live_wires.size} wires for #{options[:timeout]} seconds&quot; }
 
       if live_wires.empty?
+        sleep 0.01
         Thread.pass 
         raise NoMessageAvailable 
       end</diff>
      <filename>lib/telegraph/switchboard.rb</filename>
    </modified>
    <modified>
      <diff>@@ -20,8 +20,12 @@ module Telegraph
     end
 
     def close
-      debug { &quot;closing stream&quot; }
-      @stream.close
+      if @stream.closed?
+        debug { &quot;stream already closed&quot; }
+      else
+        debug { &quot;closing stream #{@stream.inspect}&quot; }
+        @stream.close
+      end
     end
 
     def closed?</diff>
      <filename>lib/telegraph/wire.rb</filename>
    </modified>
    <modified>
      <diff>@@ -8,7 +8,7 @@ module DeepTest
       central_command.write_work Test::WorkUnit.new(TestFactory.passing_test)
       central_command.done_with_work
 
-      Agent.new(0, options, stub_everything).execute
+      Agent.new(0, options, stub_everything).execute(StringIO.new, StringIO.new)
 
       assert_equal [], central_command.switchboard.live_wires.first.unacked_messages
       assert_kind_of ::Test::Unit::TestResult, central_command.take_result
@@ -21,7 +21,7 @@ module DeepTest
       central_command.write_work Test::WorkUnit.new(TestFactory.failing_test)
       central_command.done_with_work
 
-      Agent.new(0, options, stub_everything).execute
+      Agent.new(0, options, stub_everything).execute(StringIO.new, StringIO.new)
 
       result_1 = central_command.take_result
       result_2 = central_command.take_result
@@ -37,7 +37,7 @@ module DeepTest
       listener = stub_everything
       agent = Agent.new(0, options, listener)
       listener.expects(:starting).with(agent)
-      agent.execute
+      agent.execute(StringIO.new, StringIO.new)
     end
 
     test &quot;notifies listener that it is about to do work&quot; do
@@ -49,7 +49,7 @@ module DeepTest
       listener = stub_everything
       agent = Agent.new(0, options, listener)
       listener.expects(:starting_work).with(agent, work_unit)
-      agent.execute
+      agent.execute(StringIO.new, StringIO.new)
     end
 
     test &quot;notifies listener that it has done work&quot; do
@@ -61,7 +61,23 @@ module DeepTest
       listener = stub_everything
       agent = Agent.new(0, options, listener)
       listener.expects(:finished_work).with(agent, work_unit, TestResult.new(:result))
-      agent.execute
+      agent.execute(StringIO.new, StringIO.new)
+    end
+
+    test &quot;connect indicates it has connected&quot; do
+      options = Options.new({})
+      central_command = TestCentralCommand.start(options)
+      agent = Agent.new(0, options, stub_everything)
+      agent.connect(io = StringIO.new)
+      assert_equal &quot;Connected\n&quot;, io.string
+    end
+
+    test &quot;connect closes stream even if there is an error&quot; do
+      options = Options.new({})
+      agent = Agent.new(0, options, stub_everything)
+      io = StringIO.new
+      assert_raises(Errno::EADDRNOTAVAIL) { agent.connect io }
+      assert_equal true, io.closed?
     end
 
     class ResultWorkUnit
@@ -103,7 +119,7 @@ module DeepTest
       central_command.write_work work_unit
       central_command.done_with_work
 
-      Agent.new(0, options, stub_everything).execute
+      Agent.new(0, options, stub_everything).execute(StringIO.new, StringIO.new)
       
       assert_equal Agent::Error.new(work_unit, exception), central_command.take_result
     end
@@ -123,7 +139,7 @@ module DeepTest
       options = Options.new({})
       central_command = TestCentralCommand.start(options)
 
-      t = Thread.new { Agent.new(0, options, stub_everything).execute }
+      t = Thread.new { Agent.new(0, options, stub_everything).execute(StringIO.new, StringIO.new) }
       Thread.pass
       central_command.write_work stub(:run =&gt; TestResult.new(:result))
       central_command.done_with_work
@@ -135,7 +151,7 @@ module DeepTest
       options = Options.new({})
       central_command = TestCentralCommand.start(options)
       begin
-        t = Thread.new { Agent.new(0, options, stub_everything).execute }
+        t = Thread.new { Agent.new(0, options, stub_everything).execute(StringIO.new, StringIO.new) }
         sleep 0.1
       ensure
         central_command.stop</diff>
      <filename>test/deep_test/agent_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>75d66841cc0143101a94484d843debda5a74c05b</id>
    </parent>
  </parents>
  <author>
    <name>qxjit (David Vollbracht)</name>
    <email>david.vollbracht@gmail.com</email>
  </author>
  <url>http://github.com/qxjit/deep-test/commit/af3efed593da67df338e75df26b8fe3404c06f40</url>
  <id>af3efed593da67df338e75df26b8fe3404c06f40</id>
  <committed-date>2009-07-31T15:07:17-07:00</committed-date>
  <authored-date>2009-07-31T15:07:17-07:00</authored-date>
  <message>upgrade to latest Telegraph; wait for agents to connect to CentralCommand before moving on to read results</message>
  <tree>80a8dd8455b013d52bcd7954a41ae8bdd41d3f06</tree>
  <committer>
    <name>qxjit (David Vollbracht)</name>
    <email>david.vollbracht@gmail.com</email>
  </committer>
</commit>
