Skip to content

Commit cc0283b

Browse files
Cleaned up tests and made them more robust
1 parent 1b228ec commit cc0283b

File tree

5 files changed

+95
-37
lines changed

5 files changed

+95
-37
lines changed

spec/ruby/nginx_add_spec.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,53 @@
77
Ruby::Nginx.add!(
88
domain: "example.test",
99
port: Puma::Configuration::DEFAULTS[:tcp_port],
10-
root_path: RSpec.configuration.dummy_dir,
10+
root_path: RSpec.configuration.dummy_path,
1111
ssl: true,
1212
log: true
1313
)
1414
end
1515

1616
it "adds the hosts mapping" do
17-
hosts = File.read("/etc/hosts")
18-
expect(hosts).to include("example.test")
17+
RetryExpectation.new(limit: 30, delay: 1).attempt do
18+
hosts = File.read("/etc/hosts")
19+
expect(hosts).to include("example.test")
20+
end
1921
end
2022

2123
it "creates the NGINX configuration" do
22-
path = File.expand_path("~/.ruby-nginx/servers/ruby_nginx_example_test.conf")
23-
expect(File.exist?(path)).to be_truthy
24+
RetryExpectation.new(limit: 30, delay: 1).attempt do
25+
path = File.expand_path("~/.ruby-nginx/servers/ruby_nginx_example_test.conf")
26+
expect(File.exist?(path)).to be_truthy
27+
end
2428
end
2529

2630
it "creates the SSL certificate" do
27-
path = File.expand_path("~/.ruby-nginx/certs/_example.test.pem")
28-
expect(File.exist?(path)).to be_truthy
31+
RetryExpectation.new(limit: 30, delay: 1).attempt do
32+
path = File.expand_path("~/.ruby-nginx/certs/_example.test.pem")
33+
expect(File.exist?(path)).to be_truthy
2934

30-
path = File.expand_path("~/.ruby-nginx/certs/_example.test-key.pem")
31-
expect(File.exist?(path)).to be_truthy
35+
path = File.expand_path("~/.ruby-nginx/certs/_example.test-key.pem")
36+
expect(File.exist?(path)).to be_truthy
37+
end
3238
end
3339

3440
it "creates the log files" do
35-
path = File.expand_path("~/.ruby-nginx/logs/example.test.access.log")
36-
expect(File.exist?(path)).to be_truthy
41+
RetryExpectation.new(limit: 30, delay: 1).attempt do
42+
path = File.expand_path("~/.ruby-nginx/logs/example.test.access.log")
43+
expect(File.exist?(path)).to be_truthy
3744

38-
path = File.expand_path("~/.ruby-nginx/logs/example.test.error.log")
39-
expect(File.exist?(path)).to be_truthy
45+
path = File.expand_path("~/.ruby-nginx/logs/example.test.error.log")
46+
expect(File.exist?(path)).to be_truthy
47+
end
4048
end
4149

4250
it "successfully builds up a NGINX site" do
43-
html = `curl -s http://example.test`
44-
expect(html).to include("Hello, from Ruby NGINX!")
51+
RetryExpectation.new(limit: 30, delay: 1).attempt do
52+
html = `curl -s http://example.test`
53+
expect(html).to include("Hello, from Ruby NGINX!")
4554

46-
html = `curl -s https://example.test`
47-
expect(html).to include("Hello, from Ruby NGINX!")
55+
html = `curl -s https://example.test`
56+
expect(html).to include("Hello, from Ruby NGINX!")
57+
end
4858
end
4959
end

spec/ruby/nginx_remove_spec.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
end
77

88
it "removes the hosts mapping" do
9-
hosts = File.read("/etc/hosts")
10-
expect(hosts).not_to include("example.test")
9+
RetryExpectation.new(limit: 30, delay: 1).attempt do
10+
hosts = File.read("/etc/hosts")
11+
expect(hosts).not_to include("example.test")
12+
end
1113
end
1214

1315
it "deletes the NGINX configuration" do
14-
path = File.expand_path("~/.ruby-nginx/servers/ruby_nginx_example_test.conf")
15-
expect(File.exist?(path)).to be_falsey
16+
RetryExpectation.new(limit: 30, delay: 1).attempt do
17+
path = File.expand_path("~/.ruby-nginx/servers/ruby_nginx_example_test.conf")
18+
expect(File.exist?(path)).to be_falsey
19+
end
1620
end
1721

1822
it "successfully tears down a NGINX site" do
19-
sleep(1) # Sometimes the system takes a second to lose the DNS host mapping.
20-
html = `curl -s http://example.test`
21-
expect(html).to eq("")
23+
RetryExpectation.new(limit: 30, delay: 1).attempt do
24+
html = `curl -s http://example.test`
25+
expect(html).to eq("")
2226

23-
html = `curl -s https://example.test`
24-
expect(html).to eq("")
27+
html = `curl -s https://example.test`
28+
expect(html).to eq("")
29+
end
2530
end
2631
end

spec/spec_helper.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# frozen_string_literal: true
22

3-
require "puma"
4-
require "puma/cli"
53
require "ruby/nginx"
64

7-
ENV["SKIP_PROMPT"] = "true"
5+
Dir[File.expand_path("support/**/*.rb", __dir__)].each { |file| require file }
86

97
RSpec.configure do |config|
108
# Enable flags like --only-failures and --next-failure
@@ -17,18 +15,18 @@
1715
c.syntax = :expect
1816
end
1917

20-
config.add_setting :dummy_dir
21-
config.dummy_dir = File.expand_path("./dummy", __dir__)
18+
config.add_setting :dummy_path
19+
config.dummy_path = File.expand_path("./dummy", __dir__)
2220

2321
config.before(:suite) do
24-
print "Starting Dummy Server"
25-
$dummy_server = Thread.new do # standard:disable Style/GlobalVars
26-
Puma::CLI.new(["-s", "--dir=#{RSpec.configuration.dummy_dir}"]).run
27-
end
22+
puts "Starting Dummy Server"
23+
DummyServer.instance.start
24+
rescue
25+
DummyServer.instance.stop
2826
end
2927

3028
config.after(:suite) do
31-
print "Stopping Dummy Server"
32-
$dummy_server.exit if $dummy_server.alive? # standard:disable Style/GlobalVars
29+
puts "Stopping Dummy Server"
30+
DummyServer.instance.stop
3331
end
3432
end

spec/support/dummy_server.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "puma"
2+
require "puma/cli"
3+
require "singleton"
4+
5+
ENV["SKIP_PROMPT"] = "true"
6+
DUMMY_PATH = File.expand_path("../dummy", __dir__)
7+
8+
class DummyServer
9+
include Singleton
10+
11+
def initialize
12+
@semaphore = Mutex.new
13+
@server = nil
14+
end
15+
16+
def start
17+
@semaphore.synchronize do
18+
@server = Thread.new do # standard:disable Style/GlobalVars
19+
Puma::CLI.new(["-s", "--dir=#{DUMMY_PATH}"]).run
20+
end
21+
end
22+
end
23+
24+
def stop
25+
@semaphore.synchronize do
26+
@server.exit if @server.alive?
27+
end
28+
end
29+
end

spec/support/retry_expectation.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class RetryExpectation
2+
def initialize(limit:, delay:)
3+
@limit = limit
4+
@delay = delay
5+
@count = 0
6+
end
7+
8+
def attempt
9+
yield
10+
rescue RSpec::Expectations::ExpectationNotMetError
11+
raise if @count >= @limit
12+
sleep(@delay) if @delay
13+
@count += 1
14+
retry
15+
end
16+
end

0 commit comments

Comments
 (0)