sanford-protocol 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'rake'
6
- gem 'pry'
6
+ gem 'pry', "~> 0.9.0"
7
7
 
8
8
  gem 'bson_ext', '~>1.7'
@@ -0,0 +1,46 @@
1
+ module Sanford; end
2
+ module Sanford::Protocol
3
+
4
+ class FakeConnection
5
+
6
+ attr_accessor :read_data, :peek_data, :write_data
7
+ attr_accessor :closed, :closed_write
8
+ attr_reader :read_timeout, :peek_timeout
9
+
10
+ def initialize(read_data = nil)
11
+ @read_data = read_data || ""
12
+ @peek_data = read_data ? read_data[1] : ""
13
+ @write_data = nil
14
+
15
+ @read_timeout = nil
16
+ @peek_timeout = nil
17
+
18
+ @closed = false
19
+ @closed_write = false
20
+ end
21
+
22
+ def read(timeout = nil)
23
+ @read_timeout = timeout
24
+ @read_data
25
+ end
26
+
27
+ def write(data)
28
+ @write_data = data
29
+ end
30
+
31
+ def peek(timeout = nil)
32
+ @peek_timeout = timeout
33
+ @peek_data
34
+ end
35
+
36
+ def close
37
+ @closed = true
38
+ end
39
+
40
+ def close_write
41
+ @closed_write = true
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -35,6 +35,14 @@ module Sanford::Protocol
35
35
  " @params=#{params.inspect}>"
36
36
  end
37
37
 
38
+ def ==(other)
39
+ if other.kind_of?(self.class)
40
+ self.to_hash == other.to_hash
41
+ else
42
+ super
43
+ end
44
+ end
45
+
38
46
  protected
39
47
 
40
48
  def validate!(name, params)
@@ -31,6 +31,14 @@ module Sanford::Protocol
31
31
  "#<#{self.class}:#{reference} @status=#{status} @data=#{data.inspect}>"
32
32
  end
33
33
 
34
+ def ==(other)
35
+ if other.kind_of?(self.class)
36
+ self.to_hash == other.to_hash
37
+ else
38
+ super
39
+ end
40
+ end
41
+
34
42
  private
35
43
 
36
44
  def build_status(status)
@@ -1,5 +1,5 @@
1
1
  module Sanford
2
2
  module Protocol
3
- GEM_VERSION = "0.8.0"
3
+ GEM_VERSION = "0.9.0"
4
4
  end
5
5
  end
@@ -19,6 +19,5 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency("bson", ["~> 1.7", "< 1.10.0"])
21
21
 
22
- gem.add_development_dependency("assert", ["~> 2.10"])
23
- gem.add_development_dependency("assert-mocha", ["~> 1.1"])
22
+ gem.add_development_dependency("assert", ["~> 2.11"])
24
23
  end
@@ -12,7 +12,7 @@ ENV['SANFORD_PROTOCOL_DEBUG'] = 'yes'
12
12
  require 'sanford-protocol/fake_socket'
13
13
  FakeSocket = Sanford::Protocol::FakeSocket
14
14
 
15
- require 'assert-mocha' if defined?(Assert)
15
+ require 'test/support/factory'
16
16
 
17
17
  class Assert::Context
18
18
 
@@ -0,0 +1,6 @@
1
+ require 'assert/factory'
2
+
3
+ module Factory
4
+ extend Assert::Factory
5
+
6
+ end
@@ -0,0 +1,80 @@
1
+ require 'assert'
2
+ require 'sanford-protocol/fake_connection'
3
+
4
+ class Sanford::Protocol::FakeConnection
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "Sanford::Protocol::FakeConnection"
8
+ setup do
9
+ @read_data = Factory.binary
10
+ @fake_connection = Sanford::Protocol::FakeConnection.new(@read_data)
11
+ end
12
+ subject{ @fake_connection }
13
+
14
+ should have_accessors :read_data, :peek_data, :write_data
15
+ should have_accessors :closed, :closed_write
16
+ should have_readers :read_timeout, :peek_timeout
17
+ should have_imeths :read, :write, :peek, :close, :close_write
18
+
19
+ should "know its read data and peek data" do
20
+ assert_equal @read_data, subject.read_data
21
+ assert_equal @read_data[1], subject.peek_data
22
+ end
23
+
24
+ should "default its attributes" do
25
+ fake_connection = Sanford::Protocol::FakeConnection.new
26
+ assert_equal "", fake_connection.read_data
27
+ assert_equal "", fake_connection.peek_data
28
+ assert_nil fake_connection.write_data
29
+ assert_false fake_connection.closed
30
+ assert_false fake_connection.closed_write
31
+ assert_nil fake_connection.read_timeout
32
+ assert_nil fake_connection.peek_timeout
33
+ end
34
+
35
+ should "allow reading the read data using `read`" do
36
+ assert_nil subject.read_timeout
37
+ result = subject.read
38
+ assert_equal subject.read_data, result
39
+ assert_nil subject.read_timeout
40
+
41
+ timeout = Factory.integer
42
+ assert_nil subject.read_timeout
43
+ result = subject.read(timeout)
44
+ assert_equal timeout, subject.read_timeout
45
+ end
46
+
47
+ should "allow writing to the write data using `write`" do
48
+ data = Factory.boolean
49
+ assert_nil subject.write_data
50
+ subject.write(data)
51
+ assert_equal data, subject.write_data
52
+ end
53
+
54
+ should "allow reading the peek data using `peek`" do
55
+ assert_nil subject.peek_timeout
56
+ result = subject.peek
57
+ assert_equal subject.peek_data, result
58
+ assert_nil subject.peek_timeout
59
+
60
+ timeout = Factory.integer
61
+ assert_nil subject.peek_timeout
62
+ result = subject.peek(timeout)
63
+ assert_equal timeout, subject.peek_timeout
64
+ end
65
+
66
+ should "close itself using `close`" do
67
+ assert_false subject.closed
68
+ subject.close
69
+ assert_true subject.closed
70
+ end
71
+
72
+ should "close its write using `close_write`" do
73
+ assert_false subject.closed_write
74
+ subject.close_write
75
+ assert_true subject.closed_write
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -30,93 +30,87 @@ class Sanford::Protocol::MsgData
30
30
  class BadSizeTests < BadMessageTests
31
31
  desc "that errors when reading the version part"
32
32
  setup do
33
- @socket.stubs(:read). # when reading the version, fail
34
- with(Sanford::Protocol.msg_version.bytesize).
35
- raises("simulated socket read error!")
36
- end
37
- teardown do
38
- @socket.unstub(:read)
33
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_version.bytesize) do
34
+ raises "simulated socket read error!"
35
+ end
39
36
  end
40
37
 
41
38
  should "raise a BadMessageError with a relevant message" do
42
39
  assert_bad_message "Error reading message protocol version"
43
40
  end
41
+
44
42
  end
45
43
 
46
44
  class BadVersionTests < BadMessageTests
47
45
  desc "that errors when reading the size part"
48
46
  setup do
49
- @socket.stubs(:read). # when reading the version, succeed
50
- with(Sanford::Protocol.msg_version.bytesize).
51
- returns(Sanford::Protocol.msg_version)
52
- @socket.stubs(:read). # when reading the size, fail
53
- with(Sanford::Protocol.msg_size.bytes).
54
- raises("simulated socket read error!")
55
- end
56
- teardown do
57
- @socket.unstub(:read)
47
+ # when reading the version, succeed
48
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_version.bytesize) do
49
+ Sanford::Protocol.msg_version
50
+ end
51
+ # when reading the size, fail
52
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_size.bytes) do
53
+ raise "simulated socket read error!"
54
+ end
58
55
  end
59
56
 
