sanford-protocol 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'sanford-protocol/msg_data'
2
+ require 'socket'
2
3
 
3
4
  module Sanford::Protocol
4
5
 
@@ -33,6 +34,11 @@ module Sanford::Protocol
33
34
  @socket.write(msg_version, size, body)
34
35
  end
35
36
 
37
+ def peek(timeout=nil)
38
+ wait_for_data(timeout) if timeout
39
+ @socket.peek
40
+ end
41
+
36
42
  def close
37
43
  @socket.close
38
44
  end
@@ -57,13 +63,17 @@ module Sanford::Protocol
57
63
  end
58
64
 
59
65
  def read(number_of_bytes)
60
- tcp_socket.recvfrom(number_of_bytes).first
66
+ tcp_socket.recv(number_of_bytes)
61
67
  end
62
68
 
63
69
  def write(*binary_strings)
64
70
  tcp_socket.send(binary_strings.join, 0)
65
71
  end
66
72
 
73
+ def peek(number_of_bytes = 1)
74
+ tcp_socket.recv(number_of_bytes, ::Socket::MSG_PEEK)
75
+ end
76
+
67
77
  def close
68
78
  tcp_socket.close rescue false
69
79
  end
@@ -39,8 +39,8 @@ module Sanford::Protocol::Test
39
39
 
40
40
  # Socket methods -- requied by Sanford::Protocol
41
41
 
42
- def recvfrom(number_of_bytes)
43
- [ @in.read(number_of_bytes.to_i) ]
42
+ def recv(number_of_bytes, flags = nil)
43
+ @in.read(number_of_bytes.to_i) || ""
44
44
  end
45
45
 
46
46
  def send(bytes, flag)
@@ -1,5 +1,5 @@
1
1
  module Sanford
2
2
  module Protocol
3
- GEM_VERSION = "0.5.1"
3
+ GEM_VERSION = "0.5.3"
4
4
  end
5
5
  end
@@ -12,7 +12,7 @@ class Sanford::Protocol::Connection
12
12
  end
13
13
  subject{ @connection }
14
14
 
15
- should have_instance_methods :read, :write, :close
15
+ should have_instance_methods :read, :write, :close, :peek
16
16
 
17
17
  should "read messages off the socket with #read" do
18
18
  assert_equal @data, subject.read
@@ -27,19 +27,45 @@ class Sanford::Protocol::Connection
27
27
  subject.close
28
28
  assert @socket.closed?
29
29
  end
30
- end
31
30
 
32
- class TimeoutTests < BaseTests
33
- desc "when timing out on a read"
34
- setup do
35
- IO.stubs(:select).returns(nil) # mock IO.select behavior when it times out
31
+ should "look at the first byte of data on the socket with #peek" do
32
+ assert_equal @socket.in[0, 1], subject.peek
36
33
  end
37
- teardown do
38
- IO.unstub(:select)
34
+
35
+ end
36
+
37
+ class RealConnectionTests < BaseTests
38
+
39
+ def start_server(options, &block)
40
+ begin
41
+ # this `fork` is a separate process, so it runs parallel to the code
42
+ # after it's block
43
+ pid = fork do
44
+ tcp_server = TCPServer.open 'localhost', 12000
45
+ trap("TERM"){ tcp_server.close }
46
+ socket = tcp_server.accept # blocks here, waits for `block` to connect
47
+ options[:serve].call(socket) if options[:serve]
48
+ end
49
+ sleep 0.2 # give the server time to boot
50
+ yield
51
+ ensure
52
+ if pid
53
+ Process.kill("TERM", pid)
54
+ Process.wait(pid)
55
+ end
56
+ end
39
57
  end
40
58
 
59
+ end
60
+
61
+ class TimeoutTests < RealConnectionTests
62
+ desc "when timing out on a read"
63
+
41
64
  should "raise `TimeoutError` if given a timeout value" do
42
- assert_raises(Sanford::Protocol::TimeoutError) { subject.read(1) }
65
+ self.start_server(:serve => proc{ sleep 0.2 }) do
66
+ connection = Sanford::Protocol::Connection.new(TCPSocket.new('localhost', 12000))
67
+ assert_raises(Sanford::Protocol::TimeoutError) { connection.read(0.1) }
68
+ end
43
69
  end
44
70
 
45
71
  end
@@ -13,7 +13,7 @@ class Sanford::Protocol::Test::FakeSocket
13
13
 
14
14
  should have_cmeths :with_request, :with_msg_body, :with_encoded_msg_body
15
15
  should have_imeths :in, :out, :reset
16
- should have_imeths :recvfrom, :send
16
+ should have_imeths :recv, :send
17
17
 
18
18
  should "have no `in` or `out` data by default" do
19
19
  assert_empty subject.in
@@ -38,11 +38,10 @@ class Sanford::Protocol::Test::FakeSocket
38
38
  assert_equal @in_data, subject.in
39
39
  end
40
40
 
41
- should "pull `in` data using #recvfrom" do
42
- recvfrom_data = subject.recvfrom(@in_data.bytesize)
41
+ should "pull `in` data using #recv" do
42
+ recv_data = subject.recv(@in_data.bytesize)
43
43
 
44
- assert_kind_of ::Array, recvfrom_data
45
- assert_equal @in_data, recvfrom_data.first
44
+ assert_equal @in_data, recv_data
46
45
  end
47
46
 
48
47
  should "reset its `in` data using #reset" do
@@ -75,7 +75,7 @@ class Sanford::Protocol::MsgData
75
75
  raises("simulated socket read error!")
76
76
  end
77
77
  teardown do
78
- Sanford::Protocol.unstub(:read)
78
+ @socket.unstub(:read)
79
79
  end
80
80
 
81
81
  should "raise a BadMessageError with a relevant message" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanford-protocol
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 3
10
+ version: 0.5.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-01-17 00:00:00 Z
19
+ date: 2013-02-05 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false