Initial, simple, unit-tested implementation of QMPClient.

This follows the README given earlier, at least in principle, but
doesn't implement the entirety of QMP, by any stretch of the
imagination.

Notable by their absence are error responses, argument validation
(for incoming and outgoing messsages of all types), and any visibility
into qmp_capabilities.

Also missing are integration tests.
This commit is contained in:
Nick Thomas
2011-11-13 18:36:51 +00:00
parent 7777e5cacb
commit 51175ddf56
13 changed files with 833 additions and 1 deletions

View File

@@ -0,0 +1,82 @@
require 'helper'
require 'qmp_client'
module TestQMPClient
class TestModuleMethods < BaseTestCase
include QMPClient
def test_require_loads_main_constants
assert(defined?(::QMPClient), "QMPClient not defined")
assert(defined?(::QMPClient::API), "QMPClient::API not defined")
assert(defined?(::QMPClient::Connectors), "Connectors not defined")
assert(defined?(::QMPClient::Connectors::Socket), "Socket connector not defined")
assert(defined?(::QMPClient::Connectors::WriteProxy), "WriteProxy not defined")
assert(defined?(::QMPClient::Messages), "Messages module not defined")
assert(defined?(::QMPClient::Messages::Query), "Query message not defined")
assert(defined?(::QMPClient::Messages::Command), "Command message not defined")
assert(defined?(::QMPClient::Messages::Event), "Event message not defined")
assert(defined?(::QMPClient::Messages::Reply), "Greeting message not defined")
end
def expects_socket_run(rio, wio=nil)
rq, wq = [Queue.new, Queue.new]
Connectors::Socket.any_instance.expects(:run).with(rio, wio).
once.yields(rq, wq)
API.expects(:run).with(rq, wq).once.yields("(mock-api)")
end
def test_connect_tcp
cargs = ["127.0.0.1", 4440, "127.0.0.1", 40000]
mock_sock = mock("(tcp-socket)")
mock_sock.stubs(:closed? => false, :close => true)
TCPSocket.expects(:connect).with(*cargs).once.returns(mock_sock)
expects_socket_run(mock_sock)
runs_api_block = false
QMPClient::connect_tcp(*cargs) do |api|
runs_api_block = true
end
assert(runs_api_block, "API not yielded by connect_tcp")
end
def test_connect_unix
mock_sock = mock("(unix-socket)")
mock_sock.stubs(:closed? => false, :close => true)
UNIXSocket.expects(:connect).with("/tmp/test.sock").once.returns(mock_sock)
expects_socket_run(mock_sock)
runs_api_block = false
QMPClient::connect_unix("/tmp/test.sock") do |api|
runs_api_block = true
end
assert(runs_api_block, "API not yielded by connect_unix")
end
def test_connect_socket
rs, ws = [mock("(read-socket)"), mock("(write-socket)")]
expects_socket_run(rs, ws)
runs_api_block = false
QMPClient::connect_socket(rs, ws) do |api|
runs_api_block = true
end
assert(runs_api_block, "API not yielded by connect_socket")
# If we pass in just one socket, it's used for both read and write
rw = mock("(rw-socket)")
runs_api_block = false
expects_socket_run(rw)
QMPClient::connect_socket(rw) do
runs_api_block = true
end
assert(runs_api_block, "API not yielded by connect_socket with one sock")
end
end
end