60
57
  should "raise a BadMessageError with a relevant message" do
61
58
  assert_bad_message "Error reading message body size."
62
59
  end
60
+
63
61
  end
64
62
 
65
63
  class BadBodyTests < BadMessageTests
66
64
  desc "that errors when reading the body part"
67
65
  setup do
68
- @socket.stubs(:read). # when reading the version, succeed
69
- with(Sanford::Protocol.msg_version.bytesize).
70
- returns(Sanford::Protocol.msg_version)
71
- @socket.stubs(:read). # when reading the size, succeed
72
- with(Sanford::Protocol.msg_size.bytes).
73
- returns(Sanford::Protocol.msg_size.encode(50))
74
- @socket.stubs(:read). # when reading the body, fail
75
- with(50).
76
- raises("simulated socket read error!")
77
- end
78
- teardown do
79
- @socket.unstub(:read)
66
+ # when reading the version, succeed
67
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_version.bytesize) do
68
+ Sanford::Protocol.msg_version
69
+ end
70
+ # when reading the size, succeed
71
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_size.bytes) do
72
+ Sanford::Protocol.msg_size.encode(50)
73
+ end
74
+ # when reading the body, fail
75
+ Assert.stub(@socket, :read).with(50){ raise "simulated socket read error!" }
80
76
  end
81
77
 
82
78
  should "raise a BadMessageError with a relevant message" do
83
79
  assert_bad_message "Error reading message body."
84
80
  end
81
+
85
82
  end
86
83
 
87
84
  class NilSizeTests < BadMessageTests
88
85
  desc 'that reads a nil size'
89
86
  setup do
90
- @socket.stubs(:read). # when reading the version, succeed
91
- with(Sanford::Protocol.msg_version.bytesize).
92
- returns(Sanford::Protocol.msg_version)
93
- @socket.stubs(:read). # when reading the size, return nil
94
- with(Sanford::Protocol.msg_size.bytes).
95
- returns(nil)
96
- end
97
- teardown do
98
- @socket.unstub(:read)
87
+ # when reading the version, succeed
88
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_version.bytesize) do
89
+ Sanford::Protocol.msg_version
90
+ end
91
+ # when reading the size, return nil
92
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_size.bytes){ nil }
99
93
  end
100
94
 
101
95
  should "raise a BadMessageError with a relevant message" do
102
96
  assert_bad_message "Empty message size"
103
97
  end
98
+
104
99
  end
105
100
 
106
101
  class VersionMismatchTests < BadMessageTests
107
102
  desc "with a mismatched protocol version"
108
103
  setup do
109
- @socket.stubs(:read). # when reading the version, encode wrong number
110
- with(Sanford::Protocol.msg_version.bytesize).
111
- returns(Sanford::Protocol::PackedHeader.new(1, 'C').encode(0))
112
- end
113
- teardown do
114
- @socket.unstub(:read)
104
+ # when reading the version, encode wrong number
105
+ Assert.stub(@socket, :read).with(Sanford::Protocol.msg_version.bytesize) do
106
+ Sanford::Protocol::PackedHeader.new(1, 'C').encode(0)
107
+ end
115
108
  end
116
109
 
117
110
  should "raise a BadMessageError with a relevant message" do
118
111
  assert_bad_message "Protocol version mismatch"
119
112
  end
113
+
120
114
  end
121
115
 
122
116
  end
@@ -53,6 +53,17 @@ class Sanford::Protocol::Request
53
53
  assert_equal expected, request.to_hash
54
54
  end
55
55
 
56
+ should "be comparable" do
57
+ match_request = Sanford::Protocol::Request.new(
58
+ subject.name.dup,
59
+ subject.params.dup
60
+ )
61
+ assert_equal match_request, subject
62
+
63
+ not_match_request = Sanford::Protocol::Request.new('other', {})
64
+ assert_not_equal not_match_request, subject
65
+ end
66
+
56
67
  end
