Skip to content

Commit

Permalink
freeze time in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanne committed Jul 22, 2014
1 parent e7270cf commit 46ce627
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
12 changes: 7 additions & 5 deletions spec/bitcoin/protocol/addr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def on_addr(addr); (@addr ||= []) << addr; end
end

it 'initalize time, service and port' do
addr = Bitcoin::Protocol::Addr.new(nil)
t = Time.now.to_i; (t-10..t+10).include?(addr[:time]).should == true
addr[:service] .should == 1
addr[:port] .should == Bitcoin.network[:default_port]
addr[:ip] .should == "127.0.0.1"
Time.freeze do
addr = Bitcoin::Protocol::Addr.new(nil)
addr[:time].should == Time.now.to_i
addr[:service] .should == 1
addr[:port] .should == Bitcoin.network[:default_port]
addr[:ip] .should == "127.0.0.1"
end
end

it 'addr payload' do
Expand Down
19 changes: 19 additions & 0 deletions spec/bitcoin/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ def setup_db backend, db = nil, conf = {}
end
Bitcoin::Storage.send(backend, conf.merge(db: uri, log_level: :warn))
end


class Time
class << self
alias_method :real_new, :new
alias_method :new, :now
def now; @time || real_new; end
def freeze(time = nil)
begin
prev = @time
@time = time || now
yield
ensure
@time = prev
end
end
def frozen?; !@time.nil?; end
end
end
8 changes: 5 additions & 3 deletions spec/bitcoin/storage/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ def check_block blk, error
end

it "5. Block timestamp must not be more than two hours in the future" do
fake_time = (Time.now + 3 * 60 * 60).to_i
check_block(@block, [:max_timestamp, [fake_time, Time.now.to_i + 7200]]) {|b|
b.time = fake_time }
Time.freeze do
fake_time = (Time.now + 3 * 60 * 60).to_i
check_block(@block, [:max_timestamp, [fake_time, Time.now.to_i + 7200]]) {|b|
b.time = fake_time }
end
end

it "6. First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be" do
Expand Down

0 comments on commit 46ce627

Please sign in to comment.