aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-11-01 17:06:37 +0000
committerLoïc Hoguin <[email protected]>2017-11-01 17:06:37 +0000
commit5bb6438e10ae6b620b88f8d9989e6f6c0d252f34 (patch)
tree7c5fb1cde5e31df7a069889ccbdde3f2c51bc347
parent836342abb86b3ff15d1c8319a455d776f7027a87 (diff)
downloadcowboy-5bb6438e10ae6b620b88f8d9989e6f6c0d252f34.tar.gz
cowboy-5bb6438e10ae6b620b88f8d9989e6f6c0d252f34.tar.bz2
cowboy-5bb6438e10ae6b620b88f8d9989e6f6c0d252f34.zip
Don't crash when cowboy_clock is not running
This can happen normally when Cowboy is restarted, for example. Instead of failing requests when that happens, we degrade gracefully and do a little more work to provide the current date header.
-rw-r--r--src/cowboy_clock.erl10
-rw-r--r--test/misc_SUITE.erl29
2 files changed, 36 insertions, 3 deletions
diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl
index e0e387d..28f8a1b 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -49,9 +49,17 @@ start_link() ->
stop() ->
gen_server:call(?MODULE, stop).
+%% When the ets table doesn't exist, either because of a bug
+%% or because Cowboy is being restarted, we perform in a
+%% slightly degraded state and build a new timestamp for
+%% every request.
-spec rfc1123() -> binary().
rfc1123() ->
- ets:lookup_element(?MODULE, rfc1123, 2).
+ try
+ ets:lookup_element(?MODULE, rfc1123, 2)
+ catch error:badarg ->
+ rfc1123(erlang:universaltime())
+ end.
-spec rfc1123(calendar:datetime()) -> binary().
rfc1123(DateTime) ->
diff --git a/test/misc_SUITE.erl b/test/misc_SUITE.erl
index 1e0012e..6d39848 100644
--- a/test/misc_SUITE.erl
+++ b/test/misc_SUITE.erl
@@ -23,9 +23,14 @@ all() ->
[{group, no_env}|cowboy_test:common_all()].
groups() ->
- Common = ct_helper:all(?MODULE) -- [set_env_missing],
- [{no_env, [], [set_env_missing]}|cowboy_test:common_groups(Common)].
+ Common = ct_helper:all(?MODULE) -- [restart_gracefully, set_env_missing],
+ [
+ {app, [], [restart_gracefully]},
+ {no_env, [], [set_env_missing]}
+ |cowboy_test:common_groups(Common)].
+init_per_group(app, Config) ->
+ cowboy_test:init_common_groups(http, Config, ?MODULE);
init_per_group(Name=no_env, Config) ->
cowboy_test:init_http(Name, #{}, Config);
init_per_group(Name, Config) ->
@@ -39,6 +44,26 @@ init_dispatch(_) ->
{"/", hello_h, []}
]}]).
+%% Tests.
+
+restart_gracefully(Config) ->
+ doc("Ensure we can process request when the cowboy application is being restarted."),
+ ConnPid = gun_open(Config),
+ %% We can do a request before stopping cowboy.
+ Ref1 = gun:get(ConnPid, "/"),
+ {response, _, 200, _} = gun:await(ConnPid, Ref1),
+ %% Stop the cowboy application.
+ ok = application:stop(cowboy),
+ %% We can still do a request even though cowboy is stopped.
+ Ref2 = gun:get(ConnPid, "/"),
+ {response, _, 200, _} = gun:await(ConnPid, Ref2),
+ %% Start the cowboy application again.
+ ok = application:start(cowboy),
+ %% Even after restarting there are no issues.
+ Ref3 = gun:get(ConnPid, "/"),
+ {response, _, 200, _} = gun:await(ConnPid, Ref3),
+ ok.
+
router_invalid_path(Config) ->
doc("Ensure a path with invalid percent-encoded characters results in a 400."),
ConnPid = gun_open(Config),