25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

662 lines
24 KiB

13 년 전
14 년 전
13 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
13 년 전
13 년 전
13 년 전
  1. %%% File : ibrowse_test.erl
  2. %%% Author : Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  3. %%% Description : Test ibrowse
  4. %%% Created : 14 Oct 2003 by Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  5. -module(ibrowse_test).
  6. -export([
  7. load_test/3,
  8. send_reqs_1/3,
  9. do_send_req/2,
  10. unit_tests/0,
  11. unit_tests/1,
  12. unit_tests_1/2,
  13. ue_test/0,
  14. ue_test/1,
  15. verify_chunked_streaming/0,
  16. verify_chunked_streaming/1,
  17. test_chunked_streaming_once/0,
  18. i_do_async_req_list/4,
  19. test_stream_once/3,
  20. test_stream_once/4,
  21. test_20122010/0,
  22. test_20122010/1,
  23. test_pipeline_head_timeout/0,
  24. test_pipeline_head_timeout/1,
  25. do_test_pipeline_head_timeout/4,
  26. test_head_transfer_encoding/0,
  27. test_head_transfer_encoding/1,
  28. test_head_response_with_body/0,
  29. test_head_response_with_body/1,
  30. test_303_response_with_no_body/0,
  31. test_303_response_with_no_body/1,
  32. test_303_response_with_a_body/0,
  33. test_303_response_with_a_body/1
  34. ]).
  35. test_stream_once(Url, Method, Options) ->
  36. test_stream_once(Url, Method, Options, 5000).
  37. test_stream_once(Url, Method, Options, Timeout) ->
  38. case ibrowse:send_req(Url, [], Method, [], [{stream_to, {self(), once}} | Options], Timeout) of
  39. {ibrowse_req_id, Req_id} ->
  40. case ibrowse:stream_next(Req_id) of
  41. ok ->
  42. test_stream_once(Req_id);
  43. Err ->
  44. Err
  45. end;
  46. Err ->
  47. Err
  48. end.
  49. test_stream_once(Req_id) ->
  50. receive
  51. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  52. io:format("Recvd headers~n~p~n", [{ibrowse_async_headers, Req_id, StatCode, Headers}]),
  53. case ibrowse:stream_next(Req_id) of
  54. ok ->
  55. test_stream_once(Req_id);
  56. Err ->
  57. Err
  58. end;
  59. {ibrowse_async_response, Req_id, {error, Err}} ->
  60. io:format("Recvd error: ~p~n", [Err]);
  61. {ibrowse_async_response, Req_id, Body_1} ->
  62. io:format("Recvd body part: ~n~p~n", [{ibrowse_async_response, Req_id, Body_1}]),
  63. case ibrowse:stream_next(Req_id) of
  64. ok ->
  65. test_stream_once(Req_id);
  66. Err ->
  67. Err
  68. end;
  69. {ibrowse_async_response_end, Req_id} ->
  70. ok
  71. end.
  72. %% Use ibrowse:set_max_sessions/3 and ibrowse:set_max_pipeline_size/3 to
  73. %% tweak settings before running the load test. The defaults are 10 and 10.
  74. load_test(Url, NumWorkers, NumReqsPerWorker) when is_list(Url),
  75. is_integer(NumWorkers),
  76. is_integer(NumReqsPerWorker),
  77. NumWorkers > 0,
  78. NumReqsPerWorker > 0 ->
  79. proc_lib:spawn(?MODULE, send_reqs_1, [Url, NumWorkers, NumReqsPerWorker]).
  80. send_reqs_1(Url, NumWorkers, NumReqsPerWorker) ->
  81. Start_time = now(),
  82. ets:new(pid_table, [named_table, public]),
  83. ets:new(ibrowse_test_results, [named_table, public]),
  84. ets:new(ibrowse_errors, [named_table, public, ordered_set]),
  85. init_results(),
  86. process_flag(trap_exit, true),
  87. log_msg("Starting spawning of workers...~n", []),
  88. spawn_workers(Url, NumWorkers, NumReqsPerWorker),
  89. log_msg("Finished spawning workers...~n", []),
  90. do_wait(Url),
  91. End_time = now(),
  92. log_msg("All workers are done...~n", []),
  93. log_msg("ibrowse_test_results table: ~n~p~n", [ets:tab2list(ibrowse_test_results)]),
  94. log_msg("Start time: ~1000.p~n", [calendar:now_to_local_time(Start_time)]),
  95. log_msg("End time : ~1000.p~n", [calendar:now_to_local_time(End_time)]),
  96. Elapsed_time_secs = trunc(timer:now_diff(End_time, Start_time) / 1000000),
  97. log_msg("Elapsed : ~p~n", [Elapsed_time_secs]),
  98. log_msg("Reqs/sec : ~p~n", [round(trunc((NumWorkers*NumReqsPerWorker) / Elapsed_time_secs))]),
  99. dump_errors().
  100. init_results() ->
  101. ets:insert(ibrowse_test_results, {crash, 0}),
  102. ets:insert(ibrowse_test_results, {send_failed, 0}),
  103. ets:insert(ibrowse_test_results, {other_error, 0}),
  104. ets:insert(ibrowse_test_results, {success, 0}),
  105. ets:insert(ibrowse_test_results, {retry_later, 0}),
  106. ets:insert(ibrowse_test_results, {trid_mismatch, 0}),
  107. ets:insert(ibrowse_test_results, {success_no_trid, 0}),
  108. ets:insert(ibrowse_test_results, {failed, 0}),
  109. ets:insert(ibrowse_test_results, {timeout, 0}),
  110. ets:insert(ibrowse_test_results, {req_id, 0}).
  111. spawn_workers(_Url, 0, _) ->
  112. ok;
  113. spawn_workers(Url, NumWorkers, NumReqsPerWorker) ->
  114. Pid = proc_lib:spawn_link(?MODULE, do_send_req, [Url, NumReqsPerWorker]),
  115. ets:insert(pid_table, {Pid, []}),
  116. spawn_workers(Url, NumWorkers - 1, NumReqsPerWorker).
  117. do_wait(Url) ->
  118. receive
  119. {'EXIT', _, normal} ->
  120. catch ibrowse:show_dest_status(Url),
  121. catch ibrowse:show_dest_status(),
  122. do_wait(Url);
  123. {'EXIT', Pid, Reason} ->
  124. ets:delete(pid_table, Pid),
  125. ets:insert(ibrowse_errors, {Pid, Reason}),
  126. ets:update_counter(ibrowse_test_results, crash, 1),
  127. do_wait(Url);
  128. Msg ->
  129. io:format("Recvd unknown message...~p~n", [Msg]),
  130. do_wait(Url)
  131. after 1000 ->
  132. case ets:info(pid_table, size) of
  133. 0 ->
  134. done;
  135. _ ->
  136. catch ibrowse:show_dest_status(Url),
  137. catch ibrowse:show_dest_status(),
  138. do_wait(Url)
  139. end
  140. end.
  141. do_send_req(Url, NumReqs) ->
  142. do_send_req_1(Url, NumReqs).
  143. do_send_req_1(_Url, 0) ->
  144. ets:delete(pid_table, self());
  145. do_send_req_1(Url, NumReqs) ->
  146. Counter = integer_to_list(ets:update_counter(ibrowse_test_results, req_id, 1)),
  147. case ibrowse:send_req(Url, [{"ib_req_id", Counter}], get, [], [], 10000) of
  148. {ok, _Status, Headers, _Body} ->
  149. case lists:keysearch("ib_req_id", 1, Headers) of
  150. {value, {_, Counter}} ->
  151. ets:update_counter(ibrowse_test_results, success, 1);
  152. {value, _} ->
  153. ets:update_counter(ibrowse_test_results, trid_mismatch, 1);
  154. false ->
  155. ets:update_counter(ibrowse_test_results, success_no_trid, 1)
  156. end;
  157. {error, req_timedout} ->
  158. ets:update_counter(ibrowse_test_results, timeout, 1);
  159. {error, send_failed} ->
  160. ets:update_counter(ibrowse_test_results, send_failed, 1);
  161. {error, retry_later} ->
  162. ets:update_counter(ibrowse_test_results, retry_later, 1);
  163. Err ->
  164. ets:insert(ibrowse_errors, {now(), Err}),
  165. ets:update_counter(ibrowse_test_results, other_error, 1),
  166. ok
  167. end,
  168. do_send_req_1(Url, NumReqs-1).
  169. dump_errors() ->
  170. case ets:info(ibrowse_errors, size) of
  171. 0 ->
  172. ok;
  173. _ ->
  174. {A, B, C} = now(),
  175. Filename = lists:flatten(
  176. io_lib:format("ibrowse_errors_~p_~p_~p.txt" , [A, B, C])),
  177. case file:open(Filename, [write, delayed_write, raw]) of
  178. {ok, Iod} ->
  179. dump_errors(ets:first(ibrowse_errors), Iod);
  180. Err ->
  181. io:format("failed to create file ~s. Reason: ~p~n", [Filename, Err]),
  182. ok
  183. end
  184. end.
  185. dump_errors('$end_of_table', Iod) ->
  186. file:close(Iod);
  187. dump_errors(Key, Iod) ->
  188. [{_, Term}] = ets:lookup(ibrowse_errors, Key),
  189. file:write(Iod, io_lib:format("~p~n", [Term])),
  190. dump_errors(ets:next(ibrowse_errors, Key), Iod).
  191. %%------------------------------------------------------------------------------
  192. %% Unit Tests
  193. %%------------------------------------------------------------------------------
  194. -define(TEST_LIST, [{"http://intranet/messenger", get},
  195. {"http://www.google.co.uk", get},
  196. {"http://www.google.com", get},
  197. {"http://www.google.com", options},
  198. {"https://mail.google.com", get},
  199. {"http://www.sun.com", get},
  200. {"http://www.oracle.com", get},
  201. {"http://www.bbc.co.uk", get},
  202. {"http://www.bbc.co.uk", trace},
  203. {"http://www.bbc.co.uk", options},
  204. {"http://yaws.hyber.org", get},
  205. {"http://jigsaw.w3.org/HTTP/ChunkedScript", get},
  206. {"http://jigsaw.w3.org/HTTP/TE/foo.txt", get},
  207. {"http://jigsaw.w3.org/HTTP/TE/bar.txt", get},
  208. {"http://jigsaw.w3.org/HTTP/connection.html", get},
  209. {"http://jigsaw.w3.org/HTTP/cc.html", get},
  210. {"http://jigsaw.w3.org/HTTP/cc-private.html", get},
  211. {"http://jigsaw.w3.org/HTTP/cc-proxy-revalidate.html", get},
  212. {"http://jigsaw.w3.org/HTTP/cc-nocache.html", get},
  213. {"http://jigsaw.w3.org/HTTP/h-content-md5.html", get},
  214. {"http://jigsaw.w3.org/HTTP/h-retry-after.html", get},
  215. {"http://jigsaw.w3.org/HTTP/h-retry-after-date.html", get},
  216. {"http://jigsaw.w3.org/HTTP/neg", get},
  217. {"http://jigsaw.w3.org/HTTP/negbad", get},
  218. {"http://jigsaw.w3.org/HTTP/400/toolong/", get},
  219. {"http://jigsaw.w3.org/HTTP/300/", get},
  220. {"http://jigsaw.w3.org/HTTP/Basic/", get, [{basic_auth, {"guest", "guest"}}]},
  221. {"http://jigsaw.w3.org/HTTP/CL/", get},
  222. {"http://www.httpwatch.com/httpgallery/chunked/", get},
  223. {"https://github.com", get, [{ssl_options, [{depth, 2}]}]},
  224. {local_test_fun, test_20122010, []},
  225. {local_test_fun, test_pipeline_head_timeout, []},
  226. {local_test_fun, test_head_transfer_encoding, []},
  227. {local_test_fun, test_head_response_with_body, []},
  228. {local_test_fun, test_303_response_with_a_body, []}
  229. ]).
  230. unit_tests() ->
  231. unit_tests([]).
  232. unit_tests(Options) ->
  233. application:start(crypto),
  234. application:start(public_key),
  235. application:start(ssl),
  236. (catch ibrowse_test_server:start_server(8181, tcp)),
  237. ibrowse:start(),
  238. Options_1 = Options ++ [{connect_timeout, 5000}],
  239. Test_timeout = proplists:get_value(test_timeout, Options, 60000),
  240. {Pid, Ref} = erlang:spawn_monitor(?MODULE, unit_tests_1, [self(), Options_1]),
  241. receive
  242. {done, Pid} ->
  243. ok;
  244. {'DOWN', Ref, _, _, Info} ->
  245. io:format("Test process crashed: ~p~n", [Info])
  246. after Test_timeout ->
  247. exit(Pid, kill),
  248. io:format("Timed out waiting for tests to complete~n", [])
  249. end,
  250. catch ibrowse_test_server:stop_server(8181),
  251. ok.
  252. unit_tests_1(Parent, Options) ->
  253. lists:foreach(fun({local_test_fun, Fun_name, Args}) ->
  254. execute_req(local_test_fun, Fun_name, Args);
  255. ({Url, Method}) ->
  256. execute_req(Url, Method, Options);
  257. ({Url, Method, X_Opts}) ->
  258. execute_req(Url, Method, X_Opts ++ Options)
  259. end, ?TEST_LIST),
  260. Parent ! {done, self()}.
  261. verify_chunked_streaming() ->
  262. verify_chunked_streaming([]).
  263. verify_chunked_streaming(Options) ->
  264. io:format("~nVerifying that chunked streaming is working...~n", []),
  265. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  266. io:format(" URL: ~s~n", [Url]),
  267. io:format(" Fetching data without streaming...~n", []),
  268. Result_without_streaming = ibrowse:send_req(
  269. Url, [], get, [],
  270. [{response_format, binary} | Options]),
  271. io:format(" Fetching data with streaming as list...~n", []),
  272. Async_response_list = do_async_req_list(
  273. Url, get, [{response_format, list} | Options]),
  274. io:format(" Fetching data with streaming as binary...~n", []),
  275. Async_response_bin = do_async_req_list(
  276. Url, get, [{response_format, binary} | Options]),
  277. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  278. Async_response_bin_once = do_async_req_list(
  279. Url, get, [once, {response_format, binary} | Options]),
  280. Res1 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin),
  281. Res2 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin_once),
  282. case {Res1, Res2} of
  283. {success, success} ->
  284. io:format(" Chunked streaming working~n", []);
  285. _ ->
  286. ok
  287. end.
  288. test_chunked_streaming_once() ->
  289. test_chunked_streaming_once([]).
  290. test_chunked_streaming_once(Options) ->
  291. io:format("~nTesting chunked streaming with the {stream_to, {Pid, once}} option...~n", []),
  292. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  293. io:format(" URL: ~s~n", [Url]),
  294. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  295. case do_async_req_list(Url, get, [once, {response_format, binary} | Options]) of
  296. {ok, _, _, _} ->
  297. io:format(" Success!~n", []);
  298. Err ->
  299. io:format(" Fail: ~p~n", [Err])
  300. end.
  301. compare_responses({ok, St_code, _, Body}, {ok, St_code, _, Body}, {ok, St_code, _, Body}) ->
  302. success;
  303. compare_responses({ok, St_code, _, Body_1}, {ok, St_code, _, Body_2}, {ok, St_code, _, Body_3}) ->
  304. case Body_1 of
  305. Body_2 ->
  306. io:format("Body_1 and Body_2 match~n", []);
  307. Body_3 ->
  308. io:format("Body_1 and Body_3 match~n", []);
  309. _ when Body_2 == Body_3 ->
  310. io:format("Body_2 and Body_3 match~n", []);
  311. _ ->
  312. io:format("All three bodies are different!~n", [])
  313. end,
  314. io:format("Body_1 -> ~p~n", [Body_1]),
  315. io:format("Body_2 -> ~p~n", [Body_2]),
  316. io:format("Body_3 -> ~p~n", [Body_3]),
  317. fail_bodies_mismatch;
  318. compare_responses(R1, R2, R3) ->
  319. io:format("R1 -> ~p~n", [R1]),
  320. io:format("R2 -> ~p~n", [R2]),
  321. io:format("R3 -> ~p~n", [R3]),
  322. fail.
  323. %% do_async_req_list(Url) ->
  324. %% do_async_req_list(Url, get).
  325. %% do_async_req_list(Url, Method) ->
  326. %% do_async_req_list(Url, Method, [{stream_to, self()},
  327. %% {stream_chunk_size, 1000}]).
  328. do_async_req_list(Url, Method, Options) ->
  329. {Pid,_} = erlang:spawn_monitor(?MODULE, i_do_async_req_list,
  330. [self(), Url, Method,
  331. Options ++ [{stream_chunk_size, 1000}]]),
  332. %% io:format("Spawned process ~p~n", [Pid]),
  333. wait_for_resp(Pid).
  334. wait_for_resp(Pid) ->
  335. receive
  336. {async_result, Pid, Res} ->
  337. Res;
  338. {async_result, Other_pid, _} ->
  339. io:format("~p: Waiting for result from ~p: got from ~p~n", [self(), Pid, Other_pid]),
  340. wait_for_resp(Pid);
  341. {'DOWN', _, _, Pid, Reason} ->
  342. {'EXIT', Reason};
  343. {'DOWN', _, _, _, _} ->
  344. wait_for_resp(Pid);
  345. Msg ->
  346. io:format("Recvd unknown message: ~p~n", [Msg]),
  347. wait_for_resp(Pid)
  348. after 100000 ->
  349. {error, timeout}
  350. end.
  351. i_do_async_req_list(Parent, Url, Method, Options) ->
  352. Options_1 = case lists:member(once, Options) of
  353. true ->
  354. [{stream_to, {self(), once}} | (Options -- [once])];
  355. false ->
  356. [{stream_to, self()} | Options]
  357. end,
  358. Res = ibrowse:send_req(Url, [], Method, [], Options_1),
  359. case Res of
  360. {ibrowse_req_id, Req_id} ->
  361. Result = wait_for_async_resp(Req_id, Options, undefined, undefined, []),
  362. Parent ! {async_result, self(), Result};
  363. Err ->
  364. Parent ! {async_result, self(), Err}
  365. end.
  366. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, Body) ->
  367. receive
  368. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  369. %% io:format("Recvd headers...~n", []),
  370. maybe_stream_next(Req_id, Options),
  371. wait_for_async_resp(Req_id, Options, StatCode, Headers, Body);
  372. {ibrowse_async_response_end, Req_id} ->
  373. %% io:format("Recvd end of response.~n", []),
  374. Body_1 = list_to_binary(lists:reverse(Body)),
  375. {ok, Acc_Stat_code, Acc_Headers, Body_1};
  376. {ibrowse_async_response, Req_id, Data} ->
  377. maybe_stream_next(Req_id, Options),
  378. %% io:format("Recvd data...~n", []),
  379. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, [Data | Body]);
  380. {ibrowse_async_response, Req_id, {error, _} = Err} ->
  381. {ok, Acc_Stat_code, Acc_Headers, Err};
  382. Err ->
  383. {ok, Acc_Stat_code, Acc_Headers, Err}
  384. after 10000 ->
  385. {timeout, Acc_Stat_code, Acc_Headers, Body}
  386. end.
  387. maybe_stream_next(Req_id, Options) ->
  388. case lists:member(once, Options) of
  389. true ->
  390. ibrowse:stream_next(Req_id);
  391. false ->
  392. ok
  393. end.
  394. execute_req(local_test_fun, Method, Args) ->
  395. io:format(" ~-54.54w: ", [Method]),
  396. Result = (catch apply(?MODULE, Method, Args)),
  397. io:format("~p~n", [Result]);
  398. execute_req(Url, Method, Options) ->
  399. io:format("~7.7w, ~50.50s: ", [Method, Url]),
  400. Result = (catch ibrowse:send_req(Url, [], Method, [], Options)),
  401. case Result of
  402. {ok, SCode, _H, _B} ->
  403. io:format("Status code: ~p~n", [SCode]);
  404. Err ->
  405. io:format("~p~n", [Err])
  406. end.
  407. ue_test() ->
  408. ue_test(lists:duplicate(1024, $?)).
  409. ue_test(Data) ->
  410. {Time, Res} = timer:tc(ibrowse_lib, url_encode, [Data]),
  411. io:format("Time -> ~p~n", [Time]),
  412. io:format("Data Length -> ~p~n", [length(Data)]),
  413. io:format("Res Length -> ~p~n", [length(Res)]).
  414. % io:format("Result -> ~s~n", [Res]).
  415. log_msg(Fmt, Args) ->
  416. io:format("~s -- " ++ Fmt,
  417. [ibrowse_lib:printable_date() | Args]).
  418. %%------------------------------------------------------------------------------
  419. %% Test what happens when the response to a HEAD request is a
  420. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  421. %% Github
  422. %% ------------------------------------------------------------------------------
  423. test_head_transfer_encoding() ->
  424. clear_msg_q(),
  425. test_head_transfer_encoding("http://localhost:8181/ibrowse_head_test").
  426. test_head_transfer_encoding(Url) ->
  427. case ibrowse:send_req(Url, [], head) of
  428. {ok, "200", _, _} ->
  429. success;
  430. Res ->
  431. {test_failed, Res}
  432. end.
  433. %%------------------------------------------------------------------------------
  434. %% Test what happens when the response to a HEAD request is a
  435. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  436. %% Github
  437. %% ------------------------------------------------------------------------------
  438. test_head_response_with_body() ->
  439. clear_msg_q(),
  440. test_head_response_with_body("http://localhost:8181/ibrowse_head_transfer_enc").
  441. test_head_response_with_body(Url) ->
  442. case ibrowse:send_req(Url, [], head, [], [{workaround, head_response_with_body}]) of
  443. {ok, "400", _, _} ->
  444. success;
  445. Res ->
  446. {test_failed, Res}
  447. end.
  448. %%------------------------------------------------------------------------------
  449. %% Test what happens when a 303 response has no body
  450. %% Github issue #97
  451. %% ------------------------------------------------------------------------------
  452. test_303_response_with_no_body() ->
  453. clear_msg_q(),
  454. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_no_body_test").
  455. test_303_response_with_no_body(Url) ->
  456. ibrowse:add_config([{allow_303_with_no_body, true}]),
  457. case ibrowse:send_req(Url, [], post) of
  458. {ok, "303", _, _} ->
  459. success;
  460. Res ->
  461. {test_failed, Res}
  462. end.
  463. %% Make sure we don't break requests that do have a body.
  464. test_303_response_with_a_body() ->
  465. clear_msg_q(),
  466. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_with_body_test").
  467. test_303_response_with_a_body(Url) ->
  468. ibrowse:add_config([{allow_303_with_no_body, true}]),
  469. case ibrowse:send_req(Url, [], post) of
  470. {ok, "303", _, "abcde"} ->
  471. success;
  472. Res ->
  473. {test_failed, Res}
  474. end.
  475. %%------------------------------------------------------------------------------
  476. %% Test what happens when the request at the head of a pipeline times out
  477. %%------------------------------------------------------------------------------
  478. test_pipeline_head_timeout() ->
  479. clear_msg_q(),
  480. test_pipeline_head_timeout("http://localhost:8181/ibrowse_inac_timeout_test").
  481. test_pipeline_head_timeout(Url) ->
  482. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  483. Test_parent = self(),
  484. Fun = fun({fixed, Timeout}) ->
  485. spawn(fun() ->
  486. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  487. end);
  488. (Timeout_mult) ->
  489. spawn(fun() ->
  490. Timeout = 1000 + Timeout_mult*1000,
  491. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  492. end)
  493. end,
  494. Pids = [Fun(X) || X <- [{fixed, 32000} | lists:seq(1,10)]],
  495. Result = accumulate_worker_resp(Pids),
  496. case lists:all(fun({_, X_res}) ->
  497. X_res == {error,req_timedout}
  498. end, Result) of
  499. true ->
  500. success;
  501. false ->
  502. {test_failed, Result}
  503. end.
  504. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Req_timeout) ->
  505. Resp = ibrowse:send_req_direct(
  506. Pid,
  507. Url,
  508. [], get, [],
  509. [{socket_options,[{keepalive,true}]},
  510. {inactivity_timeout,180000},
  511. {connect_timeout,180000}], Req_timeout),
  512. Test_parent ! {self(), Resp}.
  513. accumulate_worker_resp(Pids) ->
  514. accumulate_worker_resp(Pids, []).
  515. accumulate_worker_resp([_ | _] = Pids, Acc) ->
  516. receive
  517. {Pid, Res} when is_pid(Pid) ->
  518. accumulate_worker_resp(Pids -- [Pid], [{Pid, Res} | Acc]);
  519. Err ->
  520. io:format("Received unexpected: ~p~n", [Err])
  521. end;
  522. accumulate_worker_resp([], Acc) ->
  523. lists:reverse(Acc).
  524. clear_msg_q() ->
  525. receive
  526. _ ->
  527. clear_msg_q()
  528. after 0 ->
  529. ok
  530. end.
  531. %%------------------------------------------------------------------------------
  532. %%
  533. %%------------------------------------------------------------------------------
  534. test_20122010() ->
  535. test_20122010("http://localhost:8181").
  536. test_20122010(Url) ->
  537. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  538. Expected_resp = <<"1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-60-61-62-63-64-65-66-67-68-69-70-71-72-73-74-75-76-77-78-79-80-81-82-83-84-85-86-87-88-89-90-91-92-93-94-95-96-97-98-99-100">>,
  539. Test_parent = self(),
  540. Fun = fun() ->
  541. do_test_20122010(Url, Pid, Expected_resp, Test_parent)
  542. end,
  543. Pids = [erlang:spawn_monitor(Fun) || _ <- lists:seq(1,10)],
  544. wait_for_workers(Pids).
  545. wait_for_workers([{Pid, _Ref} | Pids]) ->
  546. receive
  547. {Pid, success} ->
  548. wait_for_workers(Pids)
  549. after 60000 ->
  550. test_failed
  551. end;
  552. wait_for_workers([]) ->
  553. success.
  554. do_test_20122010(Url, Pid, Expected_resp, Test_parent) ->
  555. do_test_20122010(10, Url, Pid, Expected_resp, Test_parent).
  556. do_test_20122010(0, _Url, _Pid, _Expected_resp, Test_parent) ->
  557. Test_parent ! {self(), success};
  558. do_test_20122010(Rem_count, Url, Pid, Expected_resp, Test_parent) ->
  559. {ibrowse_req_id, Req_id} = ibrowse:send_req_direct(
  560. Pid,
  561. Url ++ "/ibrowse_stream_once_chunk_pipeline_test",
  562. [], get, [],
  563. [{stream_to, {self(), once}},
  564. {inactivity_timeout, 10000},
  565. {include_ibrowse_req_id, true}]),
  566. do_trace("~p -- sent request ~1000.p~n", [self(), Req_id]),
  567. Req_id_str = lists:flatten(io_lib:format("~1000.p",[Req_id])),
  568. receive
  569. {ibrowse_async_headers, Req_id, "200", Headers} ->
  570. case lists:keysearch("x-ibrowse-request-id", 1, Headers) of
  571. {value, {_, Req_id_str}} ->
  572. ok;
  573. {value, {_, Req_id_1}} ->
  574. do_trace("~p -- Sent req-id: ~1000.p. Recvd: ~1000.p~n",
  575. [self(), Req_id, Req_id_1]),
  576. exit(req_id_mismatch)
  577. end
  578. after 5000 ->
  579. do_trace("~p -- response headers not received~n", [self()]),
  580. exit({timeout, test_failed})
  581. end,
  582. do_trace("~p -- response headers received~n", [self()]),
  583. ok = ibrowse:stream_next(Req_id),
  584. case do_test_20122010_1(Expected_resp, Req_id, []) of
  585. true ->
  586. do_test_20122010(Rem_count - 1, Url, Pid, Expected_resp, Test_parent);
  587. false ->
  588. Test_parent ! {self(), failed}
  589. end.
  590. do_test_20122010_1(Expected_resp, Req_id, Acc) ->
  591. receive
  592. {ibrowse_async_response, Req_id, Body_part} ->
  593. ok = ibrowse:stream_next(Req_id),
  594. do_test_20122010_1(Expected_resp, Req_id, [Body_part | Acc]);
  595. {ibrowse_async_response_end, Req_id} ->
  596. Acc_1 = list_to_binary(lists:reverse(Acc)),
  597. Result = Acc_1 == Expected_resp,
  598. do_trace("~p -- End of response. Result: ~p~n", [self(), Result]),
  599. Result
  600. after 1000 ->
  601. exit({timeout, test_failed})
  602. end.
  603. do_trace(Fmt, Args) ->
  604. do_trace(get(my_trace_flag), Fmt, Args).
  605. do_trace(true, Fmt, Args) ->
  606. io:format("~s -- " ++ Fmt, [ibrowse_lib:printable_date() | Args]);
  607. do_trace(_, _, _) ->
  608. ok.