Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mailbox can listen on a port.

  • Loading branch information...
commit b598cf7ff18af61799e98b0ab821feeb8fb88f8e 1 parent 0c86a5d
@adscott authored
Showing with 57 additions and 0 deletions.
  1. +4 −0 Rakefile
  2. +11 −0 lib/mailbox.rb
  3. +42 −0 spec/mailbox_spec.rb
View
4 Rakefile
@@ -0,0 +1,4 @@
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
View
11 lib/mailbox.rb
@@ -0,0 +1,11 @@
+class Mailbox
+ def initialize(port)
+ @port = port
+ end
+
+
+ def start
+ @service = TCPServer.new('localhost', @port )
+ end
+
+end
View
42 spec/mailbox_spec.rb
@@ -0,0 +1,42 @@
+require 'mailbox'
+
+describe Mailbox do
+
+ it 'should open a port' do
+ mailbox = Mailbox.new(2525)
+ mailbox.start
+
+ port(2525).should be_open
+ end
+
+ require 'socket'
+ require 'timeout'
+
+ class Port
+ def initialize(port)
+ @port = port
+ end
+
+ def open?
+ begin
+ Timeout::timeout(1) do
+ begin
+ s = TCPSocket.new('localhost', @port)
+ s.close
+ return true
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ return false
+ end
+ end
+ rescue Timeout::Error
+ end
+
+ return false
+ end
+ end
+
+ def port(port)
+ Port.new(port)
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.