spec/helpers.rb in cross-stub-0.1.1 vs spec/helpers.rb in cross-stub-0.1.2

- old
+ new

@@ -1,9 +1,11 @@ require 'rubygems' require 'eventmachine' -$cache_file = '/tmp/crossstub.cache' +$cache_file = File.join(File.dirname(__FILE__), '..', 'tmp', 'stubbing.cache') +$log_file = File.join(File.dirname(__FILE__), '..', 'tmp', 'echoserver.log') +$sleep_time = 1.5 # may need to increase this depending on ur machine's prowess $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'cross-stub' class AnyClass @@ -26,13 +28,13 @@ def get(klass_and_method) address, port = EchoServer::ADDRESS, EchoServer::PORT EventMachine::run do (EventMachine::connect(address, port, EM)). - execute(klass_and_method) {|data| self.result = data } + execute(klass_and_method) {|data| self.result = Marshal.load(data) } end - (self.result == '<NIL>') ? nil : self.result + self.result end end private @@ -61,11 +63,11 @@ end def start(other_process=false) unless other_process @process = IO.popen("ruby #{__FILE__}") - sleep 1 + sleep $sleep_time else EventMachine::run { EventMachine::start_server(ADDRESS, PORT, EM) } end end @@ -77,20 +79,36 @@ private module EM def receive_data(klass_and_method) + log "(1) EchoServer::EM#receive_data ... receives: #{klass_and_method}" CrossStub.refresh(:file => $cache_file) + log "(2) EchoServer::EM#receive_data ... completes stubs refresh" klass, method, *args = klass_and_method.split('.') - value = - if args.empty? - Object.const_get(klass).send(method) rescue $! - else - Object.const_get(klass).send(method, *args) rescue $! - end - send_data(value.nil? ? '<NIL>' : value) + log "(3) EchoServer::EM#receive_data ... parses arguments to:", + " * klass ... #{klass}", + " * method ... #{method}", + " * args ... #{args.inspect}" + value = args.empty? ? Object.const_get(klass).send(method) : + Object.const_get(klass).send(method, *args) rescue $!.message + log "(4) EchoServer::EM#receive_data ... returns: #{value.inspect}" + send_data(Marshal.dump(value)) + log "(5) EchoServer::EM#receive_data ... end" end + + def log(*msg) + $logger << [msg, ""].flatten.join("\n") + end end end -EchoServer.start(true) if ($0 == __FILE__) +if $0 == __FILE__ + begin + require 'logger' + $logger = Logger.new($log_file) + EchoServer.start(true) + ensure + $logger.close + end +end