Skip to content

Commit 42f120d

Browse files
author
Suszyński Krzysztof
committed
Adding stopwatch tests to ensure speed in tests
1 parent f98de9f commit 42f120d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

puppet-forge-server.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Gem::Specification.new do |spec|
5151
spec.add_development_dependency 'rspec', '~> 3.1'
5252
spec.add_development_dependency 'rspec-core', '~> 3.1'
5353
spec.add_development_dependency 'simplecov', '~> 0.11.0'
54+
spec.add_development_dependency 'rspec-stopwatch', '~> 0.1.3'
5455

5556
spec.required_ruby_version = '>= 1.9.3'
5657
end

spec/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
require 'rack/test'
1818
require 'rspec'
19+
require 'rspec/stopwatch'
1920
require 'puppet_forge_server'
2021

2122
ENV['RACK_ENV'] = 'test'
@@ -44,7 +45,7 @@ module RSpecMixin
4445
rescue Gem::LoadError
4546
# do nothing
4647
end
47-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[*formatters]
48+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(*formatters)
4849
SimpleCov.start do
4950
add_filter "/spec/"
5051
add_filter "/.vendor/"

spec/unit/http/http_client_spec.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'puppet_forge_server'
33

44
describe PuppetForgeServer::Http::HttpClient do
5+
# Finding a free port to host mock server
56
let(:port) do
67
require 'socket'
78
server = TCPServer.new('127.0.0.1', 0)
@@ -11,6 +12,7 @@
1112
end
1213
let(:uri) { "http://localhost:#{port}/" }
1314
before(:each) do
15+
# Staring a mock server on free port to test network fetching performence
1416
@server = TCPServer.new('localhost', port)
1517
@thr = Thread.new do
1618
loop do
@@ -21,6 +23,7 @@
2123
"Content-Length: #{response.bytesize}\r\n" +
2224
"Connection: close\r\n"
2325
socket.print "\r\n"
26+
# To simulate network lag
2427
sleep 0.01
2528
socket.print response
2629
socket.close
@@ -33,39 +36,55 @@
3336
end
3437
let(:instance) { described_class.new(cache) }
3538
describe '#download' do
39+
let(:load) do
40+
Proc.new do
41+
# To simulate multiple fetches
42+
99.times { instance.download(uri) }
43+
instance.download(uri)
44+
end
45+
end
3646
subject do
37-
99.times { instance.download(uri) }
38-
instance.download(uri)
47+
load.call
3948
end
4049
context 'with 1sec LRU cache' do
4150
let(:cache) do
4251
require 'lrucache'
4352
LRUCache.new(:ttl => 1)
4453
end
54+
it { expect { load.call }.to run_for < 0.01 }
4555
it { expect(subject).not_to be_nil }
4656
it { expect(subject).not_to be_closed }
4757
end
4858
context 'with default cache' do
4959
let(:cache) { nil }
60+
it { expect { load.call }.to run_for < 0.01 }
5061
it { expect(subject).not_to be_nil }
5162
it { expect(subject).not_to be_closed }
5263
end
5364
end
5465
describe '#get' do
66+
let(:load) do
67+
Proc.new do
68+
# To simulate multiple fetches
69+
99.times { instance.get(uri) }
70+
instance.get(uri)
71+
end
72+
end
5573
subject do
56-
99.times { instance.get(uri) }
57-
instance.get(uri)
74+
load.call
5875
end
5976
context 'with 1sec LRU cache' do
6077
let(:cache) do
6178
require 'lrucache'
6279
LRUCache.new(:ttl => 1)
6380
end
6481
it { expect(subject).to eq('Hello!') }
82+
it { expect { load.call }.to run_for < 0.01 }
6583
end
6684
context 'with default cache' do
6785
let(:cache) { nil }
6886
it { expect(subject).to eq('Hello!') }
87+
it { expect { load.call }.to run_for < 0.01 }
6988
end
7089
end
7190
end

0 commit comments

Comments
 (0)