Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

race condition in tests. fixes #1 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ tmp
*.o
*.a
mkmf.log

#swapfiles
*.swp
4 changes: 4 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--color
--format documentation
--backtrace
--order random
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gemspec

gem 'rake'

group :test do
group :test, :development do
gem 'pry-byebug'
gem 'awesome_print'
gem 'rspec'
gem 'pry'
end
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ client = BN::P2P::Client.connect(host)
client.send(BN::Protocol::Messages.version)
client.send(BN::Protocol::Messages.ping)
client.send(BN::Protocol::Messages.getaddr)
```
```
8 changes: 4 additions & 4 deletions lib/bitcoin_node/p2p/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Server

def initialize(port = 3333, probe = LoggingProbe.new('server'))
@server = TCPServer.new('localhost', port)
@probe = probe
@probe = probe
@peers = Peers.new
async.run
end
Expand Down Expand Up @@ -40,7 +40,7 @@ def handle_messages(socket)
@probe << { receiving: command }

if command == 'version'
payload = BN::Protocol::Version.new(addr_recv: ['127.0.0.1', port])
payload = BN::Protocol::Version.new(addr_recv: ['127.0.0.1', port])
message = BN::Protocol::Message.new(payload)
@probe << { sending: 'version' }
@peers.update(host, :version)
Expand Down Expand Up @@ -68,7 +68,7 @@ def handle_messages(socket)

require 'monitor'

class Peers
class Peers
include MonitorMixin

def initialize
Expand All @@ -89,7 +89,7 @@ def status(peer)
end

def to_s
@peers.to_s
@peers.to_s
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/P2p/server_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe 'Server' do

let(:addr) { '127.0.0.1' }
let(:port) { random_port }

it 'returns socket' do
server = BN::P2p::Server.new(port)
thread = Thread.new { server.accept }
socket = within_io_actor { Celluloid::IO::TCPSocket.open(addr, port) }
expect(socket).to be_a(Celluloid::IO::TCPSocket)
end

end
16 changes: 8 additions & 8 deletions spec/integration/version_handshake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe 'Version handshake' do

let(:port) { 3333 }
let(:addr) { '127.0.0.1' }
let(:port) { random_port }

it 'peers exchanges version properly' do
server = BN::P2p::Server.new

server = BN::P2p::Server.new(port)
client_probe = BN::P2p::StoreProbe.new
client = BN::P2p::Client.connect('localhost', port, probe: client_probe)
client.version = 60001
Expand All @@ -15,7 +15,7 @@
message = BN::Protocol::Message.new(payload)

expect(client.handshaked?).to eql false

client.send(message)

expect(client.handshaked?).to eql true
Expand All @@ -29,9 +29,9 @@
end

it 'server does not answer to verack if no version exchanged beforehand' do
server = BN::P2p::Server.new
server = BN::P2p::Server.new(port)
client = BN::P2p::Client.connect('localhost', port, read_timeout: 1)

expect(client.handshaked?).to eql false
expect {
client.send(BN::Protocol::Messages.verack)
Expand All @@ -44,12 +44,12 @@ class BadPeer
include Celluloid::IO

def initialize(port)
@server = TCPServer.new('localhost', port)
@server = TCPServer.new('localhost', port)
async.run
end

def run
loop { async.on_connection @server.accept }
loop { async.on_connection @server.accept }
end

def on_connection(socket)
Expand Down
21 changes: 21 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
require File.expand_path('../lib/bitcoin_node.rb', __dir__)

require 'celluloid/io'
require 'pry'
require 'awesome_print'

class WrapperActor
include Celluloid::IO
execute_block_on_receiver :wrap

def wrap
yield
end
end

def random_port
port = 12_000 + Random.rand(1024)
end

def within_io_actor(&block)
actor = WrapperActor.new
actor.wrap(&block)
ensure
actor.terminate if actor.alive? rescue nil
end