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ů.

134 řádky
4.8 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. -define(SHORT_TIMEOUT_MS, 5000).
  13. -define(LONG_TIMEOUT_MS, 30000).
  14. -define(PAUSE_FOR_CONNECTIONS_MS, 2000).
  15. setup() ->
  16. application:start(crypto),
  17. application:start(public_key),
  18. application:start(ssl),
  19. ibrowse_test_server:start_server(?SERVER_PORT, tcp),
  20. ibrowse:start(),
  21. ok.
  22. teardown(_) ->
  23. ibrowse:stop(),
  24. ibrowse_test_server:stop_server(?SERVER_PORT),
  25. ok.
  26. running_server_fixture_test_() ->
  27. {foreach,
  28. fun setup/0,
  29. fun teardown/1,
  30. [
  31. ?TIMEDTEST("Simple request can be honored", simple_request),
  32. ?TIMEDTEST("Slow server causes timeout", slow_server_timeout),
  33. ?TIMEDTEST("Pipeline depth goes down with responses", pipeline_depth),
  34. ?TIMEDTEST("Timeout closes pipe", closing_pipes),
  35. ?TIMEDTEST("Requests are balanced over connections", balanced_connections),
  36. ?TIMEDTEST("Pipeline too small signals retries", small_pipeline)
  37. ]
  38. }.
  39. simple_request() ->
  40. ?assertMatch({ok, "200", _, _}, ibrowse:send_req(?BASE_URL, [], get, [], [])).
  41. slow_server_timeout() ->
  42. ?assertMatch({error, req_timedout}, ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [], 5000)).
  43. pipeline_depth() ->
  44. MaxSessions = 2,
  45. MaxPipeline = 2,
  46. RequestsSent = 2,
  47. EmptyPipelineDepth = 0,
  48. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()),
  49. Fun = fun() -> ibrowse:send_req(?BASE_URL, [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], ?SHORT_TIMEOUT_MS) end,
  50. times(RequestsSent, fun() -> spawn_link(Fun) end),
  51. timer:sleep(?PAUSE_FOR_CONNECTIONS_MS),
  52. Counts = [Count || {_Pid, Count} <- ibrowse_test_server:get_conn_pipeline_depth()],
  53. ?assertEqual(MaxSessions, length(Counts)),
  54. ?assertEqual(lists:duplicate(MaxSessions, EmptyPipelineDepth), Counts).
  55. closing_pipes() ->
  56. MaxSessions = 2,
  57. MaxPipeline = 2,
  58. RequestsSent = 2,
  59. BalancedNumberOfRequestsPerConnection = 1,
  60. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()),
  61. Fun = fun() -> ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], ?SHORT_TIMEOUT_MS) end,
  62. times(RequestsSent, fun() -> spawn_link(Fun) end),
  63. timer:sleep(?PAUSE_FOR_CONNECTIONS_MS),
  64. Counts = [Count || {_Pid, Count} <- ibrowse_test_server:get_conn_pipeline_depth()],
  65. ?assertEqual(MaxSessions, length(Counts)),
  66. ?assertEqual(lists:duplicate(MaxSessions, BalancedNumberOfRequestsPerConnection), Counts),
  67. timer:sleep(?SHORT_TIMEOUT_MS),
  68. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()).
  69. balanced_connections() ->
  70. MaxSessions = 4,
  71. MaxPipeline = 100,
  72. RequestsSent = 80,
  73. BalancedNumberOfRequestsPerConnection = 20,
  74. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()),
  75. Fun = fun() -> ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], ?LONG_TIMEOUT_MS) end,
  76. times(RequestsSent, fun() -> spawn_link(Fun) end),
  77. timer:sleep(?PAUSE_FOR_CONNECTIONS_MS),
  78. Counts = [Count || {_Pid, Count} <- ibrowse_test_server:get_conn_pipeline_depth()],
  79. ?assertEqual(MaxSessions, length(Counts)),
  80. ?assertEqual(lists:duplicate(MaxSessions, BalancedNumberOfRequestsPerConnection), Counts).
  81. small_pipeline() ->
  82. MaxSessions = 10,
  83. MaxPipeline = 10,
  84. RequestsSent = 100,
  85. FullRequestsPerConnection = 10,
  86. ?assertEqual([], ibrowse_test_server:get_conn_pipeline_depth()),
  87. Fun = fun() -> ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], ?SHORT_TIMEOUT_MS) end,
  88. times(RequestsSent, fun() -> spawn(Fun) end),
  89. timer:sleep(?PAUSE_FOR_CONNECTIONS_MS), %% Wait for everyone to get in line
  90. Counts = [Count || {_Pid, Count} <- ibrowse_test_server:get_conn_pipeline_depth()],
  91. ?assertEqual(MaxSessions, length(Counts)),
  92. ?assertEqual(lists:duplicate(MaxSessions, FullRequestsPerConnection), Counts),
  93. Response = ibrowse:send_req(?BASE_URL ++ "/never_respond", [], get, [], [{max_sessions, MaxSessions}, {max_pipeline_size, MaxPipeline}], ?SHORT_TIMEOUT_MS),
  94. ?assertEqual({error, retry_later}, Response).
  95. times(0, _) ->
  96. ok;
  97. times(X, Fun) ->
  98. Fun(),
  99. times(X - 1, Fun).