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,17 +59,19 @@ RSpec.describe "Simple" do
let(:name) { "routes" }
it "should support custom routes" do
response = app.get("/foo.html")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello world")
app.run do
response = app.get("/foo.html")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello world")
response = app.get("/route/foo")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello from route")
response = app.get("/route/foo")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello from route")
response = app.get("/route/foo/bar")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello from route")
response = app.get("/route/foo/bar")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("hello from route")
end
end
end
......@@ -158,30 +160,34 @@ STATIC_JSON
let(:name) { "custom_headers" }
it "should return the respected headers only for the path specified" do
response = app.get("/")
expect(response["cache-control"]).to eq("no-cache")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
app.run do
response = app.get("/")
expect(response["cache-control"]).to eq("no-cache")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
response = app.get("/foo.html")
expect(response["cache-control"]).to eq(nil)
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
response = app.get("/foo.html")
expect(response["cache-control"]).to eq(nil)
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
end
end
describe "wildcard paths" do
let(:name) { "custom_headers_wildcard" }
it "should add the headers" do
response = app.get("/cache/")
expect(response["Cache-Control"]).to eq("max-age=38400")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("cached index")
response = app.get("/")
expect(response["Cache-Control"]).to eq(nil)
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
app.run do
response = app.get("/cache/")
expect(response["Cache-Control"]).to eq("max-age=38400")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("cached index")
response = app.get("/")
expect(response["Cache-Control"]).to eq(nil)
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
end
end
end
......@@ -199,15 +205,17 @@ STATIC_JSON
let(:name) { "custom_headers_clean_urls" }
it "should add the headers" do
response = app.get("/foo")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
expect(response["X-Header"]).to eq("present")
response = app.get("/bar")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("bar")
expect(response["X-Header"]).to be_nil
app.run do
response = app.get("/foo")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
expect(response["X-Header"]).to eq("present")
response = app.get("/bar")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("bar")
expect(response["X-Header"]).to be_nil
end
end
it "should not add headers for .html urls" do
......@@ -221,15 +229,17 @@ STATIC_JSON
let(:name) { "custom_headers_routes" }
it "should add headers" do
response = app.get("/active")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
expect(response["X-Header"]).to eq("present")
response = app.get("/foo/foo.html")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
expect(response["X-Header"]).to be_nil
app.run do
response = app.get("/active")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("index")
expect(response["X-Header"]).to eq("present")
response = app.get("/foo/foo.html")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("foo")
expect(response["X-Header"]).to be_nil
end
end
end
......@@ -269,15 +279,17 @@ STATIC_JSON
end
it "should proxy requests" do
response = app.get("/api/bar/")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("api")
expect(response["X-Header"]).to eq("present")
response = app.get("/api/baz/")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("baz")
expect(response["X-Header"]).to be_nil
app.run do
response = app.get("/api/bar/")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("api")
expect(response["X-Header"]).to eq("present")
response = app.get("/api/baz/")
expect(response.code).to eq("200")
expect(response.body.chomp).to eq("baz")
expect(response["X-Header"]).to be_nil
end
end
end
end
......
......@@ -20,6 +20,7 @@ class AppRunner
CONTAINER_PORT = "3000"
def initialize(fixture, env = nil, debug = false)
@run = false
@debug = debug
@container = Docker::Container.create(
'Image' => BuildpackBuilder::TAG,
......@@ -39,7 +40,9 @@ class AppRunner
end
def run
latch = Concurrent::CountDownLatch.new(1)
@run = true
retn = nil
latch = Concurrent::CountDownLatch.new(1)
run_thread = Thread.new {
latch.wait(0.5)
yield(@container)
......@@ -51,26 +54,20 @@ class AppRunner
end
}
run_thread.join
retn = run_thread.value
@container.stop
container_thread.join
@run = false
retn
end
def get(path, max_retries = 5)
response = nil
run do
network_retry(max_retries) do
uri = URI("#{path}")
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.scheme = "http" if uri.scheme.nil?
response = Net::HTTP.get_response(URI(uri.to_s))
end
if @run
response = get_retry(path, max_retries)
else
run { get_retry(path, max_retries) }
end
response
end
def destroy
......@@ -78,6 +75,17 @@ class AppRunner
end
private
def get_retry(path, max_retries)
network_retry(max_retries) do
uri = URI("#{path}")
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.scheme = "http" if uri.scheme.nil?
Net::HTTP.get_response(URI(uri.to_s))
end
end
def network_retry(max_retries, retry_count = 0)
yield
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