|
2 | 2 | require 'puppet_forge_server'
|
3 | 3 |
|
4 | 4 | describe PuppetForgeServer::Http::HttpClient do
|
| 5 | + # Finding a free port to host mock server |
5 | 6 | let(:port) do
|
6 | 7 | require 'socket'
|
7 | 8 | server = TCPServer.new('127.0.0.1', 0)
|
|
11 | 12 | end
|
12 | 13 | let(:uri) { "http://localhost:#{port}/" }
|
13 | 14 | before(:each) do
|
| 15 | + # Staring a mock server on free port to test network fetching performence |
14 | 16 | @server = TCPServer.new('localhost', port)
|
15 | 17 | @thr = Thread.new do
|
16 | 18 | loop do
|
|
21 | 23 | "Content-Length: #{response.bytesize}\r\n" +
|
22 | 24 | "Connection: close\r\n"
|
23 | 25 | socket.print "\r\n"
|
| 26 | + # To simulate network lag |
24 | 27 | sleep 0.01
|
25 | 28 | socket.print response
|
26 | 29 | socket.close
|
|
33 | 36 | end
|
34 | 37 | let(:instance) { described_class.new(cache) }
|
35 | 38 | 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 |
36 | 46 | subject do
|
37 |
| - 99.times { instance.download(uri) } |
38 |
| - instance.download(uri) |
| 47 | + load.call |
39 | 48 | end
|
40 | 49 | context 'with 1sec LRU cache' do
|
41 | 50 | let(:cache) do
|
42 | 51 | require 'lrucache'
|
43 | 52 | LRUCache.new(:ttl => 1)
|
44 | 53 | end
|
| 54 | + it { expect { load.call }.to run_for < 0.01 } |
45 | 55 | it { expect(subject).not_to be_nil }
|
46 | 56 | it { expect(subject).not_to be_closed }
|
47 | 57 | end
|
48 | 58 | context 'with default cache' do
|
49 | 59 | let(:cache) { nil }
|
| 60 | + it { expect { load.call }.to run_for < 0.01 } |
50 | 61 | it { expect(subject).not_to be_nil }
|
51 | 62 | it { expect(subject).not_to be_closed }
|
52 | 63 | end
|
53 | 64 | end
|
54 | 65 | 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 |
55 | 73 | subject do
|
56 |
| - 99.times { instance.get(uri) } |
57 |
| - instance.get(uri) |
| 74 | + load.call |
58 | 75 | end
|
59 | 76 | context 'with 1sec LRU cache' do
|
60 | 77 | let(:cache) do
|
61 | 78 | require 'lrucache'
|
62 | 79 | LRUCache.new(:ttl => 1)
|
63 | 80 | end
|
64 | 81 | it { expect(subject).to eq('Hello!') }
|
| 82 | + it { expect { load.call }.to run_for < 0.01 } |
65 | 83 | end
|
66 | 84 | context 'with default cache' do
|
67 | 85 | let(:cache) { nil }
|
68 | 86 | it { expect(subject).to eq('Hello!') }
|
| 87 | + it { expect { load.call }.to run_for < 0.01 } |
69 | 88 | end
|
70 | 89 | end
|
71 | 90 | end
|
0 commit comments