Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

73 řádky
2.3 KiB

  1. %%% File : ibrowse_functional_tests.erl
  2. %%% Authors : Benjamin Lee <yardspoon@gmail.com>
  3. %%% Brian Richards <bmrichards16@gmail.com>
  4. %%% Description : Functional tests of the ibrowse library using a live test HTTP server
  5. %%% Created : 18 November 2014 by Benjamin Lee <yardspoon@gmail.com>
  6. -module(ibrowse_functional_tests).
  7. -include_lib("eunit/include/eunit.hrl").
  8. -define(PER_TEST_TIMEOUT_SEC, 60).
  9. -define(TIMEDTEST(Desc, Fun), {Desc, {timeout, ?PER_TEST_TIMEOUT_SEC, fun Fun/0}}).
  10. -define(SERVER_PORT, 8181).
  11. -define(BASE_URL, "http://localhost:" ++ integer_to_list(?SERVER_PORT)).
  12. setup() ->
  13. application:start(crypto),
  14. application:start(public_key),
  15. application:start(ssl),
  16. ibrowse_test_server:start_server(?SERVER_PORT, tcp),
  17. ibrowse:start(),
  18. ok.
  19. teardown(_) ->
  20. ibrowse:stop(),
  21. ibrowse_test_server:stop_server(?SERVER_PORT),
  22. ok.
  23. running_server_fixture_test_() ->
  24. {foreach,
  25. fun setup/0,
  26. fun teardown/1,
  27. [
  28. ?TIMEDTEST("Simple request can be honored", simple_request),
  29. ?TIMEDTEST("Slow server causes timeout", slow_server_timeout),
  30. ?TIMEDTEST("Requests are balanced over connections", balanced_connections)
  31. ]
  32. }.
  33. simple_request() ->
  34. ?assertMatch({ok, "200", _, _}, ibrowse:send_req(?BASE_URL, [], get, [], [])).
  35. slow_server_timeout() ->
  36. ?assertMatch({error, req_timedout}, ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [], 5000)).
  37. balanced_connections() ->
  38. MaxSessions = 4,
  39. MaxPipeline = 100,
  40. RequestsSent = 80,
  41. BalancedNumberOfRequestsPerConnection = 20,
  42. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()),
  43. Fun = fun() -> ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], 30000) end,
  44. times(RequestsSent, fun() -> spawn_link(Fun) end),
  45. timer:sleep(1000),
  46. Diffs = [Count - BalancedNumberOfRequestsPerConnection || {_Pid, Count} <- ibrowse_test_server:get_conn_pipeline_depth()],
  47. ?assertEqual(MaxSessions, length(Diffs)),
  48. lists:foreach(fun(X) -> ?assertEqual(yep, close_to_zero(X)) end, Diffs).
  49. close_to_zero(0) -> yep;
  50. close_to_zero(-1) -> yep;
  51. close_to_zero(1) -> yep;
  52. close_to_zero(X) -> {nope, X}.
  53. times(0, _) ->
  54. ok;
  55. times(X, Fun) ->
  56. Fun(),
  57. times(X - 1, Fun).