2011-06-10 23:14:39 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'em-bitcoin'
|
|
|
|
|
|
|
|
|
|
|
|
include ::EM::P
|
|
|
|
|
|
|
|
class MockActor
|
2011-06-21 21:18:23 +01:00
|
|
|
attr_accessor :connection, :network_name, :sub_version, :current_height,
|
|
|
|
:node_nonce
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<MockActor: #{name}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s ; inspect ; end
|
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
self.network_name = :main # defaults
|
|
|
|
self.current_height = 0
|
|
|
|
self.node_nonce = 0
|
|
|
|
|
|
|
|
attributes.each {|k,v| self.send("#{k}=", v) }
|
|
|
|
end
|
2011-06-10 23:14:39 +01:00
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
def current_time=(new_time)
|
|
|
|
@current_time = new_time
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_time
|
|
|
|
@current_time || Time.now
|
|
|
|
end
|
2011-06-10 23:14:39 +01:00
|
|
|
|
|
|
|
def log(level, msg)
|
2011-06-21 21:18:23 +01:00
|
|
|
STDERR.puts([name, level, msg].join(": ")) #if $DEBUG
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready!
|
2011-06-21 21:40:59 +01:00
|
|
|
log(:info, "Received ready! call from em-bitcoin")
|
2011-06-10 23:14:39 +01:00
|
|
|
@ready = true
|
2011-06-21 21:40:59 +01:00
|
|
|
@on_ready.call
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
|
|
|
@ready == true
|
|
|
|
end
|
|
|
|
|
2011-06-21 21:40:59 +01:00
|
|
|
# triggers for various events
|
|
|
|
def on_ready(&blk)
|
|
|
|
@on_ready = blk
|
|
|
|
end
|
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
protected
|
2011-06-10 23:14:39 +01:00
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
attr_accessor :name
|
2011-06-10 23:14:39 +01:00
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
end
|
|
|
|
shared_examples_for BitcoinPeer do
|
|
|
|
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
describe BitcoinClient do
|
|
|
|
include EM::Spec
|
|
|
|
|
|
|
|
def host
|
|
|
|
"127.0.0.1"
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
def port
|
|
|
|
@port || 0
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
def start_server(actor = @server_actor)
|
|
|
|
sig = EM::start_server(host, port, EM::P::BitcoinServer, actor)
|
|
|
|
|
|
|
|
@port = Socket::unpack_sockaddr_in(EM::get_sockname(sig))[0]
|
|
|
|
@server
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_client(actor = @client_actor)
|
|
|
|
@client = EM::connect(host, port, EM::P::BitcoinClient, actor)
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
before(:each) do
|
2011-06-21 21:18:23 +01:00
|
|
|
@client_actor = MockActor.new(:name => "client")
|
|
|
|
@server_actor = MockActor.new(:name => "server")
|
|
|
|
done
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
2011-06-21 21:18:23 +01:00
|
|
|
|
2011-06-10 23:14:39 +01:00
|
|
|
describe "connection setup" do
|
|
|
|
context "successful" do
|
2011-06-21 21:18:23 +01:00
|
|
|
it "should connect to a Bitcoin server" do
|
|
|
|
start_server
|
|
|
|
start_client
|
|
|
|
|
2011-06-21 21:40:59 +01:00
|
|
|
@client_actor.on_ready do
|
|
|
|
@client_actor.connection.should_not == nil
|
|
|
|
done
|
|
|
|
end
|
2011-06-10 23:14:39 +01:00
|
|
|
|
2011-06-21 21:18:23 +01:00
|
|
|
end
|
2011-06-10 23:14:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe BitcoinServer do
|
|
|
|
end
|
|
|
|
|