57
68
 
58
69
  class ValidTests < UnitTests
@@ -45,6 +45,17 @@ class Sanford::Protocol::Response
45
45
  assert_equal expected, subject.to_hash
46
46
  end
47
47
 
48
+ should "be comparable" do
49
+ match_response = Sanford::Protocol::Response.new(
50
+ subject.status.dup,
51
+ subject.data.dup
52
+ )
53
+ assert_equal match_response, subject
54
+
55
+ not_match_response = Sanford::Protocol::Response.new(123, {})
56
+ assert_not_equal not_match_response, subject
57
+ end
58
+
48
59
  end
49
60
 
50
61
  # Somewhat of a system test, want to make sure if Response is passed some
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: 63
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 8
8
+ - 9
9
9
  - 0
10
- version: 0.8.0
10
+ version: 0.9.0
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: 2014-06-09 00:00:00 Z
19
+ date: 2014-06-27 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -37,9 +37,9 @@ dependencies:
37
37
  - 10
38
38
  - 0
39
39
  version: 1.10.0
40
+ version_requirements: *id001
40
41
  type: :runtime
41
42
  name: bson
42
- version_requirements: *id001
43
43
  prerelease: false
44
44
  - !ruby/object:Gem::Dependency
45
45
  requirement: &id002 !ruby/object:Gem::Requirement
@@ -47,29 +47,14 @@ dependencies:
47
47
  requirements:
48
48
  - - ~>
49
49
  - !ruby/object:Gem::Version
50
- hash: 23
50
+ hash: 21
51
51
  segments:
52
52
  - 2
53
- - 10
54
- version: "2.10"
55
- type: :development
56
- name: assert
53
+ - 11
54
+ version: "2.11"
57
55
  version_requirements: *id002
58
- prerelease: false
59
- - !ruby/object:Gem::Dependency
60
- requirement: &id003 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ~>
64
- - !ruby/object:Gem::Version
65
- hash: 13
66
- segments:
67
- - 1
68
- - 1
69
- version: "1.1"
70
56
  type: :development
71
- name: assert-mocha
72
- version_requirements: *id003
57
+ name: assert
73
58
  prerelease: false
74
59
  description: Ruby implementation of the Sanford TCP communication protocol.
75
60
  email:
@@ -89,6 +74,7 @@ files:
89
74
  - Rakefile
90
75
  - lib/sanford-protocol.rb
91
76
  - lib/sanford-protocol/connection.rb
77
+ - lib/sanford-protocol/fake_connection.rb
92
78
  - lib/sanford-protocol/fake_socket.rb
93
79
  - lib/sanford-protocol/msg_data.rb
94
80
  - lib/sanford-protocol/request.rb
@@ -98,7 +84,9 @@ files:
98
84
  - lib/sanford-protocol/version.rb
99
85
  - sanford-protocol.gemspec
100
86
  - test/helper.rb
87
+ - test/support/factory.rb
101
88
  - test/unit/connection_tests.rb
89
+ - test/unit/fake_connection_tests.rb
102
90
  - test/unit/fake_socket_tests.rb
103
91
  - test/unit/msg_data_tests.rb
104
92
  - test/unit/request_tests.rb
@@ -135,13 +123,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
123
  requirements: []
136
124
 
137
125
  rubyforge_project:
138
- rubygems_version: 1.8.25
126
+ rubygems_version: 1.8.29
139
127
  signing_key:
140
128
  specification_version: 3
141
129
  summary: Ruby implementation of the Sanford TCP communication protocol.
142
130
  test_files:
143
131
  - test/helper.rb
132
+ - test/support/factory.rb
144
133
  - test/unit/connection_tests.rb
134
+ - test/unit/fake_connection_tests.rb
145
135
  - test/unit/fake_socket_tests.rb
146
136
  - test/unit/msg_data_tests.rb
147
137
  - test/unit/request_tests.rb