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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2018. 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%
%%
-module(erl_distribution_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2]).
-export([tick/1, tick_change/1,
nodenames/1, hostnames/1,
illegal_nodenames/1, hidden_node/1,
setopts/1,
table_waste/1, net_setuptime/1,
inet_dist_options_options/1,
monitor_nodes_nodedown_reason/1,
monitor_nodes_complex_nodedown_reason/1,
monitor_nodes_node_type/1,
monitor_nodes_misc/1,
monitor_nodes_otp_6481/1,
monitor_nodes_errors/1,
monitor_nodes_combinations/1,
monitor_nodes_cleanup/1,
monitor_nodes_many/1]).
%% Performs the test at another node.
-export([get_socket_priorities/0,
tick_cli_test/1, tick_cli_test1/1,
tick_serv_test/2, tick_serv_test1/1,
run_remote_test/1,
setopts_do/2,
keep_conn/1, time_ping/1]).
-export([init_per_testcase/2, end_per_testcase/2]).
-export([start_node/2]).
-export([pinger/1]).
-define(DUMMY_NODE,dummy@test01).
%%-----------------------------------------------------------------
%% The distribution is mainly tested in the big old test_suite.
%% This test only tests the net_ticktime configuration flag.
%% Should be started in a CC view with:
%% erl -sname master -rsh ctrsh
%%-----------------------------------------------------------------
suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,4}}].
all() ->
[tick, tick_change, nodenames, hostnames, illegal_nodenames,
hidden_node, setopts,
table_waste, net_setuptime, inet_dist_options_options,
{group, monitor_nodes}].
groups() ->
[{monitor_nodes, [],
[monitor_nodes_nodedown_reason,
monitor_nodes_complex_nodedown_reason,
monitor_nodes_node_type, monitor_nodes_misc,
monitor_nodes_otp_6481, monitor_nodes_errors,
monitor_nodes_combinations, monitor_nodes_cleanup,
monitor_nodes_many]}].
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(TC, Config) when TC == hostnames;
TC == nodenames ->
file:make_dir("hostnames_nodedir"),
file:write_file("hostnames_nodedir/ignore_core_files",""),
Config;
init_per_testcase(Func, Config) when is_atom(Func), is_list(Config) ->
Config.
end_per_testcase(_Func, _Config) ->
ok.
tick(Config) when is_list(Config) ->
PaDir = filename:dirname(code:which(erl_distribution_SUITE)),
%% First check that the normal case is OK!
{ok, Node} = start_node(dist_test, "-pa " ++ PaDir),
rpc:call(Node, erl_distribution_SUITE, tick_cli_test, [node()]),
erlang:monitor_node(Node, true),
receive
{nodedown, Node} ->
ct:fail("nodedown from other node")
after 30000 ->
erlang:monitor_node(Node, false),
stop_node(Node)
end,
%% Now, set the net_ticktime for the other node to 12 secs.
%% After the sleep(2sec) and cast the other node shall destroy
%% the connection as it has not received anything on the connection.
%% The nodedown message should arrive within 8 < T < 16 secs.
%% We must have two slave nodes as the slave mechanism otherwise
%% halts the client node after tick timeout (the connection is down
%% and the slave node decides to halt !!
%% Set the ticktime on the server node to 100 secs so the server
%% node doesn't tick the client node within the interval ...
{ok, ServNode} = start_node(dist_test_server,
"-kernel net_ticktime 100 "
"-pa " ++ PaDir),
rpc:call(ServNode, erl_distribution_SUITE, tick_serv_test, [Node, node()]),
{ok, _} = start_node(dist_test,
"-kernel net_ticktime 12 "
"-pa " ++ PaDir),
rpc:call(Node, erl_distribution_SUITE, tick_cli_test, [ServNode]),
spawn_link(erl_distribution_SUITE, keep_conn, [Node]),
{tick_serv, ServNode} ! {i_want_the_result, self()},
monitor_node(ServNode, true),
monitor_node(Node, true),
receive
{tick_test, T} when is_integer(T) ->
stop_node(ServNode),
stop_node(Node),
T;
{tick_test, Error} ->
stop_node(ServNode),
stop_node(Node),
ct:fail(Error);
{nodedown, Node} ->
stop_node(ServNode),
ct:fail("client node died");
{nodedown, ServNode} ->
stop_node(Node),
ct:fail("server node died")
end,
ok.
%% Checks that pinging nonexistyent nodes does not waste space in distribution table.
table_waste(Config) when is_list(Config) ->
{ok, HName} = inet:gethostname(),
F = fun(0,_F) -> [];
(N,F) ->
Name = list_to_atom("erl_distribution_"++integer_to_list(N)++
"@"++HName),
pang = net_adm:ping(Name),
F(N-1,F)
end,
F(256,F),
{ok, N} = start_node(erl_distribution_300,""),
stop_node(N),
ok.
%% Test that starting nodes with different legal name part works, and that illegal
%% ones are filtered
nodenames(Config) when is_list(Config) ->
legal("a1@b"),
legal("a-1@b"),
legal("a_1@b"),
illegal("cdé@a"),
illegal("te欢st@a").
%% Test that starting nodes with different legal host part works, and that illegal
%% ones are filtered
hostnames(Config) when is_list(Config) ->
Host = gethostname(),
legal([$a,$@|atom_to_list(Host)]),
legal("1@b1"),
legal("b@b1-c"),
legal("c@b1_c"),
legal("d@b1#c"),
legal("f@::1"),
legal("g@1:bc3:4e3f:f20:0:1"),
case file:native_name_encoding() of
latin1 -> ignore;
_ -> legal("e@b1é")
end,
long_hostnames(net_kernel:longnames()),
illegal("h@testالع"),
illegal("i@языtest"),
illegal("j@te欢st").
long_hostnames(true) ->
legal("[email protected]"),
legal("[email protected]"),
legal("[email protected]_c.d"),
legal("[email protected]"),
legal("[email protected]");
long_hostnames(false) ->
illegal("[email protected]").
legal(Name) ->
case test_node(Name) of
started ->
ok;
not_started ->
ct:fail("no ~p node started", [Name])
end.
illegal(Name) ->
case test_node(Name, true) of
not_started ->
ok;
started ->
ct:fail("~p node started with illegal name", [Name])
end.
test_node(Name) ->
test_node(Name, false).
test_node(Name, Illigal) ->
ProgName = ct:get_progname(),
Command = ProgName ++ " -noinput " ++ long_or_short() ++ Name ++
" -eval \"net_adm:ping('" ++ atom_to_list(node()) ++ "')\"" ++
case Illigal of
true ->
" -eval \"timer:sleep(10000),init:stop().\"";
false ->
""
end,
net_kernel:monitor_nodes(true),
BinCommand = unicode:characters_to_binary(Command, utf8),
Prt = open_port({spawn, BinCommand}, [stream,{cd,"hostnames_nodedir"}]),
Node = list_to_atom(Name),
receive
{nodeup, Node} ->
net_kernel:monitor_nodes(false),
slave:stop(Node),
started
after 5000 ->
net_kernel:monitor_nodes(false),
not_started
end.
long_or_short() ->
case net_kernel:longnames() of
true -> " -name ";
false -> " -sname "
end.
% get the localhost's name, depending on the using name policy
gethostname() ->
Hostname = case net_kernel:longnames() of
true->
net_adm:localhost();
_->
{ok, Name}=inet:gethostname(),
Name
end,
list_to_atom(Hostname).
%% Test that pinging an illegal nodename does not kill the node.
illegal_nodenames(Config) when is_list(Config) ->
PaDir = filename:dirname(code:which(erl_distribution_SUITE)),
{ok, Node}=start_node(illegal_nodenames, "-pa " ++ PaDir),
monitor_node(Node, true),
RPid=rpc:call(Node, erlang, spawn,
[?MODULE, pinger, [self()]]),
receive
{RPid, pinged} ->
ok;
{nodedown, Node} ->
ct:fail("Remote node died.")
end,
stop_node(Node),
ok.
pinger(Starter) ->
io:format("Starter:~p~n",[Starter]),
net_adm:ping(a@b@c),
Starter ! {self(), pinged},
ok.
%% Test that you can set the net_setuptime properly.
net_setuptime(Config) when is_list(Config) ->
%% In this test case, we reluctantly accept shorter times than the given
%% setup time, because the connection attempt can end in a
%% "Host unreachable" error before the timeout fires.
Res0 = do_test_setuptime("2"),
io:format("Res0 = ~p", [Res0]),
true = (Res0 =< 4000),
Res1 = do_test_setuptime("0.3"),
io:format("Res1 = ~p", [Res1]),
true = (Res1 =< 500),
ok.
do_test_setuptime(Setuptime) when is_list(Setuptime) ->
PaDir = filename:dirname(code:which(?MODULE)),
{ok, Node} = start_node(dist_setuptime_test, "-pa " ++ PaDir ++
" -kernel net_setuptime " ++ Setuptime),
Res = rpc:call(Node,?MODULE,time_ping,[?DUMMY_NODE]),
stop_node(Node),
Res.
time_ping(Node) ->
T0 = erlang:monotonic_time(),
pang = net_adm:ping(Node),
T1 = erlang:monotonic_time(),
erlang:convert_time_unit(T1 - T0, native, millisecond).
%% Keep the connection with the client node up.
%% This is necessary as the client node runs with much shorter
%% tick time !!
keep_conn(Node) ->
sleep(1),
rpc:cast(Node, erlang, time, []),
keep_conn(Node).
tick_serv_test(Node, MasterNode) ->
spawn(erl_distribution_SUITE, keep_conn, [MasterNode]),
spawn(erl_distribution_SUITE, tick_serv_test1, [Node]).
tick_serv_test1(Node) ->
register(tick_serv, self()),
TestServer = receive {i_want_the_result, TS} -> TS end,
monitor_node(Node, true),
receive
{nodedown, Node} ->
net_adm:ping(Node), %% Set up the connection again !!
{tick_test, Node} ! {whats_the_result, self()},
receive
{tick_test, Res} ->
TestServer ! {tick_test, Res}
end
end.
tick_cli_test(Node) ->
spawn(erl_distribution_SUITE, tick_cli_test1, [Node]).
tick_cli_test1(Node) ->
register(tick_test, self()),
erlang:monitor_node(Node, true),
sleep(2),
rpc:call(Node, erlang, time, []), %% simulate action on the connection
T1 = erlang:monotonic_time(),
receive
{nodedown, Node} ->
T2 = erlang:monotonic_time(),
receive
{whats_the_result, From} ->
Diff = erlang:convert_time_unit(T2-T1, native,
millisecond),
case Diff of
T when T > 8000, T < 16000 ->
From ! {tick_test, T};
T ->
From ! {tick_test,
{"T not in interval 8000 < T < 16000",
T}}
end
end
end.
setopts(Config) when is_list(Config) ->
register(setopts_regname, self()),
[N1,N2,N3,N4] = get_nodenames(4, setopts),
{_N1F,Port1} = start_node_unconnected(N1, ?MODULE, run_remote_test,
["setopts_do", atom_to_list(node()), "1", "ping"]),
0 = wait_for_port_exit(Port1),
{_N2F,Port2} = start_node_unconnected(N2, ?MODULE, run_remote_test,
["setopts_do", atom_to_list(node()), "2", "ping"]),
0 = wait_for_port_exit(Port2),
{ok, LSock} = gen_tcp:listen(0, [{packet,2}, {active,false}]),
{ok, LTcpPort} = inet:port(LSock),
{N3F,Port3} = start_node_unconnected(N3, ?MODULE, run_remote_test,
["setopts_do", atom_to_list(node()),
"1", integer_to_list(LTcpPort)]),
wait_and_connect(LSock, N3F, Port3),
0 = wait_for_port_exit(Port3),
{N4F,Port4} = start_node_unconnected(N4, ?MODULE, run_remote_test,
["setopts_do", atom_to_list(node()),
"2", integer_to_list(LTcpPort)]),
wait_and_connect(LSock, N4F, Port4),
0 = wait_for_port_exit(Port4),
ok.
wait_and_connect(LSock, NodeName, NodePort) ->
{ok, Sock} = gen_tcp:accept(LSock),
{ok, "Connect please"} = gen_tcp:recv(Sock, 0),
flush_from_port(NodePort),
pong = net_adm:ping(NodeName),
gen_tcp:send(Sock, "Connect done"),
gen_tcp:close(Sock).
flush_from_port(Port) ->
flush_from_port(Port, 10).
flush_from_port(Port, Timeout) ->
receive
{Port,{data,String}} ->
io:format("~p: ~s\n", [Port, String]),
flush_from_port(Port, Timeout)
after Timeout ->
timeout
end.
wait_for_port_exit(Port) ->
case (receive M -> M end) of
{Port,{exit_status,Status}} ->
Status;
{Port,{data,String}} ->
io:format("~p: ~s\n", [Port, String]),
wait_for_port_exit(Port)
end.
run_remote_test([FuncStr, TestNodeStr | Args]) ->
Status = try
io:format("Node ~p started~n", [node()]),
TestNode = list_to_atom(TestNodeStr),
io:format("Node ~p spawning function ~p~n", [node(), FuncStr]),
{Pid,Ref} = spawn_monitor(?MODULE, list_to_atom(FuncStr), [TestNode, Args]),
io:format("Node ~p waiting for function ~p~n", [node(), FuncStr]),
receive
{'DOWN', Ref, process, Pid, normal} ->
0;
Other ->
io:format("Node ~p got unexpected msg: ~p\n",[node(), Other]),
1
end
catch
C:E:S ->
io:format("Node ~p got EXCEPTION ~p:~p\nat ~p\n",
[node(), C, E, S]),
2
end,
io:format("Node ~p doing halt(~p).\n",[node(), Status]),
erlang:halt(Status).
% Do the actual test on the remote node
setopts_do(TestNode, [OptNr, ConnectData]) ->
[] = nodes(),
{Opt, Val} = opt_from_nr(OptNr),
ok = net_kernel:setopts(new, [{Opt, Val}]),
[] = nodes(),
{error, noconnection} = net_kernel:getopts(TestNode, [Opt]),
case ConnectData of
"ping" -> % We connect
net_adm:ping(TestNode);
TcpPort -> % Other connect
{ok, Sock} = gen_tcp:connect("localhost", list_to_integer(TcpPort),
[{active,false},{packet,2}]),
ok = gen_tcp:send(Sock, "Connect please"),
{ok, "Connect done"} = gen_tcp:recv(Sock, 0),
gen_tcp:close(Sock)
end,
[TestNode] = nodes(),
{ok, [{Opt,Val}]} = net_kernel:getopts(TestNode, [Opt]),
{error, noconnection} = net_kernel:getopts('pixie@fairyland', [Opt]),
NewVal = change_val(Val),
ok = net_kernel:setopts(TestNode, [{Opt, NewVal}]),
{ok, [{Opt,NewVal}]} = net_kernel:getopts(TestNode, [Opt]),
ok = net_kernel:setopts(TestNode, [{Opt, Val}]),
{ok, [{Opt,Val}]} = net_kernel:getopts(TestNode, [Opt]),
ok.
opt_from_nr("1") -> {nodelay, true};
opt_from_nr("2") -> {nodelay, false}.
change_val(true) -> false;
change_val(false) -> true.
start_node_unconnected(Name, Mod, Func, Args) ->
FullName = full_node_name(Name),
CmdLine = mk_node_cmdline(Name,Mod,Func,Args),
io:format("Starting node ~p: ~s~n", [FullName, CmdLine]),
case open_port({spawn, CmdLine}, [exit_status]) of
Port when is_port(Port) ->
{FullName, Port};
Error ->
exit({failed_to_start_node, FullName, Error})
end.
full_node_name(PreName) ->
HostSuffix = lists:dropwhile(fun ($@) -> false; (_) -> true end,
atom_to_list(node())),
list_to_atom(atom_to_list(PreName) ++ HostSuffix).
mk_node_cmdline(Name,Mod,Func,Args) ->
Static = "-noinput",
Pa = filename:dirname(code:which(?MODULE)),
Prog = case catch init:get_argument(progname) of
{ok,[[P]]} -> P;
_ -> exit(no_progname_argument_found)
end,
NameSw = case net_kernel:longnames() of
false -> "-sname ";
true -> "-name ";
_ -> exit(not_distributed_node)
end,
{ok, Pwd} = file:get_cwd(),
NameStr = atom_to_list(Name),
Prog ++ " "
++ Static ++ " "
++ NameSw ++ " " ++ NameStr
++ " -pa " ++ Pa
++ " -env ERL_CRASH_DUMP " ++ Pwd ++ "/erl_crash_dump." ++ NameStr
++ " -setcookie " ++ atom_to_list(erlang:get_cookie())
++ " -run " ++ atom_to_list(Mod) ++ " " ++ atom_to_list(Func)
++ " " ++ string:join(Args, " ").
%% OTP-4255.
tick_change(Config) when is_list(Config) ->
PaDir = filename:dirname(code:which(?MODULE)),
[BN, CN] = get_nodenames(2, tick_change),
DefaultTT = net_kernel:get_net_ticktime(),
unchanged = net_kernel:set_net_ticktime(DefaultTT, 60),
case DefaultTT of
I when is_integer(I) -> ok;
_ -> ct:fail(DefaultTT)
end,
%% In case other nodes are connected
case nodes(connected) of
[] -> net_kernel:set_net_ticktime(10, 0);
_ -> rpc:multicall(nodes([this, connected]), net_kernel,
set_net_ticktime, [10, 5])
end,
wait_until(fun () -> 10 == net_kernel:get_net_ticktime() end),
{ok, B} = start_node(BN, "-kernel net_ticktime 10 -pa " ++ PaDir),
{ok, C} = start_node(CN, "-kernel net_ticktime 10 -hidden -pa "
++ PaDir),
OTE = process_flag(trap_exit, true),
case catch begin
run_tick_change_test(B, C, 10, 1, PaDir),
run_tick_change_test(B, C, 1, 10, PaDir)
end of
{'EXIT', Reason} ->
stop_node(B),
stop_node(C),
%% In case other nodes are connected
case nodes(connected) of
[] -> net_kernel:set_net_ticktime(DefaultTT, 0);
_ -> rpc:multicall(nodes([this, connected]), net_kernel,
set_net_ticktime, [DefaultTT, 10])
end,
wait_until(fun () ->
DefaultTT == net_kernel:get_net_ticktime()
end),
process_flag(trap_exit, OTE),
ct:fail(Reason);
_ ->
ok
end,
process_flag(trap_exit, OTE),
stop_node(B),
stop_node(C),
%% In case other nodes are connected
case nodes(connected) of
[] -> net_kernel:set_net_ticktime(DefaultTT, 0);
_ -> rpc:multicall(nodes([this, connected]), net_kernel,
set_net_ticktime, [DefaultTT, 5])
end,
wait_until(fun () -> DefaultTT == net_kernel:get_net_ticktime() end),
ok.
wait_for_nodedowns(Tester, Ref) ->
receive
{nodedown, Node} ->
io:format("~p~n", [{node(), {nodedown, Node}}]),
Tester ! {Ref, {node(), {nodedown, Node}}}
end,
wait_for_nodedowns(Tester, Ref).
run_tick_change_test(B, C, PrevTT, TT, PaDir) ->
[DN, EN] = get_nodenames(2, tick_change),
Tester = self(),
Ref = make_ref(),
MonitorNodes = fun (Nodes) ->
lists:foreach(
fun (N) ->
monitor_node(N,true)
end,
Nodes),
wait_for_nodedowns(Tester, Ref)
end,
{ok, D} = start_node(DN, "-kernel net_ticktime "
++ integer_to_list(PrevTT) ++ " -pa " ++ PaDir),
NMA = spawn_link(fun () -> MonitorNodes([B, C, D]) end),
NMB = spawn_link(B, fun () -> MonitorNodes([node(), C, D]) end),
NMC = spawn_link(C, fun () -> MonitorNodes([node(), B, D]) end),
MaxTT = case PrevTT > TT of
true -> PrevTT;
false -> TT
end,
CheckResult = make_ref(),
spawn_link(fun () ->
receive
after (25 + MaxTT)*1000 ->
Tester ! CheckResult
end
end),
%% In case other nodes than these are connected
case nodes(connected) -- [B, C, D] of
[] -> ok;
OtherNodes -> rpc:multicall(OtherNodes, net_kernel,
set_net_ticktime, [TT, 20])
end,
change_initiated = net_kernel:set_net_ticktime(TT,20),
{ongoing_change_to,_} = net_kernel:set_net_ticktime(TT,20),
sleep(3),
change_initiated = rpc:call(B,net_kernel,set_net_ticktime,[TT,15]),
sleep(7),
change_initiated = rpc:call(C,net_kernel,set_net_ticktime,[TT,10]),
{ok, E} = start_node(EN, "-kernel net_ticktime "
++ integer_to_list(TT) ++ " -pa " ++ PaDir),
NME = spawn_link(E, fun () -> MonitorNodes([node(), B, C, D]) end),
NMA2 = spawn_link(fun () -> MonitorNodes([E]) end),
NMB2 = spawn_link(B, fun () -> MonitorNodes([E]) end),
NMC2 = spawn_link(C, fun () -> MonitorNodes([E]) end),
receive CheckResult -> ok end,
unlink(NMA), exit(NMA, kill),
unlink(NMB), exit(NMB, kill),
unlink(NMC), exit(NMC, kill),
unlink(NME), exit(NME, kill),
unlink(NMA2), exit(NMA2, kill),
unlink(NMB2), exit(NMB2, kill),
unlink(NMC2), exit(NMC2, kill),
%% The node not changing ticktime should have been disconnected from the
%% other nodes
receive {Ref, {Node, {nodedown, D}}} when Node == node() -> ok
after 0 -> exit({?LINE, no_nodedown})
end,
receive {Ref, {B, {nodedown, D}}} -> ok
after 0 -> exit({?LINE, no_nodedown})
end,
receive {Ref, {C, {nodedown, D}}} -> ok
after 0 -> exit({?LINE, no_nodedown})
end,
receive {Ref, {E, {nodedown, D}}} -> ok
after 0 -> exit({?LINE, no_nodedown})
end,
%% No other connections should have been broken
receive
{Ref, Reason} ->
stop_node(E),
exit({?LINE, Reason});
{'EXIT', Pid, Reason} when Pid == NMA;
Pid == NMB;
Pid == NMC;
Pid == NME;
Pid == NMA2;
Pid == NMB2;
Pid == NMC2 ->
stop_node(E),
exit({?LINE, {node(Pid), Reason}})
after 0 ->
TT = net_kernel:get_net_ticktime(),
TT = rpc:call(B, net_kernel, get_net_ticktime, []),
TT = rpc:call(C, net_kernel, get_net_ticktime, []),
TT = rpc:call(E, net_kernel, get_net_ticktime, []),
stop_node(E),
ok
end.
%%
%% Basic tests of hidden node.
%%
%% Basic test of hidden node.
hidden_node(Config) when is_list(Config) ->
PaDir = filename:dirname(code:which(?MODULE)),
VArgs = "-pa " ++ PaDir,
HArgs = "-hidden -pa " ++ PaDir,
{ok, V} = start_node(visible_node, VArgs),
VMN = start_monitor_nodes_proc(V),
{ok, H} = start_node(hidden_node, HArgs),
%% Connect visible_node -> hidden_node
connect_nodes(V, H),
test_nodes(V, H),
stop_node(H),
sleep(5),
check_monitor_nodes_res(VMN, H),
stop_node(V),
{ok, H} = start_node(hidden_node, HArgs),
HMN = start_monitor_nodes_proc(H),
{ok, V} = start_node(visible_node, VArgs),
%% Connect hidden_node -> visible_node
connect_nodes(H, V),
test_nodes(V, H),
stop_node(V),
sleep(5),
check_monitor_nodes_res(HMN, V),
stop_node(H),
ok.
connect_nodes(A, B) ->
%% Check that they haven't already connected.
false = lists:member(A, rpc:call(B, erlang, nodes, [connected])),
false = lists:member(B, rpc:call(A, erlang, nodes, [connected])),
%% Connect them.
pong = rpc:call(A, net_adm, ping, [B]).
test_nodes(V, H) ->
%% No nodes should be visible on hidden_node
[] = rpc:call(H, erlang, nodes, []),
%% visible_node should be hidden on hidden_node
true = lists:member(V, rpc:call(H, erlang, nodes, [hidden])),
%% hidden_node node shouldn't be visible on visible_node
false = lists:member(H, rpc:call(V, erlang, nodes, [])),
%% hidden_node should be hidden on visible_node
true = lists:member(H, rpc:call(V, erlang, nodes, [hidden])).
mn_loop(MNs) ->
receive
{nodeup, N} ->
mn_loop([{nodeup, N}|MNs]);
{nodedown, N} ->
mn_loop([{nodedown, N}|MNs]);
{monitor_nodes_result, Ref, From} ->
From ! {Ref, MNs};
_ ->
mn_loop(MNs)
end.
start_monitor_nodes_proc(Node) ->
Ref = make_ref(),
Starter = self(),
Pid = spawn(Node,
fun() ->
net_kernel:monitor_nodes(true),
Starter ! Ref,
mn_loop([])
end),
receive
Ref ->
ok
end,
Pid.
check_monitor_nodes_res(Pid, Node) ->
Ref = make_ref(),
Pid ! {monitor_nodes_result, Ref, self()},
receive
{Ref, MNs} ->
false = lists:keysearch(Node, 2, MNs)
end.
%% Check the kernel inet_dist_{listen,connect}_options options.
inet_dist_options_options(Config) when is_list(Config) ->
Prio = 1,
case gen_udp:open(0, [{priority,Prio}]) of
{ok,Socket} ->
case inet:getopts(Socket, [priority]) of
{ok,[{priority,Prio}]} ->
ok = gen_udp:close(Socket),
do_inet_dist_options_options(Prio);
_ ->
ok = gen_udp:close(Socket),
{skip,
"Can not set priority "++integer_to_list(Prio)++
" on socket"}
end;
{error,_} ->
{skip, "Can not set priority on socket"}
end.
do_inet_dist_options_options(Prio) ->
PriorityString0 = "[{priority,"++integer_to_list(Prio)++"}]",
PriorityString =
case os:cmd("echo [{a,1}]") of
"[{a,1}]"++_ ->
PriorityString0;
_ ->
%% Some shells need quoting of [{}]
"'"++PriorityString0++"'"
end,
InetDistOptions =
"-hidden "
"-kernel inet_dist_connect_options "++PriorityString++" "
"-kernel inet_dist_listen_options "++PriorityString,
{ok,Node1} =
start_node(inet_dist_options_1, InetDistOptions),
{ok,Node2} =
start_node(inet_dist_options_2, InetDistOptions),
%%
pong =
rpc:call(Node1, net_adm, ping, [Node2]),
PrioritiesNode1 =
rpc:call(Node1, ?MODULE, get_socket_priorities, []),
PrioritiesNode2 =
rpc:call(Node2, ?MODULE, get_socket_priorities, []),
io:format("PrioritiesNode1 = ~p", [PrioritiesNode1]),
io:format("PrioritiesNode2 = ~p", [PrioritiesNode2]),
Elevated = [P || P <- PrioritiesNode1, P =:= Prio],
Elevated = [P || P <- PrioritiesNode2, P =:= Prio],
[_|_] = Elevated,
%%
stop_node(Node2),
stop_node(Node1),
ok.
get_socket_priorities() ->
[Priority ||
{ok,[{priority,Priority}]} <-
[inet:getopts(Port, [priority]) ||
Port <- erlang:ports(),
element(2, erlang:port_info(Port, name)) =:= "tcp_inet"]].
%%
%% Testcase:
%% monitor_nodes_nodedown_reason
%%
monitor_nodes_nodedown_reason(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
ok = net_kernel:monitor_nodes(true),
ok = net_kernel:monitor_nodes(true, [nodedown_reason]),
Names = get_numbered_nodenames(5, node),
[NN1, NN2, NN3, NN4, NN5] = Names,
{ok, N1} = start_node(NN1),
{ok, N2} = start_node(NN2),
{ok, N3} = start_node(NN3),
{ok, N4} = start_node(NN4, "-hidden"),
receive {nodeup, N1} -> ok end,
receive {nodeup, N2} -> ok end,
receive {nodeup, N3} -> ok end,
receive {nodeup, N1, []} -> ok end,
receive {nodeup, N2, []} -> ok end,
receive {nodeup, N3, []} -> ok end,
stop_node(N1),
stop_node(N4),
true = net_kernel:disconnect(N2),
TickTime = net_kernel:get_net_ticktime(),
SleepTime = TickTime + (TickTime div 2),
spawn(N3, fun () ->
block_emu(SleepTime*1000),
halt()
end),
receive {nodedown, N1} -> ok end,
receive {nodedown, N2} -> ok end,
receive {nodedown, N3} -> ok end,
receive {nodedown, N1, [{nodedown_reason, R1}]} -> connection_closed = R1 end,
receive {nodedown, N2, [{nodedown_reason, R2}]} -> disconnect = R2 end,
receive {nodedown, N3, [{nodedown_reason, R3}]} -> net_tick_timeout = R3 end,
ok = net_kernel:monitor_nodes(false, [nodedown_reason]),
{ok, N5} = start_node(NN5),
stop_node(N5),
receive {nodeup, N5} -> ok end,
receive {nodedown, N5} -> ok end,
print_my_messages(),
ok = check_no_nodedown_nodeup(1000),
ok = net_kernel:monitor_nodes(false),
MonNodeState = monitor_node_state(),
ok.
monitor_nodes_complex_nodedown_reason(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
Me = self(),
ok = net_kernel:monitor_nodes(true, [nodedown_reason]),
[Name] = get_nodenames(1, monitor_nodes_complex_nodedown_reason),
{ok, Node} = start_node(Name, ""),
Pid = spawn(Node,
fun() ->
Me ! {stuff,
self(),
[make_ref(),
{processes(), erlang:ports()}]}
end),
receive {nodeup, Node, []} -> ok end,
{ok, NodeInfo} = net_kernel:node_info(Node),
{value,{owner, Owner}} = lists:keysearch(owner, 1, NodeInfo),
ComplexTerm = receive {stuff, Pid, _} = Msg ->
{Msg, term_to_binary(Msg)}
end,
exit(Owner, ComplexTerm),
receive
{nodedown, Node, [{nodedown_reason, NodeDownReason}]} ->
ok
end,
%% If the complex nodedown_reason messed something up garbage collections
%% are likely to dump core
garbage_collect(),
garbage_collect(),
garbage_collect(),
ComplexTerm = NodeDownReason,
ok = net_kernel:monitor_nodes(false, [nodedown_reason]),
no_msgs(),
MonNodeState = monitor_node_state(),
ok.
%%
%% Testcase:
%% monitor_nodes_node_type
%%
monitor_nodes_node_type(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
ok = net_kernel:monitor_nodes(true),
ok = net_kernel:monitor_nodes(true, [{node_type, all}]),
Names = get_numbered_nodenames(9, node),
[NN1, NN2, NN3, NN4, NN5, NN6, NN7, NN8, NN9] = Names,
{ok, N1} = start_node(NN1),
{ok, N2} = start_node(NN2),
{ok, N3} = start_node(NN3, "-hidden"),
{ok, N4} = start_node(NN4, "-hidden"),
receive {nodeup, N1} -> ok end,
receive {nodeup, N2} -> ok end,
receive {nodeup, N1, [{node_type, visible}]} -> ok end,
receive {nodeup, N2, [{node_type, visible}]} -> ok end,
receive {nodeup, N3, [{node_type, hidden}]} -> ok end,
receive {nodeup, N4, [{node_type, hidden}]} -> ok end,
stop_node(N1),
stop_node(N2),
stop_node(N3),
stop_node(N4),
receive {nodedown, N1} -> ok end,
receive {nodedown, N2} -> ok end,
receive {nodedown, N1, [{node_type, visible}]} -> ok end,
receive {nodedown, N2, [{node_type, visible}]} -> ok end,
receive {nodedown, N3, [{node_type, hidden}]} -> ok end,
receive {nodedown, N4, [{node_type, hidden}]} -> ok end,
ok = net_kernel:monitor_nodes(false, [{node_type, all}]),
{ok, N5} = start_node(NN5),
receive {nodeup, N5} -> ok end,
stop_node(N5),
receive {nodedown, N5} -> ok end,
ok = net_kernel:monitor_nodes(true, [{node_type, hidden}]),
{ok, N6} = start_node(NN6),
{ok, N7} = start_node(NN7, "-hidden"),
receive {nodeup, N6} -> ok end,
receive {nodeup, N7, [{node_type, hidden}]} -> ok end,
stop_node(N6),
stop_node(N7),
receive {nodedown, N6} -> ok end,
receive {nodedown, N7, [{node_type, hidden}]} -> ok end,
ok = net_kernel:monitor_nodes(true, [{node_type, visible}]),
ok = net_kernel:monitor_nodes(false, [{node_type, hidden}]),
ok = net_kernel:monitor_nodes(false),
{ok, N8} = start_node(NN8),
{ok, N9} = start_node(NN9, "-hidden"),
receive {nodeup, N8, [{node_type, visible}]} -> ok end,
stop_node(N8),
stop_node(N9),
receive {nodedown, N8, [{node_type, visible}]} -> ok end,
print_my_messages(),
ok = check_no_nodedown_nodeup(1000),
ok = net_kernel:monitor_nodes(false, [{node_type, visible}]),
MonNodeState = monitor_node_state(),
ok.
%%
%% Testcase:
%% monitor_nodes
%%
monitor_nodes_misc(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
ok = net_kernel:monitor_nodes(true),
ok = net_kernel:monitor_nodes(true, [{node_type, all}, nodedown_reason]),
ok = net_kernel:monitor_nodes(true, [nodedown_reason, {node_type, all}]),
Names = get_numbered_nodenames(3, node),
[NN1, NN2, NN3] = Names,
{ok, N1} = start_node(NN1),
{ok, N2} = start_node(NN2, "-hidden"),
receive {nodeup, N1} -> ok end,
receive {nodeup, N1, [{node_type, visible}]} -> ok end,
receive {nodeup, N1, [{node_type, visible}]} -> ok end,
receive {nodeup, N2, [{node_type, hidden}]} -> ok end,
receive {nodeup, N2, [{node_type, hidden}]} -> ok end,
stop_node(N1),
stop_node(N2),
VisbleDownInfo = lists:sort([{node_type, visible},
{nodedown_reason, connection_closed}]),
HiddenDownInfo = lists:sort([{node_type, hidden},
{nodedown_reason, connection_closed}]),
receive {nodedown, N1} -> ok end,
receive {nodedown, N1, Info1A} -> VisbleDownInfo = lists:sort(Info1A) end,
receive {nodedown, N1, Info1B} -> VisbleDownInfo = lists:sort(Info1B) end,
receive {nodedown, N2, Info2A} -> HiddenDownInfo = lists:sort(Info2A) end,
receive {nodedown, N2, Info2B} -> HiddenDownInfo = lists:sort(Info2B) end,
ok = net_kernel:monitor_nodes(false, [{node_type, all}, nodedown_reason]),
{ok, N3} = start_node(NN3),
receive {nodeup, N3} -> ok end,
stop_node(N3),
receive {nodedown, N3} -> ok end,
print_my_messages(),
ok = check_no_nodedown_nodeup(1000),
ok = net_kernel:monitor_nodes(false),
MonNodeState = monitor_node_state(),
ok.
%% Tests that {nodeup, Node} messages are received before
%% messages from Node and that {nodedown, Node} messages are
%% received after messages from Node.
monitor_nodes_otp_6481(Config) when is_list(Config) ->
io:format("Testing nodedown...~n"),
monitor_nodes_otp_6481_test(Config, nodedown),
io:format("ok~n"),
io:format("Testing nodeup...~n"),
monitor_nodes_otp_6481_test(Config, nodeup),
io:format("ok~n"),
ok.
monitor_nodes_otp_6481_test(Config, TestType) when is_list(Config) ->
MonNodeState = monitor_node_state(),
NodeMsg = make_ref(),
Me = self(),
[Name] = get_nodenames(1, monitor_nodes_otp_6481),
case TestType of
nodedown -> ok = net_kernel:monitor_nodes(true);
nodeup -> ok
end,
Seq = lists:seq(1,10000),
MN = spawn_link(
fun () ->
lists:foreach(
fun (_) ->
ok = net_kernel:monitor_nodes(true)
end,
Seq),
Me ! {mon_set, self()},
receive after infinity -> ok end
end),
receive {mon_set, MN} -> ok end,
case TestType of
nodedown -> ok;
nodeup -> ok = net_kernel:monitor_nodes(true)
end,
%% Whitebox:
%% nodedown test: Since this process was the first one monitoring
%% nodes this process will be the first one notified
%% on nodedown.
%% nodeup test: Since this process was the last one monitoring
%% nodes this process will be the last one notified
%% on nodeup
%% Verify the monitor_nodes order expected
TestMonNodeState = monitor_node_state(),
%% io:format("~p~n", [TestMonNodeState]),
TestMonNodeState =
case TestType of
nodedown -> [];
nodeup -> [{self(), []}]
end
++ lists:map(fun (_) -> {MN, []} end, Seq)
++ case TestType of
nodedown -> [{self(), []}];
nodeup -> []
end
++ MonNodeState,
{ok, Node} = start_node(Name, "", this),
receive {nodeup, Node} -> ok end,
RemotePid = spawn(Node,
fun () ->
receive after 1500 -> ok end,
%% infinite loop of msgs
%% we want an endless stream of messages and the kill
%% the node mercilessly.
%% We then want to ensure that the nodedown message arrives
%% last ... without garbage after it.
_ = spawn(fun() -> node_loop_send(Me, NodeMsg, 1) end),
receive {Me, kill_it} -> ok end,
halt()
end),
net_kernel:disconnect(Node),
receive {nodedown, Node} -> ok end,
%% Verify that '{nodeup, Node}' comes before '{NodeMsg, 1}' (the message
%% bringing up the connection).
{nodeup, Node} = receive Msg1 -> Msg1 end,
{NodeMsg, N} = receive Msg2 -> Msg2 end,
%% msg stream has begun, kill the node
RemotePid ! {self(), kill_it},
%% Verify that '{nodedown, Node}' comes after the last '{NodeMsg, N}'
%% message.
{nodedown, Node} = flush_node_msgs(NodeMsg, N+1),
no_msgs(500),
Mon = erlang:monitor(process, MN),
unlink(MN),
exit(MN, bang),
receive {'DOWN', Mon, process, MN, bang} -> ok end,
ok = net_kernel:monitor_nodes(false),
MonNodeState = monitor_node_state(),
ok.
flush_node_msgs(NodeMsg, No) ->
case receive Msg -> Msg end of
{NodeMsg, N} when N >= No ->
flush_node_msgs(NodeMsg, N+1);
OtherMsg ->
OtherMsg
end.
node_loop_send(Pid, Msg, No) ->
Pid ! {Msg, No},
node_loop_send(Pid, Msg, No + 1).
monitor_nodes_errors(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
error = net_kernel:monitor_nodes(asdf),
{error,
{unknown_options,
[gurka]}} = net_kernel:monitor_nodes(true,
[gurka]),
{error,
{options_not_a_list,
gurka}} = net_kernel:monitor_nodes(true,
gurka),
{error,
{option_value_mismatch,
[{node_type,visible},
{node_type,hidden}]}}
= net_kernel:monitor_nodes(true,
[{node_type,hidden},
{node_type,visible}]),
{error,
{option_value_mismatch,
[{node_type,visible},
{node_type,all}]}}
= net_kernel:monitor_nodes(true,
[{node_type,all},
{node_type,visible}]),
{error,
{bad_option_value,
{node_type,
blaha}}}
= net_kernel:monitor_nodes(true, [{node_type, blaha}]),
MonNodeState = monitor_node_state(),
ok.
monitor_nodes_combinations(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
monitor_nodes_all_comb(true),
[VisibleName, HiddenName] = get_nodenames(2,
monitor_nodes_combinations),
{ok, Visible} = start_node(VisibleName, ""),
receive_all_comb_nodeup_msgs(visible, Visible),
no_msgs(),
stop_node(Visible),
receive_all_comb_nodedown_msgs(visible, Visible, connection_closed),
no_msgs(),
{ok, Hidden} = start_node(HiddenName, "-hidden"),
receive_all_comb_nodeup_msgs(hidden, Hidden),
no_msgs(),
stop_node(Hidden),
receive_all_comb_nodedown_msgs(hidden, Hidden, connection_closed),
no_msgs(),
monitor_nodes_all_comb(false),
MonNodeState = monitor_node_state(),
no_msgs(),
ok.
monitor_nodes_all_comb(Flag) ->
ok = net_kernel:monitor_nodes(Flag),
ok = net_kernel:monitor_nodes(Flag,
[nodedown_reason]),
ok = net_kernel:monitor_nodes(Flag,
[{node_type, hidden}]),
ok = net_kernel:monitor_nodes(Flag,
[{node_type, visible}]),
ok = net_kernel:monitor_nodes(Flag,
[{node_type, all}]),
ok = net_kernel:monitor_nodes(Flag,
[nodedown_reason,
{node_type, hidden}]),
ok = net_kernel:monitor_nodes(Flag,
[nodedown_reason,
{node_type, visible}]),
ok = net_kernel:monitor_nodes(Flag,
[nodedown_reason,
{node_type, all}]),
%% There currently are 8 different combinations
8.
receive_all_comb_nodeup_msgs(visible, Node) ->
io:format("Receive nodeup visible...~n"),
Exp = [{nodeup, Node},
{nodeup, Node, []}]
++ mk_exp_mn_all_comb_nodeup_msgs_common(visible, Node),
receive_mn_msgs(Exp),
io:format("ok~n"),
ok;
receive_all_comb_nodeup_msgs(hidden, Node) ->
io:format("Receive nodeup hidden...~n"),
Exp = mk_exp_mn_all_comb_nodeup_msgs_common(hidden, Node),
receive_mn_msgs(Exp),
io:format("ok~n"),
ok.
mk_exp_mn_all_comb_nodeup_msgs_common(Type, Node) ->
InfoNt = [{node_type, Type}],
[{nodeup, Node, InfoNt},
{nodeup, Node, InfoNt},
{nodeup, Node, InfoNt},
{nodeup, Node, InfoNt}].
receive_all_comb_nodedown_msgs(visible, Node, Reason) ->
io:format("Receive nodedown visible...~n"),
Exp = [{nodedown, Node},
{nodedown, Node, [{nodedown_reason, Reason}]}]
++ mk_exp_mn_all_comb_nodedown_msgs_common(visible,
Node,
Reason),
receive_mn_msgs(Exp),
io:format("ok~n"),
ok;
receive_all_comb_nodedown_msgs(hidden, Node, Reason) ->
io:format("Receive nodedown hidden...~n"),
Exp = mk_exp_mn_all_comb_nodedown_msgs_common(hidden, Node, Reason),
receive_mn_msgs(Exp),
io:format("ok~n"),
ok.
mk_exp_mn_all_comb_nodedown_msgs_common(Type, Node, Reason) ->
InfoNt = [{node_type, Type}],
InfoNdrNt = lists:sort([{nodedown_reason, Reason}]++InfoNt),
[{nodedown, Node, InfoNt},
{nodedown, Node, InfoNt},
{nodedown, Node, InfoNdrNt},
{nodedown, Node, InfoNdrNt}].
receive_mn_msgs([]) ->
ok;
receive_mn_msgs(Msgs) ->
io:format("Expecting msgs: ~p~n", [Msgs]),
receive
{_Dir, _Node} = Msg ->
io:format("received ~p~n", [Msg]),
case lists:member(Msg, Msgs) of
true -> receive_mn_msgs(lists:delete(Msg, Msgs));
false -> ct:fail({unexpected_message, Msg,
expected_messages, Msgs})
end;
{Dir, Node, Info} ->
Msg = {Dir, Node, lists:sort(Info)},
io:format("received ~p~n", [Msg]),
case lists:member(Msg, Msgs) of
true -> receive_mn_msgs(lists:delete(Msg, Msgs));
false -> ct:fail({unexpected_message, Msg,
expected_messages, Msgs})
end;
Msg ->
io:format("received ~p~n", [Msg]),
ct:fail({unexpected_message, Msg,
expected_messages, Msgs})
end.
monitor_nodes_cleanup(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
Me = self(),
No = monitor_nodes_all_comb(true),
Inf = spawn(fun () ->
monitor_nodes_all_comb(true),
Me ! {mons_set, self()},
receive after infinity -> ok end
end),
TO = spawn(fun () ->
monitor_nodes_all_comb(true),
Me ! {mons_set, self()},
receive after 500 -> ok end
end),
receive {mons_set, Inf} -> ok end,
receive {mons_set, TO} -> ok end,
MNLen = length(MonNodeState) + No*3,
MNLen = length(monitor_node_state()),
MonInf = erlang:monitor(process, Inf),
MonTO = erlang:monitor(process, TO),
exit(Inf, bang),
No = monitor_nodes_all_comb(false),
receive {'DOWN', MonInf, process, Inf, bang} -> ok end,
receive {'DOWN', MonTO, process, TO, normal} -> ok end,
MonNodeState = monitor_node_state(),
no_msgs(),
ok.
monitor_nodes_many(Config) when is_list(Config) ->
MonNodeState = monitor_node_state(),
[Name] = get_nodenames(1, monitor_nodes_many),
%% We want to perform more than 2^16 net_kernel:monitor_nodes
%% since this will wrap an internal counter
No = (1 bsl 16) + 17,
repeat(fun () -> ok = net_kernel:monitor_nodes(true) end, No),
No = length(monitor_node_state()) - length(MonNodeState),
{ok, Node} = start_node(Name),
repeat(fun () -> receive {nodeup, Node} -> ok end end, No),
stop_node(Node),
repeat(fun () -> receive {nodedown, Node} -> ok end end, No),
ok = net_kernel:monitor_nodes(false),
no_msgs(10),
MonNodeState = monitor_node_state(),
ok.
%% Misc. functions
monitor_node_state() ->
erts_debug:set_internal_state(available_internal_state, true),
MonitoringNodes = erts_debug:get_internal_state(monitoring_nodes),
erts_debug:set_internal_state(available_internal_state, false),
MonitoringNodes.
check_no_nodedown_nodeup(TimeOut) ->
receive
{nodeup, _, _} = Msg -> ct:fail({unexpected_nodeup, Msg});
{nodeup, _} = Msg -> ct:fail({unexpected_nodeup, Msg});
{nodedown, _, _} = Msg -> ct:fail({unexpected_nodedown, Msg});
{nodedown, _} = Msg -> ct:fail({unexpected_nodedown, Msg})
after TimeOut ->
ok
end.
print_my_messages() ->
{messages, Messages} = process_info(self(), messages),
io:format("Messages: ~p~n", [Messages]),
ok.
sleep(T) -> receive after T * 1000 -> ok end.
start_node(Name, Param, this) ->
NewParam = Param ++ " -pa " ++ filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, peer, [{args, NewParam}, {erl, [this]}]);
start_node(Name, Param, "this") ->
NewParam = Param ++ " -pa " ++ filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, peer, [{args, NewParam}, {erl, [this]}]);
start_node(Name, Param, Rel) when is_atom(Rel) ->
NewParam = Param ++ " -pa " ++ filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, peer, [{args, NewParam}, {erl, [{release, atom_to_list(Rel)}]}]);
start_node(Name, Param, Rel) when is_list(Rel) ->
NewParam = Param ++ " -pa " ++ filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, peer, [{args, NewParam}, {erl, [{release, Rel}]}]).
start_node(Name, Param) ->
NewParam = Param ++ " -pa " ++ filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, slave, [{args, NewParam}]).
start_node(Name) ->
start_node(Name, "").
stop_node(Node) ->
test_server:stop_node(Node).
get_nodenames(N, T) ->
get_nodenames(N, T, []).
get_nodenames(0, _, Acc) ->
Acc;
get_nodenames(N, T, Acc) ->
U = erlang:unique_integer([positive]),
get_nodenames(N-1, T, [list_to_atom(atom_to_list(T)
++ "-"
++ ?MODULE_STRING
++ "-"
++ integer_to_list(U)) | Acc]).
get_numbered_nodenames(N, T) ->
get_numbered_nodenames(N, T, []).
get_numbered_nodenames(0, _, Acc) ->
Acc;
get_numbered_nodenames(N, T, Acc) ->
U = erlang:unique_integer([positive]),
NL = [list_to_atom(atom_to_list(T) ++ integer_to_list(N)
++ "-"
++ ?MODULE_STRING
++ "-"
++ integer_to_list(U)) | Acc],
get_numbered_nodenames(N-1, T, NL).
wait_until(Fun) ->
case Fun() of
true ->
ok;
_ ->
receive
after 100 ->
wait_until(Fun)
end
end.
repeat(Fun, 0) when is_function(Fun) ->
ok;
repeat(Fun, N) when is_function(Fun), is_integer(N), N > 0 ->
Fun(),
repeat(Fun, N-1).
no_msgs(Wait) ->
receive after Wait -> no_msgs() end.
no_msgs() ->
{messages, []} = process_info(self(), messages).
block_emu(Ms) ->
erts_debug:set_internal_state(available_internal_state, true),
Res = erts_debug:set_internal_state(block, Ms),
erts_debug:set_internal_state(available_internal_state, false),
Res.
|