You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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