Commit 47e8f6de authored by Terence Lee's avatar Terence Lee

speed up test runs by only booting docker once in a run block per test case

parent ac036e0c
...@@ -59,6 +59,7 @@ RSpec.describe "Simple" do ...@@ -59,6 +59,7 @@ RSpec.describe "Simple" do
let(:name) { "routes" } let(:name) { "routes" }
it "should support custom routes" do it "should support custom routes" do
app.run do
response = app.get("/foo.html") response = app.get("/foo.html")
expect(response.code).to eq("200") expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello world") expect(response.body.chomp).to eq("hello world")
...@@ -72,6 +73,7 @@ RSpec.describe "Simple" do ...@@ -72,6 +73,7 @@ RSpec.describe "Simple" do
expect(response.body.chomp).to eq("hello from route") expect(response.body.chomp).to eq("hello from route")
end end
end end
end
describe "redirects" do describe "redirects" do
let(:name) { "redirects" } let(:name) { "redirects" }
...@@ -158,6 +160,7 @@ STATIC_JSON ...@@ -158,6 +160,7 @@ STATIC_JSON
let(:name) { "custom_headers" } let(:name) { "custom_headers" }
it "should return the respected headers only for the path specified" do it "should return the respected headers only for the path specified" do
app.run do
response = app.get("/") response = app.get("/")
expect(response["cache-control"]).to eq("no-cache") expect(response["cache-control"]).to eq("no-cache")
expect(response.code).to eq("200") expect(response.code).to eq("200")
...@@ -168,11 +171,13 @@ STATIC_JSON ...@@ -168,11 +171,13 @@ STATIC_JSON
expect(response.code).to eq("200") expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo") expect(response.body.chomp).to eq("foo")
end end
end
describe "wildcard paths" do describe "wildcard paths" do
let(:name) { "custom_headers_wildcard" } let(:name) { "custom_headers_wildcard" }
it "should add the headers" do it "should add the headers" do
app.run do
response = app.get("/cache/") response = app.get("/cache/")
expect(response["Cache-Control"]).to eq("max-age=38400") expect(response["Cache-Control"]).to eq("max-age=38400")
expect(response.code).to eq("200") expect(response.code).to eq("200")
...@@ -184,6 +189,7 @@ STATIC_JSON ...@@ -184,6 +189,7 @@ STATIC_JSON
expect(response.body.chomp).to eq("index") expect(response.body.chomp).to eq("index")
end end
end end
end
describe "redirect" do describe "redirect" do
let(:name) { "custom_headers_redirect" } let(:name) { "custom_headers_redirect" }
...@@ -199,6 +205,7 @@ STATIC_JSON ...@@ -199,6 +205,7 @@ STATIC_JSON
let(:name) { "custom_headers_clean_urls" } let(:name) { "custom_headers_clean_urls" }
it "should add the headers" do it "should add the headers" do
app.run do
response = app.get("/foo") response = app.get("/foo")
expect(response.code).to eq("200") expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo") expect(response.body.chomp).to eq("foo")
...@@ -209,6 +216,7 @@ STATIC_JSON ...@@ -209,6 +216,7 @@ STATIC_JSON
expect(response.body.chomp).to eq("bar") expect(response.body.chomp).to eq("bar")
expect(response["X-Header"]).to be_nil expect(response["X-Header"]).to be_nil
end end
end
it "should not add headers for .html urls" do it "should not add headers for .html urls" do
response = app.get("/foo.html") response = app.get("/foo.html")
...@@ -221,6 +229,7 @@ STATIC_JSON ...@@ -221,6 +229,7 @@ STATIC_JSON
let(:name) { "custom_headers_routes" } let(:name) { "custom_headers_routes" }
it "should add headers" do it "should add headers" do
app.run do
response = app.get("/active") response = app.get("/active")
expect(response.code).to eq("200") expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index") expect(response.body.chomp).to eq("index")
...@@ -232,6 +241,7 @@ STATIC_JSON ...@@ -232,6 +241,7 @@ STATIC_JSON
expect(response["X-Header"]).to be_nil expect(response["X-Header"]).to be_nil
end end
end end
end
describe "proxies" do describe "proxies" do
include PathHelper include PathHelper
...@@ -269,6 +279,7 @@ STATIC_JSON ...@@ -269,6 +279,7 @@ STATIC_JSON
end end
it "should proxy requests" do it "should proxy requests" do
app.run do
response = app.get("/api/bar/") response = app.get("/api/bar/")
expect(response.code).to eq("200") expect(response.code).to eq("200")
expect(response.body.chomp).to eq("api") expect(response.body.chomp).to eq("api")
...@@ -281,4 +292,5 @@ STATIC_JSON ...@@ -281,4 +292,5 @@ STATIC_JSON
end end
end end
end end
end
end end
...@@ -20,6 +20,7 @@ class AppRunner ...@@ -20,6 +20,7 @@ class AppRunner
CONTAINER_PORT = "3000" CONTAINER_PORT = "3000"
def initialize(fixture, env = nil, debug = false) def initialize(fixture, env = nil, debug = false)
@run = false
@debug = debug @debug = debug
@container = Docker::Container.create( @container = Docker::Container.create(
'Image' => BuildpackBuilder::TAG, 'Image' => BuildpackBuilder::TAG,
...@@ -39,6 +40,8 @@ class AppRunner ...@@ -39,6 +40,8 @@ class AppRunner
end end
def run def run
@run = true
retn = nil
latch = Concurrent::CountDownLatch.new(1) latch = Concurrent::CountDownLatch.new(1)
run_thread = Thread.new { run_thread = Thread.new {
latch.wait(0.5) latch.wait(0.5)
...@@ -51,33 +54,38 @@ class AppRunner ...@@ -51,33 +54,38 @@ class AppRunner
end end
} }
run_thread.join retn = run_thread.value
@container.stop @container.stop
container_thread.join container_thread.join
@run = false
retn
end end
def get(path, max_retries = 5) def get(path, max_retries = 5)
response = nil if @run
response = get_retry(path, max_retries)
else
run { get_retry(path, max_retries) }
end
end
def destroy
@container.delete(force: true) unless @debug
end
run do private
def get_retry(path, max_retries)
network_retry(max_retries) do network_retry(max_retries) do
uri = URI("#{path}") uri = URI("#{path}")
uri.host = HOST_IP if uri.host.nil? uri.host = HOST_IP if uri.host.nil?
uri.port = HOST_PORT if (uri.host == HOST_IP && uri.port != HOST_PORT) || uri.port.nil? uri.port = HOST_PORT if (uri.host == HOST_IP && uri.port != HOST_PORT) || uri.port.nil?
uri.scheme = "http" if uri.scheme.nil? uri.scheme = "http" if uri.scheme.nil?
response = Net::HTTP.get_response(URI(uri.to_s)) Net::HTTP.get_response(URI(uri.to_s))
end end
end end
response
end
def destroy
@container.delete(force: true) unless @debug
end
private
def network_retry(max_retries, retry_count = 0) def network_retry(max_retries, retry_count = 0)
yield yield
rescue Errno::ECONNRESET, EOFError rescue Errno::ECONNRESET, EOFError
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment