Skip to content
Browse files

Implement read on connection

  • Loading branch information...
1 parent b356627 commit 848e543a56a702f01edfb9d6cfcd21c95a18a390 @durran durran committed
Showing with 80 additions and 28 deletions.
  1. +11 −0 lib/mongo/pool/connection.rb
  2. +8 −0 lib/mongo/pool/socket/base.rb
  3. +0 −8 lib/mongo/protocol/message.rb
  4. +61 −20 spec/mongo/pool/connection_spec.rb
View
11 lib/mongo/pool/connection.rb
@@ -88,7 +88,18 @@ def initialize(host, port, timeout = nil, options = {})
@socket = nil
end
+ # Read a reply from the connection.
+ #
+ # @example Read a reply from the connection.
+ # connection.read
+ #
+ # @return [ Protocol::Reply ] The reply object.
+ #
+ # @since 3.0.0
def read
+ ensure_connected do |socket|
+ Protocol::Reply.deserialize(socket)
+ end
end
# Write messages to the connection in a single network call.
View
8 lib/mongo/pool/socket/base.rb
@@ -72,6 +72,14 @@ def read(length)
handle_socket_error { @socket.read(length) }
end
+ def gets(*args)
+ handle_socket_error { @socket.gets(*args) }
+ end
+
+ def readbyte
+ handle_socket_error { @socket.readbyte }
+ end
+
# Writes data to the socket instance.
#
# @example Write to the socket.
View
8 lib/mongo/protocol/message.rb
@@ -226,14 +226,6 @@ def self.deserialize_field(message, io, field)
field[:type].deserialize(io)
)
end
-
-
- private
-
- # @durran: temp for testing until read from connection is done.
- def self.reset_request_id
- @@request_id -= 1
- end
end
end
end
View
81 spec/mongo/pool/connection_spec.rb
@@ -126,6 +126,47 @@
end
end
+ describe '#read' do
+
+ let(:connection) do
+ described_class.new('127.0.0.1', 27017, 5)
+ end
+
+ let(:documents) do
+ [{ 'name' => 'testing' }]
+ end
+
+ let(:insert) do
+ Mongo::Protocol::Insert.new('mongo_test', 'users', documents)
+ end
+
+ let(:query) do
+ Mongo::Protocol::Query.new('mongo_test', 'users', {})
+ end
+
+ let(:delete) do
+ Mongo::Protocol::Delete.new('mongo_test', 'users', {})
+ end
+
+ before do
+ connection.write([ insert ])
+ connection.write([ query ])
+ end
+
+ # @todo: Can remove this once we have more implemented with global hooks.
+ after do
+ connection.write([ delete ])
+ end
+
+ let(:reply) do
+ connection.read
+ end
+
+ it 'returns the reply from the connection' do
+ expect(reply.documents.first['name']).to eq('testing')
+ end
+ end
+
describe '#write' do
let(:connection) do
@@ -136,28 +177,32 @@
[{ 'name' => 'testing' }]
end
- let(:message) do
+ let(:insert) do
Mongo::Protocol::Insert.new('mongo_test', 'users', documents)
end
- let(:socket) do
- connection.send(:socket)
+ let(:query) do
+ Mongo::Protocol::Query.new('mongo_test', 'users', {})
+ end
+
+ let(:delete) do
+ Mongo::Protocol::Delete.new('mongo_test', 'users', {})
end
context 'when providing a single message' do
- let!(:serialized) do
- message.serialize('')
+ before do
+ connection.write([ insert ])
+ connection.write([ query ])
end
- before do
- connection.connect!
- Mongo::Protocol::Message.send(:reset_request_id)
- expect(socket).to receive(:write).with(serialized)
+ # @todo: Can remove this once we have more implemented with global hooks.
+ after do
+ connection.write([ delete ])
end
it 'it writes the message to the socket' do
- connection.write([ message ])
+ expect(connection.read.documents.first['name']).to eq('testing')
end
end
@@ -171,21 +216,17 @@
Mongo::Protocol::Query.new('mongo_test', '$cmd', selector, :limit => -1)
end
- let(:buffer) { '' }
-
- let!(:serialized) do
- message.serialize(buffer)
- command.serialize(buffer)
+ before do
+ connection.write([ insert, command ])
end
- before do
- connection.connect!
- 2.times { Mongo::Protocol::Message.send(:reset_request_id) }
- expect(socket).to receive(:write).with(serialized)
+ # @todo: Can remove this once we have more implemented with global hooks.
+ after do
+ connection.write([ delete ])
end
it 'it writes the message to the socket' do
- connection.write([ message, command ])
+ expect(connection.read.documents.first['ok']).to eq(1.0)
end
end
end

0 comments on commit 848e543

Please sign in to comment.
Something went wrong with that request. Please try again.