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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2022. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%% Purpose : Test the timer module a simpler/faster test than timer_SUITE
-module(timer_simple_SUITE).
%% This test suite matches on opaque tref() return values from the *_after
%% and *_interval functions, namely {instant, _}, {send_local, _}, {once, _}
%% and {interval, _}.
%% If the implementation changes, the test suite has to be changed accordingly.
%% external
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
init_per_testcase/2,
apply_after1/1,
apply_after2/1,
apply_after3/1,
apply_after4/1,
apply_after_invalid_args/1,
send_after1/1,
send_after2/1,
send_after3/1,
send_after4/1,
send_after5/1,
send_after6/1,
send_after7/1,
send_after_invalid_args/1,
exit_after1/1,
exit_after2/1,
exit_after3/1,
exit_after4/1,
kill_after1/1,
kill_after2/1,
kill_after3/1,
apply_interval1/1,
apply_interval_invalid_args/1,
send_interval1/1,
send_interval2/1,
send_interval3/1,
send_interval4/1,
send_interval5/1,
send_interval_invalid_args/1,
cancel1/1,
cancel2/1,
cancel3/1,
cancel4/1,
cancel5/1,
cancel6/1,
cancel_invalid_args/1,
sleep1/1,
sleep2/1,
tc/1,
unexpected1/1,
unexpected2/1,
unexpected3/1,
nonexistent1/1,
nonexistent2/1,
timer_perf/1]).
%% internal
-export([forever/0,
do_nrev/2,
send/2,
timer/4,
timer/5]).
-include_lib("common_test/include/ct.hrl").
suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,10}}].
all() ->
[
{group, apply_after},
{group, send_after},
{group, exit_after},
{group, kill_after},
{group, apply_interval},
{group, send_interval},
{group, cancel},
{group, sleep},
{group, misc}
].
groups() ->
[
{
apply_after,
[],
[
apply_after1,
apply_after2,
apply_after3,
apply_after4,
apply_after_invalid_args
]
},
{
send_after,
[],
[
send_after1,
send_after2,
send_after3,
send_after4,
send_after5,
send_after6,
send_after7,
send_after_invalid_args
]
},
{
exit_after,
[],
[
exit_after1,
exit_after2,
exit_after3,
exit_after4
]
},
{
kill_after,
[],
[
kill_after1,
kill_after2,
kill_after3
]
},
{
apply_interval,
[],
[
apply_interval1,
apply_interval_invalid_args
]
},
{
send_interval,
[],
[
send_interval1,
send_interval2,
send_interval3,
send_interval4,
send_interval5,
send_interval_invalid_args
]
},
{
cancel,
[],
[
cancel1,
cancel2,
cancel3,
cancel4,
cancel5,
cancel6,
cancel_invalid_args
]
},
{
sleep,
[],
[
sleep1,
sleep2
]
},
{
misc,
[],
[
tc,
unexpected1,
unexpected2,
unexpected3,
nonexistent1,
nonexistent2,
timer_perf
]
}
].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
init_per_testcase(_, Config) when is_list(Config) ->
ok = timer:start(),
Config.
%% Testing timer interface!!
%% Test of apply_after with time = 0, with sending of message.
apply_after1(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, {instant, _}} = timer:apply_after(0, ?MODULE, send, [self(), Msg]),
ok = get_mess(1000, Msg).
%% Test of apply_after with time = 500, with sending of message.
apply_after2(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, {once, _}} = timer:apply_after(500, ?MODULE, send, [self(), Msg]),
ok = get_mess(1000, Msg).
%% Test that a request starts the timer server if it is not running.
apply_after3(Config) when is_list(Config) ->
ok = supervisor:terminate_child(kernel_sup, timer_server),
Msg = make_ref(),
timer:apply_after(100, erlang, send, [self(), Msg]),
ok = get_mess(500, Msg),
{timer_server, Pid, worker, [timer]} = lists:keyfind(timer_server, 1, supervisor:which_children(kernel_sup)),
true = is_pid(Pid).
%% Test that a request starts the timer server if it is not running.
apply_after4(Config) when is_list(Config) ->
ok = supervisor:terminate_child(kernel_sup, timer_server),
ok = supervisor:delete_child(kernel_sup, timer_server),
Msg = make_ref(),
timer:apply_after(100, erlang, send, [self(), Msg]),
ok = get_mess(500, Msg),
{timer_server, Pid, worker, [timer]} = lists:keyfind(timer_server, 1, supervisor:which_children(kernel_sup)),
true = is_pid(Pid).
%% Test that apply_after rejects invalid arguments.
apply_after_invalid_args(Config) when is_list(Config) ->
{error, badarg} = timer:apply_after(-1, foo, bar, []),
{error, badarg} = timer:apply_after(0, "foo", bar, []),
{error, badarg} = timer:apply_after(0, foo, "bar", []),
{error, badarg} = timer:apply_after(0, foo, bar, baz),
ok.
%% Test of send_after with time = 0.
send_after1(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, {instant, _}} = timer:send_after(0, Msg),
ok = get_mess(1000, Msg).
%% Test of send_after with time = 0 using a registered name.
send_after2(Config) when is_list(Config) ->
Msg = make_ref(),
Name = register_name(self()),
{ok, {instant, _}} = timer:send_after(0, Name, Msg),
ok = get_mess(1000, Msg),
unregister(Name).
%% Test of send_after with time = 0 using a registered name
%% and node.
send_after3(Config) when is_list(Config) ->
Msg = make_ref(),
Name = register_name(self()),
{ok, {instant, _}} = timer:send_after(0, {Name, node()}, Msg),
ok = get_mess(1000, Msg),
unregister(Name).
%% Test of send_after with time = 500.
send_after4(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, {send_local, _}} = timer:send_after(500, self(), Msg),
ok = get_mess(2000, Msg).
%% Test of send_after with time = 500, with receiver a registered
%% process. [OTP-2735]
send_after5(Config) when is_list(Config) ->
Msg = make_ref(),
Name = register_name(self()),
{ok, {once, _}} = timer:send_after(500, Name, Msg),
ok = get_mess(2000, Msg),
unregister(Name).
%% Test of send_after with time = 500 using a registered process
%% and node.
send_after6(Config) when is_list(Config) ->
Msg = make_ref(),
Name = register_name(self()),
{ok, {once, _}} = timer:send_after(500, {Name, node()}, Msg),
ok = get_mess(2000, Msg),
unregister(Name).
%% Test that send_after works if the destination is a registered
%% name which gets registered after the timer is started.
send_after7(Config) when is_list(Config) ->
Msg = make_ref(),
Name = make_name(),
{ok, {once, _}} = timer:send_after(500, Name, Msg),
register(Name, self()),
ok = get_mess(2000, Msg),
unregister(Name).
%% Test that send_after rejects invalid arguments.
send_after_invalid_args(Config) when is_list(Config) ->
{error, badarg} = timer:send_after(-1, test),
{error, badarg} = timer:send_after(-1, self(), test),
{error, badarg} = timer:send_after(-1, ?MODULE, test),
{error, badarg} = timer:send_after(0, "", test),
ok.
%% Test of exit_after with time = 1000.
exit_after1(Config) when is_list(Config) ->
Msg = make_ref(),
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, forever, []),
{ok, {once, _}} = timer:exit_after(1000, Pid, Msg),
ok = get_mess(5000, {'EXIT', Pid, Msg}).
%% Test of exit_after with time = 1000. The process to exit is the
%% name of a registered process. [OTP-2735]
exit_after2(Config) when is_list(Config) ->
Msg = make_ref(),
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, forever, []),
Name = register_name(Pid),
{ok, {once, _}} = timer:exit_after(1000, Name, Msg),
ok = get_mess(2000, {'EXIT', Pid, Msg}).
%% Test of exit_after for sending an exit to self.
exit_after3(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Msg = make_ref(),
Pid = spawn_link(
fun () ->
{ok, {once, _}} = timer:exit_after(1000, Msg),
forever()
end
),
ok = get_mess(2000, {'EXIT', Pid, Msg}).
%% Test that using exit_after to a non-existent
%% process does not crash the timer server.
exit_after4(Config) when is_list(Config) ->
Mon = monitor(process, timer_server),
timer:exit_after(0, make_name(), make_ref()),
receive
{'DOWN', Mon, process, _, _} ->
error(timer_server_crashed)
after 1000 ->
ok
end.
%% Test of kill_after with time = 1000.
kill_after1(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, forever, []),
{ok, {once, _}} = timer:kill_after(1000, Pid),
ok = get_mess(2000, {'EXIT', Pid, killed}).
%% Test of kill_after with time = 1000. The process to exit is the
%% name of a registered process. [OTP-2735]
kill_after2(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, forever, []),
Name = register_name(Pid),
{ok, {once, _}} = timer:kill_after(1000, Name),
ok = get_mess(2000, {'EXIT', Pid, killed}).
%% Test of kill_after for self-killing.
kill_after3(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Pid = spawn_link(
fun () ->
{ok, {once, _}} = timer:kill_after(1000),
forever()
end
),
ok = get_mess(2000, {'EXIT', Pid, killed}).
%% Test of apply_interval by sending messages. Receive
%% 3 messages, cancel the timer, and check that we do
%% not get any more messages.
apply_interval1(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:apply_interval(1000, ?MODULE, send,
[self(), Msg]),
ok = get_mess(1500, Msg, 3),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(1000, Msg).
%% Test that apply_interval rejects invalid arguments.
apply_interval_invalid_args(Config) when is_list(Config) ->
{error, badarg} = timer:apply_interval(-1, foo, bar, []),
{error, badarg} = timer:apply_interval(0, "foo", bar, []),
{error, badarg} = timer:apply_interval(0, foo, "bar", []),
{error, badarg} = timer:apply_interval(0, foo, bar, baz),
ok.
%% Test of send_interval/2. Receive 5 messages, cancel the timer, and
%% check that we do not get any more messages.
send_interval1(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:send_interval(1000, Msg),
ok = get_mess(1500, Msg, 5),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(1000, Msg). % We should receive only five
%% Test of send_interval/3. Receive 2 messages, cancel the timer, and
%% check that we do not get any more messages.
send_interval2(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:send_interval(1000, self(), Msg),
ok = get_mess(1500, Msg, 2),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(1000, Msg). % We should receive only two
%% Test of send_interval/3. Receive 2 messages, cancel the timer, and
%% check that we do not get any more messages. The receiver is the
%% name of a registered process. [OTP-2735]
send_interval3(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Msg = make_ref(),
Name = register_name(self()),
{ok, Ref} = timer:send_interval(1000, Name, Msg),
ok = get_mess(1500, Msg, 2),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(1000, Msg), % We should receive only two
unregister(Name).
%% Test of send_interval/3 using a registered name and node.
send_interval4(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Msg = make_ref(),
Name = register_name(self()),
{ok, Ref} = timer:send_interval(1000, {Name, node()}, Msg),
ok = get_mess(1500, Msg, 2),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(1000, Msg), % We should receive only two
unregister(Name).
%% Test that send interval stops sending msg when the receiving
%% process terminates.
send_interval5(Config) when is_list(Config) ->
Msg1 = make_ref(),
{ok, {interval, Ref}} = timer:send_interval(500, Msg1),
receive
Msg1 -> ok
end,
timer_server ! {'DOWN', Ref, process, self(), test},
Msg2 = make_ref(),
{ok, {send_local, _}} = timer:send_after(600, Msg2),
Msg2 = receive
TmpMsg -> TmpMsg
end.
%% Test that send_interval rejects invalid arguments.
send_interval_invalid_args(Config) when is_list(Config) ->
{error, badarg} = timer:send_interval(-1, test),
{error, badarg} = timer:send_interval(-1, self(), test),
{error, badarg} = timer:send_interval(-1, ?MODULE, test),
{error, badarg} = timer:send_interval(0, "", test),
ok.
%% Test that we can cancel an instant timer
cancel1(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:send_after(0, Msg),
{ok, cancel} = timer:cancel(Ref),
ok = get_mess(0, Msg). % We should rec 1 msg as it got sent immediately
%% Test that we can cancel a send-once timer.
cancel2(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:send_after(1000, Msg),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(2000, Msg). % We should rec 0 msgs
%% Test that we can cancel an apply-once timer.
cancel3(Config) when is_list(Config) ->
Msg = make_ref(),
{ok, Ref} = timer:apply_after(1000, erlang, send, [self(), Msg]),
{ok, cancel} = timer:cancel(Ref),
nor = get_mess(2000, Msg).
%% Test that we can cancel a send-interval timer.
cancel4(Config) when is_list(Config) ->
Msg1 = make_ref(),
{ok, Ref} = timer:send_interval(500, Msg1),
receive
Msg1 -> ok
end,
{ok, cancel} = timer:cancel(Ref),
Msg2 = make_ref(),
{ok, {send_local, _}} = timer:send_after(600, Msg2),
Msg2 = receive
TmpMsg -> TmpMsg
end.
%% Test that we can cancel an apply-interval timer.
cancel5(Config) when is_list(Config) ->
Msg1 = make_ref(),
{ok, Ref} = timer:apply_interval(500, erlang, send, [self(), Msg1]),
receive
Msg1 -> ok
end,
{ok, cancel} = timer:cancel(Ref),
Msg2 = make_ref(),
{ok, {send_local, _}} = timer:send_after(600, Msg2),
Msg2 = receive
TmpMsg -> TmpMsg
end.
%% Test that cancelling non-existent timers does not crash the
%% timer server.
cancel6(Config) when is_list(Config) ->
lists:foreach(
fun (TimerType) ->
Mon = monitor(process, timer_server),
{ok, cancel} = timer:cancel({TimerType, make_ref()}),
receive
{'DOWN', Mon, process, _, _} ->
error({timer_server_crashed, {cancel, TimerType}})
after 500 ->
ok
end,
demonitor(Mon)
end,
[once, instant, interval, send_local]
).
%% Test that cancel rejects invalid arguments.
cancel_invalid_args(Config) when is_list(Config) ->
{error, badarg} = timer:cancel(no_reference),
{error, badarg} = timer:cancel({foo, make_ref()}),
{error, badarg} = timer:cancel({once, foo}),
{error, badarg} = timer:cancel({interval, foo}),
{error, badarg} = timer:cancel({instant, foo}),
ok.
%% Test that sleep pauses the calling process for
%% at least the given time.
sleep1(Config) when is_list(Config) ->
T0 = erlang:monotonic_time(millisecond),
ok = timer:sleep(1000),
T1 = erlang:monotonic_time(millisecond),
true = T1 - T0 >= 1000,
ok.
%% Test that sleep accepts times >(2^32)-1, which is
%% the maximum time for the after clause of a receive
%% operation, at the time of this writing.
sleep2(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Pid = spawn_link(
fun () ->
{ok, {once, _}} = timer:kill_after(1000),
ok = timer:sleep(16#ffffffff+1)
end
),
ok = get_mess(2000, {'EXIT', Pid, killed}).
%% Test that unexpected calls do not crash the timer server.
unexpected1(Config) when is_list(Config) ->
Mon = monitor(process, timer_server),
try
gen_server:call(timer_server, foo, 100)
of
_ ->
error(timeout_expected)
catch
exit:{timeout, _} ->
ok
end,
receive
{'DOWN', Mon, process, _, _} ->
error(timer_server_crashed)
after 500 ->
ok
end,
demonitor(Mon).
%% Test that unexpected casts do not crash the timer server.
unexpected2(Config) when is_list(Config) ->
Mon = monitor(process, timer_server),
gen_server:cast(timer_server, foo),
receive
{'DOWN', Mon, process, _, _} ->
error(timer_server_crashed)
after 500 ->
ok
end,
demonitor(Mon).
%% Test that unexpected info messages do not crash the timer server.
unexpected3(Config) when is_list(Config) ->
Mon = monitor(process, timer_server),
timer_server ! foo,
receive
{'DOWN', Mon, process, _, _} ->
error(timer_server_crashed)
after 500 ->
ok
end,
demonitor(Mon).
%% Test that timeouts of one-shot timers the timer server does not
%% know are not executed.
nonexistent1(Config) when is_list(Config) ->
Msg = make_ref(),
timer_server ! {timeout, make_ref(), {apply_once, {erlang, send, [self(), Msg]}}},
nor = get_mess(1000, Msg).
%% Test that timeouts of interval timers the timer server does not
%% know are not executed.
nonexistent2(Config) when is_list(Config) ->
Msg = make_ref(),
timer_server ! {timeout, make_ref, {apply_interval, erlang:monotonic_time(millisecond), 1000, make_ref(), {erlang, send, [self(), Msg]}}},
nor = get_mess(1000, Msg).
%% Test sleep/1 and tc/3.
tc(Config) when is_list(Config) ->
%% This should test both sleep and tc/3
{Res1, ok} = timer:tc(timer, sleep, [500]),
ok = if
Res1 < 500*1000 -> {too_early, Res1}; % Too early
Res1 > 800*1000 -> {too_late, Res1}; % Too much time
true -> ok
end,
%% tc/2
{Res2, ok} = timer:tc(fun(T) -> ok = timer:sleep(T) end, [500]),
ok = if
Res2 < 500*1000 -> {too_early, Res2}; % Too early
Res2 > 800*1000 -> {too_late, Res2}; % Too much time
true -> ok
end,
%% tc/1
{Res3, ok} = timer:tc(fun() -> ok = timer:sleep(500) end),
ok = if
Res3 < 500*1000 -> {too_early, Res3}; % Too early
Res3 > 800*1000 -> {too_late, Res3}; % Too much time
true -> ok
end,
%% Check that timer:tc don't catch errors
ok = try timer:tc(erlang, exit, [foo])
catch exit:foo -> ok
end,
ok = try timer:tc(fun(Reason) -> 1 = Reason end, [foo])
catch error:{badmatch,_} -> ok
end,
ok = try timer:tc(fun() -> throw(foo) end)
catch foo -> ok
end,
%% Check that return values are propageted
Self = self(),
{_, Self} = timer:tc(erlang, self, []),
{_, Self} = timer:tc(fun(P) -> P end, [self()]),
{_, Self} = timer:tc(fun() -> self() end),
Sec = timer:seconds(4),
Min = timer:minutes(4),
Hour = timer:hours(4),
MyRes = 4*1000 + 4*60*1000 + 4*60*60*1000,
if MyRes == Sec + Min + Hour -> ok end,
TimerRes = timer:hms(4,4,4),
if MyRes == TimerRes -> ok end,
ok.
get_mess(Time, Mess) -> get_mess(Time, Mess, 1).
get_mess(_, _, 0) -> ok; % Received
get_mess(Time, Mess, N) ->
receive
Mess -> get_mess(Time, Mess, N-1)
after Time ->
nor % Not Received
end.
forever() ->
ok = timer:sleep(1000),
forever().
%%
%% Testing for performance (on different implementations) of timers
%%
timer_perf(Config) when is_list(Config) ->
performance(timer).
performance(Mod) ->
process_flag(trap_exit, true),
{Y,Mo,D} = date(),
{H,M,S} = time(),
io:format("Testing module '~p' Date: ~w/~w/~w ~w:~w:~w~n",
[Mod,Y,Mo,D,H,M,S]),
Result = big_test(Mod),
report_result(Result).
big_test(M) ->
Load_Pids = start_nrev(20, M), % Increase if more load wanted :)
LPids = spawn_timers(5, M, 10000, 5),
apply(M, sleep, [4000]),
MPids = spawn_timers(10, M, 1000, 6),
apply(M, sleep, [3500]),
SPids = spawn_timers(15, M, 100, 3),
Res = wait(SPids ++ MPids ++ LPids, [], 0, M),
lists:foreach(fun(Pid) -> exit(Pid, kill) end, Load_Pids),
Res.
wait([], Res, N, _) ->
{Res, N};
wait(Pids, ResList, N, M) ->
receive
{Pid, ok, Res, T} ->
wait(lists:delete(Pid, Pids), [{T, Res} | ResList], N, M);
{Pid, Error}->
ct:fail(Error),
wait(lists:delete(Pid, Pids), ResList, N+1, M);
{'EXIT', Pid, normal} ->
wait(lists:delete(Pid, Pids), ResList, N, M);
{'EXIT', Pid, Reason} ->
ct:fail({Pid,Reason})
end.
spawn_timers(0, _, _, _) ->
[];
spawn_timers(N, M, T, NumIter) ->
apply(M, sleep, [120*N]),
Pid1 = spawn_link(?MODULE, timer, [apply, M, T, self()]),
Pid2 = spawn_link(?MODULE, timer, [interval, M, T, self(), NumIter]),
[Pid1, Pid2 | spawn_timers(N-1, M, T, NumIter)].
timer(apply, Mod, T, Pid) ->
Before = system_time(),
{ok, Ref} = apply(Mod, apply_after, [T, ?MODULE, send, [self(), done]]),
receive
done ->
After = system_time(),
Pid ! {self(), ok, (After-Before) div 1000, T}
after T*3 + 300 -> % Watch dog
io:format("WARNING TIMER WATCHDOG timed out: ~w ~n", [T]),
{ok, cancel} = timer:cancel(Ref),
Pid ! {self(), watch_dog_timed_out}
end.
timer(interval, Mod, T, Pid, NumIter) ->
Before = system_time(),
{ok, Ref} = apply(Mod, apply_interval, [T, ?MODULE, send, [self(), done]]),
timer_irec(Before, T, {0, NumIter}, [], {Pid, Mod, Ref}).
timer_irec(_Start, T, {N, N}, Res, {Pid, Mod, Ref}) ->
apply(Mod, cancel, [Ref]),
Min = lists:min(Res),
Max = lists:max(Res),
Tot = lists:sum(Res),
Pid ! {self(), ok, {N, Tot, Tot div N, Min, Max}, T};
timer_irec(Start, T, {N, Max}, Res, {Pid, Mod, Ref}) ->
receive
done ->
Now = system_time(),
Elapsed = (Now - (Start + (N*T*1000))) div 1000,
timer_irec(Start, T,
{N+1, Max},
[Elapsed | Res],
{Pid, Mod, Ref})
after T*3 + 300 ->
apply(Mod, cancel, [Ref]),
io:format("WARNING: TIMER WATCHDOG timed out <Interval>~w~n",[T]),
Pid ! {self(), timer_watchdog_timed_out_in_interlval_test}
end.
%% ------------------------------------------------------- %%
%% Small last generator
start_nrev(0, _) ->
[];
start_nrev(N, M) ->
Pid = spawn_link(?MODULE, do_nrev, [N, M]),
[Pid | start_nrev(N-1, M)].
do_nrev(Sleep, Mod) ->
apply(Mod, sleep, [50 * Sleep]),
test(1000,"abcdefghijklmnopqrstuvxyz1234"),
ok.
test(0,_) ->
true;
test(N,L) ->
nrev(L),
test(N - 1, L).
nrev([]) ->
[];
nrev([H|T]) ->
append(nrev(T), [H]).
append([H|T],Z) ->
[H|append(T,Z)];
append([],X) ->
X.
system_time() ->
erlang:monotonic_time(microsecond).
%% ------------------------------------------------------- %%
report_result({Res, 0}) ->
{A_List, I_List} = split_list(Res, [], []),
A_val = calc_a_val(A_List),
I_val = calc_i_val(I_List),
print_report(A_val, I_val),
ok;
report_result({Head, N}) ->
io:format("Test Failed: Number of internal tmo ~w~n", [N]),
ct:fail({Head, N}).
split_list([], AL, IL) ->
{AL, IL};
split_list([{T, {N, Tot, A, Min, Max}} | Rest], AL, IL) ->
split_list(Rest, AL, [{T, {N, Tot, A, Min, Max}} | IL]);
split_list([Head | Rest], AL, IL) ->
split_list(Rest, [Head | AL], IL).
split([{T, Res} | R]) ->
split(R, {{T,[Res]}, {T*10,[]}, {T*100,[]}}).
split([{T, Res} | R], {{T,S}, M, L}) ->
split(R, {{T,[Res|S]}, M, L});
split([{T, Res} | R], {S, {T,M}, L}) ->
split(R, {S, {T, [Res|M]}, L});
split([{T, Res} | R], {S, M, {T,L}}) ->
split(R, {S, M, {T, [Res|L]}});
split(_Done, Vals) ->
Vals.
calc_a_val(List) ->
New = lists:sort(List),
{{T1, S}, {T2, M}, {T3, L}} = split(New),
S2 = {length(S), lists:max(S), lists:min(S),
lists:sum(S) div length(S)},
M2 = {length(M), lists:max(M), lists:min(M),
lists:sum(M) div length(M)},
L2 = {length(L), lists:max(L), lists:min(L),
lists:sum(L) div length(L)},
[{T1, S2}, {T2, M2}, {T3, L2}].
calc_i_val(List) ->
New = lists:sort(List),
{{T1, S}, {T2, M}, {T3, L}} = split(New),
S2 = get_ivals(S),
M2 = get_ivals(M),
L2 = get_ivals(L),
[{T1, S2}, {T2, M2}, {T3, L2}].
get_ivals(List) ->
Len = length(List),
Num = element(1, hd(List)), % Number of iterations
LTot = lists:map(fun(X) -> element(2, X) end, List),
LMin = lists:map(fun(X) -> element(4, X) end, List),
LMax = lists:map(fun(X) -> element(5, X) end, List),
MaxTot = lists:max(LTot),
MinTot = lists:min(LTot),
AverTot = lists:sum(LTot) div Len,
IterMax = lists:max(LMax),
IterMin = lists:min(LMin),
IterAver= AverTot div Num,
{Len, Num,
{MaxTot, MinTot, AverTot},
{IterMax, IterMin, IterAver}}.
print_report(A_L, I_L) ->
io:format("~nRESULTS from timer test~n~n",[]),
io:format("Time out times for send_after~n~n", []),
io:format("Time No of tests Max Min Average~n",[]),
print_aval(A_L),
io:format("Time out times for send_interval~n~n", []),
io:format("Time No.tests No.intvals TotMax TotMin TotAver MaxI MinI AverI~n", []),
print_ival(I_L).
print_aval([]) ->
io:format("~n~n", []);
print_aval([{T, {L, Max, Min, Aver}}|R]) ->
io:format("~5w ~8w ~6w ~6w ~8w ~n",
[T,L,Max,Min,Aver]),
print_aval(R).
print_ival([]) ->
io:format("~n", []);
print_ival([{T, {Len, Num,
{MaxT, MinT, AverT},
{MaxI, MinI, AverI}}}|R]) ->
io:format("~5w ~6w ~10w ~8w ~6w ~6w ~6w ~6w ~6w~n",
[T,Len,Num,MaxT,MinT,AverT, MaxI, MinI, AverI]),
print_ival(R).
send(Pid, Msg) ->
Pid ! Msg.
%% Create a unique name and register it to the given process.
register_name(Pid) ->
Name = make_name(),
register(Name, Pid),
Name.
%% Create a unique name.
make_name() ->
list_to_atom(ref_to_list(make_ref())).
|