aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/test/global_group_SUITE.erl
blob: a8b87390eb58662993a7ef88f2b7c834730dc98f (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1998-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%

-module(global_group_SUITE).

-export([all/1]).
-export([start_gg_proc/1, no_gg_proc/1, no_gg_proc_sync/1, compatible/1, 
	 one_grp/1, one_grp_x/1, two_grp/1, hidden_groups/1, test_exit/1]).
-export([init/1, init/2, init2/2, start_proc/1, start_proc_rereg/1]).

-export([init_per_testcase/2, fin_per_testcase/2]).

%-compile(export_all).

-include("test_server.hrl").

-define(NODES, [node()|nodes()]).

-define(UNTIL(Seq), loop_until_true(fun() -> Seq end)).

all(suite) -> 
    [start_gg_proc, no_gg_proc, no_gg_proc_sync, 
     compatible, one_grp, one_grp_x, two_grp, test_exit,
     hidden_groups].

-define(TESTCASE, testcase_name).
-define(testcase, ?config(?TESTCASE, Config)).

init_per_testcase(Case, Config) when atom(Case), list(Config) ->
    Dog=?t:timetrap(?t:minutes(5)),
    [{?TESTCASE, Case}, {watchdog, Dog}|Config].

fin_per_testcase(_Func, Config) ->
    Dog=?config(watchdog, Config),
    ?t:timetrap_cancel(Dog).

%%-----------------------------------------------------------------
%% Test suites for global groups.
%% Should be started in a CC view with:
%% erl -sname XXX -rsh ctrsh where XXX not in [cp1 .. cpN]
%%-----------------------------------------------------------------


start_gg_proc(suite) -> [];
start_gg_proc(doc) -> ["Check that the global_group processes are started automatically. "];
start_gg_proc(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(120)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group.config"),
    ?line {ok, Fd}=file:open(File, write),
    [Ncp1,Ncp2,Ncp3] = node_names([cp1, cp2, cp3], Config),
    ?line config(Fd, Ncp1, Ncp2, Ncp3, "cpx", "cpy", "cpz", "cpq"),

    ?line Cp1nn = node_at(Ncp1),
    ?line Cp2nn = node_at(Ncp2),
    ?line Cp3nn = node_at(Ncp3),

    ?line {ok, Cp1} = start_node(Ncp1, Config),
    ?line {ok, Cp2} = start_node(Ncp2, Config),
    ?line {ok, Cp3} = start_node(Ncp3, Config),

    ?line [] = rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}]),
    ?line [] = rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}]),
    ?line [] = rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}]),

    % stop the nodes, and make sure names are released.
    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),

    ?line ?UNTIL(undefined =:= global:whereis_name(test)),
    ?line test_server:timetrap_cancel(Dog),
    ok.
    


no_gg_proc(suite) -> [];
no_gg_proc(doc) -> ["Start a system without global groups. Nodes are not "
		    "synced at start (sync_nodes_optional is not defined)"];
no_gg_proc(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(200)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "no_global_group.config"),
    ?line {ok, Fd} = file:open(File, write),
    ?line config_no(Fd),

    ?line NN = node_name(atom_to_list(node())),
    ?line Cp1nn = list_to_atom("cp1@" ++ NN),
    ?line Cp2nn = list_to_atom("cp2@" ++ NN),
    ?line Cp3nn = list_to_atom("cp3@" ++ NN),
    ?line Cpxnn = list_to_atom("cpx@" ++ NN),
    ?line Cpynn = list_to_atom("cpy@" ++ NN),
    ?line Cpznn = list_to_atom("cpz@" ++ NN),

    ?line {ok, Cp1} = start_node_no(cp1, Config),
    ?line {ok, Cp2} = start_node_no(cp2, Config),
    ?line {ok, Cp3} = start_node_no(cp3, Config),
    ?line {ok, Cpx} = start_node_no(cpx, Config),
    ?line {ok, Cpy} = start_node_no(cpy, Config),
    ?line {ok, Cpz} = start_node_no(cpz, Config),

    %% let the nodes know of each other
    ?line pong = rpc:call(Cp1, net_adm, ping, [Cp2nn]),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp3nn]),
    ?line pong = rpc:call(Cp3, net_adm, ping, [Cpxnn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpynn]),
    ?line pong = rpc:call(Cpy, net_adm, ping, [Cpznn]),

    ?line wait_for_ready_net(),

    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}]),
    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}]),


    % start a proc and register it
    ?line {Pid2, yes} = rpc:call(Cp2, ?MODULE, start_proc, [test2]),

    ?line RegNames = lists:sort([test2,test_server]),

    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}])),


    ?line undefined = rpc:call(Cp3, global_group, global_groups, []),

    ?line Own_nodes_should = [node(), Cp1nn, Cp2nn, Cp3nn, 
			      Cpxnn, Cpynn, Cpznn],
    ?line Own_nodes = rpc:call(Cp3, global_group, own_nodes, []), 
    ?line true = (Own_nodes -- Own_nodes_should) =:= [],
    ?line true = (Own_nodes_should -- Own_nodes) =:= [],
    
    ?line Pid2 = rpc:call(Cp1, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout2)
				 end,
    ?line Pid2 = rpc:call(Cp2, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout3)
				 end,
    ?line Pid2 = rpc:call(Cpz, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout4)
				 end,


    % start a proc and register it
    ?line {PidX, yes} = rpc:call(Cpx, ?MODULE, start_proc, [test]),


    %%------------------------------------
    %% Test monitor nodes
    %%------------------------------------
    ?line Pid2 = rpc:call(Cp1, global_group, send, [{node, Cp2nn}, test2, monitor]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, monitor]),


    % Kill node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cp1}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cp1),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Kill node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cpz}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cpz),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cp1}]),
    ?line {ok, Cp1} = start_node_no(cp1, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp1nn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cp1nn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cpz}]),
    ?line {ok, Cpz} = start_node_no(cpz, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cpznn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpznn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % stop the nodes, and make sure names are released.
    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),
    stop_node(Cpx),
    stop_node(Cpy),
    stop_node(Cpz),

    ?line ?UNTIL(undefined =:= global:whereis_name(test)),
    ?line test_server:timetrap_cancel(Dog),
    ok.
    



no_gg_proc_sync(suite) -> [];
no_gg_proc_sync(doc) -> 
    ["Start a system without global groups, but syncing the nodes by using " 
     "sync_nodes_optional."];
no_gg_proc_sync(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(200)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "no_global_group_sync.config"),
    ?line {ok, Fd} = file:open(File, write),

    [Ncp1,Ncp2,Ncp3,Ncpx,Ncpy,Ncpz] = 
        node_names([cp1,cp2,cp3,cpx,cpy,cpz], Config),
    ?line config_sync(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz),

    ?line Cp1nn = node_at(Ncp1),
    ?line Cp2nn = node_at(Ncp2),
    ?line Cp3nn = node_at(Ncp3),
    ?line Cpxnn = node_at(Ncpx),
    ?line Cpynn = node_at(Ncpy),
    ?line Cpznn = node_at(Ncpz),

    ?line {ok, Cp1} = start_node_no2(Ncp1, Config),
    ?line {ok, Cp2} = start_node_no2(Ncp2, Config),
    ?line {ok, Cp3} = start_node_no2(Ncp3, Config),
    ?line {ok, Cpx} = start_node_no2(Ncpx, Config),
    ?line {ok, Cpy} = start_node_no2(Ncpy, Config),
    ?line {ok, Cpz} = start_node_no2(Ncpz, Config),

    %% let the nodes know of each other
    ?line pong = rpc:call(Cp1, net_adm, ping, [Cp2nn]),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp3nn]),
    ?line pong = rpc:call(Cp3, net_adm, ping, [Cpxnn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpynn]),
    ?line pong = rpc:call(Cpy, net_adm, ping, [Cpznn]),

    ?line wait_for_ready_net(),

    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}]),
    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}]),


    % start a proc and register it
    ?line {Pid2, yes} = rpc:call(Cp2, ?MODULE, start_proc, [test2]),

    ?line RegNames = lists:sort([test2,test_server]),

    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}])),


    ?line undefined = rpc:call(Cp3, global_group, global_groups, []),

    ?line Own_nodes_should = [node(), Cp1nn, Cp2nn, Cp3nn, 
			      Cpxnn, Cpynn, Cpznn],
    ?line Own_nodes = rpc:call(Cp3, global_group, own_nodes, []), 
    ?line true = (Own_nodes -- Own_nodes_should) =:= [],
    ?line true = (Own_nodes_should -- Own_nodes) =:= [],
    
    ?line Pid2 = rpc:call(Cp1, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout2)
				 end,
    ?line Pid2 = rpc:call(Cp2, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout3)
				 end,
    ?line Pid2 = rpc:call(Cpz, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout4)
				 end,


    % start a proc and register it
    ?line {PidX, yes} = rpc:call(Cpx, ?MODULE, start_proc, [test]),


    %%------------------------------------
    %% Test monitor nodes
    %%------------------------------------
    ?line Pid2 = rpc:call(Cp1, global_group, send, [{node, Cp2nn}, test2, monitor]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, monitor]),


    % Kill node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cp1}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cp1),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Kill node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cpz}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cpz),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cp1}]),
    ?line {ok, Cp1} = start_node_no2(Ncp1, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp1nn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cp1nn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cpz}]),
    ?line {ok, Cpz} = start_node_no2(Ncpz, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cpznn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpznn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % stop the nodes, and make sure names are released.
    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),
    stop_node(Cpx),
    stop_node(Cpy),
    stop_node(Cpz),

    ?line ?UNTIL(undefined =:= global:whereis_name(test)),
    ?line test_server:timetrap_cancel(Dog),
    ok.
    



compatible(suite) -> [];
compatible(doc) -> 
    ["Check that a system without global groups is compatible with the old R4 system."];
compatible(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(200)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group_comp.config"),
    ?line {ok, Fd} = file:open(File, write),

    [Ncp1,Ncp2,Ncp3,Ncpx,Ncpy,Ncpz] = 
        node_names([cp1,cp2,cp3,cpx,cpy,cpz], Config),
    ?line config_comp(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz),

    ?line Cp1nn = node_at(Ncp1),
    ?line Cp2nn = node_at(Ncp2),
    ?line Cp3nn = node_at(Ncp3),
    ?line Cpxnn = node_at(Ncpx),
    ?line Cpynn = node_at(Ncpy),
    ?line Cpznn = node_at(Ncpz),

    ?line {ok, Cp1} = start_node_comp(Ncp1, Config),
    ?line {ok, Cp2} = start_node_comp(Ncp2, Config),
    ?line {ok, Cp3} = start_node_comp(Ncp3, Config),
    ?line {ok, Cpx} = start_node_comp(Ncpx, Config),
    ?line {ok, Cpy} = start_node_comp(Ncpy, Config),
    ?line {ok, Cpz} = start_node_comp(Ncpz, Config),

    %% let the nodes know of each other
    ?line pong = rpc:call(Cp1, net_adm, ping, [Cp2nn]),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp3nn]),
    ?line pong = rpc:call(Cp3, net_adm, ping, [Cpxnn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpynn]),
    ?line pong = rpc:call(Cpy, net_adm, ping, [Cpznn]),

    ?line wait_for_ready_net(),

    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}]),
    ?line [test_server] = rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}]),
    ?line [test_server] = rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}]),
    ?line [test_server] = rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}]),


    % start a proc and register it
    ?line {Pid2, yes} = rpc:call(Cp2, ?MODULE, start_proc, [test2]),

    ?line RegNames = lists:sort([test2,test_server]),

    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cp1nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cp2nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cp3nn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp1, global_group, registered_names, [{node, Cpxnn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp2, global_group, registered_names, [{node, Cpynn}])),
    ?line RegNames = 
	lists:sort(
	  rpc:call(Cp3, global_group, registered_names, [{node, Cpznn}])),


    ?line undefined = rpc:call(Cp3, global_group, global_groups, []),

    ?line Own_nodes_should = [node(), Cp1nn, Cp2nn, Cp3nn, 
			      Cpxnn, Cpynn, Cpznn],
    ?line Own_nodes = rpc:call(Cp3, global_group, own_nodes, []), 
    ?line true = (Own_nodes -- Own_nodes_should) =:= [],
    ?line true = (Own_nodes_should -- Own_nodes) =:= [],
    
    ?line Pid2 = rpc:call(Cp1, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout2)
				 end,
    ?line Pid2 = rpc:call(Cp2, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout3)
				 end,
    ?line Pid2 = rpc:call(Cpz, global_group, send, [test2, {ping, self()}]),
    ?line receive
	      {pong, Cp2} -> ok
				 after
				     2000 -> test_server:fail(timeout4)
				 end,


    % start a proc and register it
    ?line {PidX, yes} = rpc:call(Cpx, ?MODULE, start_proc, [test]),


    %%------------------------------------
    %% Test monitor nodes
    %%------------------------------------
    ?line Pid2 = rpc:call(Cp1, global_group, send, [{node, Cp2nn}, test2, monitor]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, monitor]),


    % Kill node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cp1}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cp1),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Kill node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodedown, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodedown, Cpz}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cpz),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cp1
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cp1}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cp1}]),
    ?line {ok, Cp1} = start_node_comp(Ncp1, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cp1nn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cp1nn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % Restart node Cpz
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, {wait_nodeup, Cpz}]),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, {wait_nodeup, Cpz}]),
    ?line {ok, Cpz} = start_node_comp(Ncpz, Config),
    ?line pong = rpc:call(Cp2, net_adm, ping, [Cpznn]),
    ?line pong = rpc:call(Cpx, net_adm, ping, [Cpznn]),
    ?line wait_for_ready_net(),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),

    % stop the nodes, and make sure names are released.
    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),
    stop_node(Cpx),
    stop_node(Cpy),
    stop_node(Cpz),

    ?line ?UNTIL(undefined =:= global:whereis_name(test)),
    ?line test_server:timetrap_cancel(Dog),
    ok.
    



one_grp(suite) -> [];
one_grp(doc) -> ["Test a system with only one global group. "];
one_grp(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(120)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group.config"),
    ?line {ok, Fd} = file:open(File, write),
    [Ncp1,Ncp2,Ncp3] = node_names([cp1, cp2, cp3], Config),
    ?line config(Fd, Ncp1, Ncp2, Ncp3, "cpx", "cpy", "cpz", "cpq"),

    ?line {ok, Cp1} = start_node(Ncp1, Config),
    ?line {ok, Cp2} = start_node(Ncp2, Config),
    ?line {ok, Cp3} = start_node(Ncp3, Config),

    % sleep a while to make the global_group to sync...
    test_server:sleep(1000),

    % start a proc and register it
    ?line {Pid, yes} = rpc:call(Cp1, ?MODULE, start_proc, [test]),

    % test that it is registered at all nodes
    ?line Pid = rpc:call(Cp1, global, whereis_name, [test]),
    ?line Pid = rpc:call(Cp2, global, whereis_name, [test]),
    ?line Pid = rpc:call(Cp3, global, whereis_name, [test]),
    
    % try to register the same name
    ?line no = rpc:call(Cp1, global, register_name, [test, self()]),
    
    % let process exit, check that it is unregistered automatically
    Pid ! die,
    ?line 
    ?UNTIL(begin
               (undefined =:= rpc:call(Cp1, global, whereis_name, [test])) and
               (undefined =:= rpc:call(Cp2, global, whereis_name, [test])) and
               (undefined =:= rpc:call(Cp3, global, whereis_name, [test]))
           end),
    
    % test re_register
    ?line {Pid2, yes} = rpc:call(Cp1, ?MODULE, start_proc, [test]),
    ?line Pid2 = rpc:call(Cp3, global, whereis_name, [test]),    
    Pid3 = rpc:call(Cp3, ?MODULE, start_proc_rereg, [test]),
    ?line Pid3 = rpc:call(Cp3, global, whereis_name, [test]),        

    % test sending
    rpc:call(Cp1, global, send, [test, {ping, self()}]),
    receive
	{pong, Cp3} -> ok
    after
	2000 -> test_server:fail(timeout1)
    end,

    rpc:call(Cp3, global, send, [test, {ping, self()}]),
    receive
	{pong, Cp3} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    ?line rpc:call(Cp3, global, unregister_name, [test]),    
    ?line undefined = rpc:call(Cp1, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cp2, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cp3, global, whereis_name, [test]),

    Pid3 ! die,
    ?line ?UNTIL(undefined =:= rpc:call(Cp3, global, whereis_name, [test])),

    % register a proc
    ?line {_, yes} = rpc:call(Cp3, ?MODULE, start_proc, [test]),

    % stop the nodes, and make sure names are released.
    stop_node(Cp3),

    ?line ?UNTIL(undefined =:= rpc:call(Cp1, global, whereis_name, [test])),
    Pid2 ! die,

    stop_node(Cp1),
    stop_node(Cp2),

    ?line test_server:timetrap_cancel(Dog),
    ok.
    




one_grp_x(suite) -> [];
one_grp_x(doc) -> ["Check a system with only one global group. "
		   "Start the nodes with different time intervals. "];
one_grp_x(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(120)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group.config"),
    ?line {ok, Fd} = file:open(File, write),
    [Ncp1,Ncp2,Ncp3] = node_names([cp1, cp2, cp3], Config),
    ?line config(Fd, Ncp1, Ncp2, Ncp3, "cpx", "cpy", "cpz", "cpq"),

    ?line {ok, Cp1} = start_node(Ncp1, Config),
    % sleep a while to make the global_group to sync...
    test_server:sleep(1000),

    % start a proc and register it
    ?line {Pid, yes} = rpc:call(Cp1, ?MODULE, start_proc, [test]),

    ?line {ok, Cp2} = start_node(Ncp2, Config),
    % sleep a while to make the global_group to sync...
    test_server:sleep(1000),

    % test that it is registered at all nodes
    ?line Pid = rpc:call(Cp1, global, whereis_name, [test]),
    ?line Pid = rpc:call(Cp2, global, whereis_name, [test]),

    ?line {ok, Cp3} = start_node(Ncp3, Config),
    % sleep a while to make the global_group to sync...
    test_server:sleep(1000),

    ?line Pid = rpc:call(Cp3, global, whereis_name, [test]),
    
    % try to register the same name
    ?line no = rpc:call(Cp1, global, register_name, [test, self()]),
    
    % let process exit, check that it is unregistered automatically
    Pid ! die,
    ?line 
    ?UNTIL(begin
               (undefined =:= rpc:call(Cp1, global, whereis_name, [test])) and
               (undefined =:= rpc:call(Cp2, global, whereis_name, [test])) and
               (undefined =:= rpc:call(Cp3, global, whereis_name, [test]))
           end),
    
    % test re_register
    ?line {Pid2, yes} = rpc:call(Cp1, ?MODULE, start_proc, [test]),
    ?line Pid2 = rpc:call(Cp3, global, whereis_name, [test]),    

    Pid2 ! die,

    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),

    ?line test_server:timetrap_cancel(Dog),
    ok.
    





two_grp(suite) -> [];
two_grp(doc) -> ["Test a two global group system. "];
two_grp(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(200)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group.config"),
    ?line {ok, Fd} = file:open(File, write),

    [Ncp1,Ncp2,Ncp3,Ncpx,Ncpy,Ncpz,Ncpq] = 
        node_names([cp1,cp2,cp3,cpx,cpy,cpz,cpq], Config),
    ?line config(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz, Ncpq),

    ?line Cp1nn = node_at(Ncp1),
    ?line Cp2nn = node_at(Ncp2),
    ?line Cp3nn = node_at(Ncp3),
    ?line Cpxnn = node_at(Ncpx),
    ?line Cpynn = node_at(Ncpy),
    ?line Cpznn = node_at(Ncpz),

    ?line {ok, Cp1} = start_node(Ncp1, Config),
    ?line {ok, Cp2} = start_node(Ncp2, Config),
    ?line {ok, Cp3} = start_node(Ncp3, Config),
    ?line {ok, Cpx} = start_node(Ncpx, Config),
    ?line {ok, Cpy} = start_node(Ncpy, Config),
    ?line {ok, Cpz} = start_node(Ncpz, Config),

    %% The groups (cpq not started):
    %% [{nc1, [cp1,cp2,cp3]}, {nc2, [cpx,cpy,cpz]}, {nc3, [cpq]}]

    % sleep a while to make the global_groups to sync...
    test_server:sleep(1000),

    % check the global group names
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp1, global_group, global_groups, []),
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp2, global_group, global_groups, []),
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp3, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpx, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpy, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpz, global_group, global_groups, []),

    % check the global group nodes
    ?line [Cp1nn, Cp2nn, Cp3nn] = rpc:call(Cp1, global_group, own_nodes, []),
    ?line [Cp1nn, Cp2nn, Cp3nn] = rpc:call(Cp2, global_group, own_nodes, []),
    ?line [Cp1nn, Cp2nn, Cp3nn] = rpc:call(Cp3, global_group, own_nodes, []),
    ?line [Cpxnn, Cpynn, Cpznn] = rpc:call(Cpx, global_group, own_nodes, []),
    ?line [Cpxnn, Cpynn, Cpznn] = rpc:call(Cpy, global_group, own_nodes, []),
    ?line [Cpxnn, Cpynn, Cpznn] = rpc:call(Cpz, global_group, own_nodes, []),


    % start a proc and register it
    ?line {Pid1, yes} = rpc:call(Cp1, ?MODULE, start_proc, [test]),

    ?line Pid1 = rpc:call(Cp1, global_group, send, [test, {io, from_cp1}]),
    ?line Pid1 = rpc:call(Cpx, global_group, send, [test, {io, from_cpx}]),
    ?line Pid1 = rpc:call(Cp1, global_group, send, [{group,nc1}, test, 
                                                    {io, from_cp1}]),
    ?line [test] = 
        rpc:call(Cpx, global_group, registered_names, [{node, Cp1nn}]),
    ?line [test] = 
        rpc:call(Cpx, global_group, registered_names, [{group, nc1}]),
    ?line [] = rpc:call(Cpx, global_group, registered_names, [{node, Cpxnn}]),
    ?line [] = rpc:call(Cpx, global_group, registered_names, [{group, nc2}]),
    ?line Pid1 = rpc:call(Cpx, global_group, send, [{group,nc1}, test, 
                                                    {io, from_cp1}]),
    ?line {badarg,{test,{io,from_cpx}}} = 
	rpc:call(Cp1, global_group, send, [{group,nc2}, test, {io, from_cpx}]),
    ?line {badarg,{test,{io,from_cpx}}} = 
	rpc:call(Cpx, global_group, send, [{group,nc2}, test, {io, from_cpx}]),



    % test that it is registered at all nodes
    ?line Pid1 = rpc:call(Cp1, global, whereis_name, [test]),
    ?line Pid1 = rpc:call(Cp2, global, whereis_name, [test]),
    ?line Pid1 = rpc:call(Cp3, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cpx, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cpy, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cpz, global, whereis_name, [test]),
    
    % start a proc and register it
    ?line {PidX, yes} = rpc:call(Cpx, ?MODULE, start_proc, [test]),

    % test that it is registered at all nodes
    ?line Pid1 = rpc:call(Cp1, global, whereis_name, [test]),
    ?line Pid1 = rpc:call(Cp2, global, whereis_name, [test]),
    ?line Pid1 = rpc:call(Cp3, global, whereis_name, [test]),
    ?line PidX = rpc:call(Cpx, global, whereis_name, [test]),
    ?line PidX = rpc:call(Cpy, global, whereis_name, [test]),
    ?line PidX = rpc:call(Cpz, global, whereis_name, [test]),
    
    Pid1 ! die,
    %% If we don't wait for global on other nodes to have updated its
    %% tables, 'test' may still be defined at the point when it is
    %% tested a few lines below.
    ?line
    ?UNTIL(begin 
               Pid = rpc:call(Cp2, global, whereis_name, [test]),
               undefined =:= Pid
           end),

    % start a proc and register it
    ?line {Pid2, yes} = rpc:call(Cp2, ?MODULE, start_proc, [test2]),

    % test that it is registered at all nodes
    ?line Pid2 = rpc:call(Cp1, global, whereis_name, [test2]),
    ?line Pid2 = rpc:call(Cp2, global, whereis_name, [test2]),
    ?line Pid2 = rpc:call(Cp3, global, whereis_name, [test2]),
    ?line PidX = rpc:call(Cpx, global, whereis_name, [test]),
    ?line PidX = rpc:call(Cpy, global, whereis_name, [test]),
    ?line PidX = rpc:call(Cpz, global, whereis_name, [test]),
    
    ?line undefined = rpc:call(Cp1, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cp2, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cp3, global, whereis_name, [test]),
    ?line undefined = rpc:call(Cpx, global, whereis_name, [test2]),
    ?line undefined = rpc:call(Cpy, global, whereis_name, [test2]),
    ?line undefined = rpc:call(Cpz, global, whereis_name, [test2]),
    

    ?line Pid2 = rpc:call(Cp1, global_group, send, [test2, {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line Pid2 = rpc:call(Cp2, global_group, send, [test2, {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line Pid2 = rpc:call(Cp3, global_group, send, [test2, {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    ?line PidX = rpc:call(Cpx, global_group, send, [test, {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line PidX = rpc:call(Cpy, global_group, send, [test, {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line PidX = rpc:call(Cpz, global_group, send, [test, {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    ?line Pid2 = rpc:call(Cpx, global_group, send, [{node, Cp1nn}, test2, 
                                                    {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line Pid2 = rpc:call(Cpy, global_group, send, [{node, Cp2nn}, test2, 
                                                    {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line Pid2 = rpc:call(Cpz, global_group, send, [{node, Cp3nn}, test2, 
                                                    {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpznn}, test, 
                                                    {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line PidX = rpc:call(Cpy, global_group, send, [{node, Cpxnn}, test, 
                                                    {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line PidX = rpc:call(Cpz, global_group, send, [{node, Cpynn}, test, 
                                                    {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    ?line Pid2 = rpc:call(Cpx, global_group, send, [{group, nc1}, test2, 
                                                    {ping, self()}]),
    receive
	{pong, Cp2} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,
    ?line PidX = rpc:call(Cpy, global_group, send, [{group, nc2}, test, 
                                                    {ping, self()}]),
    receive
	{pong, Cpx} -> ok
    after
	2000 -> test_server:fail(timeout2)
    end,

    %%------------------------------------
    %% Test monitor nodes
    %%------------------------------------
    ?line Pid2 = 
        rpc:call(Cp1, global_group, send, [{node, Cp2nn}, test2, monitor]),
    ?line PidX = 
        rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, monitor]),    


    % Kill node Cp1
    ?line Pid2 = rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, 
                                                    {wait_nodedown, Cp1}]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, 
                                                    {wait_nodedown, Cp1}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cp1),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop_nodedown),
    ?line  PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, to_loop]),

    % Kill node Cpz
    ?line Pid2 = rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, 
                                                    {wait_nodedown, Cpz}]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, 
                                                    {wait_nodedown, Cpz}]),
    ?line test_server:sleep(100),
    ?line stop_node(Cpz),
    ?line test_server:sleep(1000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop_nodedown),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, to_loop]),

    % Restart node Cp1
    ?line [Cp1nn, Cp2nn, Cp3nn] = rpc:call(Cp2, global_group, own_nodes, []),
    ?line Pid2 = rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, 
                                                    {wait_nodeup, Cp1}]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, 
                                                    {wait_nodeup, Cp1}]),
    ?line test_server:sleep(100),
    ?line {ok, Cp1} = start_node(Ncp1, Config),
    ?line test_server:sleep(5000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop_nodeup),
    ?line PidX = 
	rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, to_loop]),


    % Restart node Cpz
    ?line Pid2 = rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, 
                                                    {wait_nodeup, Cpz}]),
    ?line PidX = rpc:call(Cpx, global_group, send, [{node, Cpxnn}, test, 
                                                    {wait_nodeup, Cpz}]),
    ?line test_server:sleep(100),
    ?line {ok, Cpz} = start_node(Ncpz, Config),
    ?line test_server:sleep(5000),
    
    ?line ok = assert_loop(Cp2, Cp2nn, test2, Pid2, loop_nodeup),
    ?line ok = assert_loop(Cpx, Cpxnn, test, PidX, loop),
    ?line Pid2 = 
	rpc:call(Cp2, global_group, send, [{node, Cp2nn}, test2, to_loop]),


    Pid2 ! die,
    PidX ! die,

    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),
    stop_node(Cpx),
    stop_node(Cpy),
    stop_node(Cpz),

    ?line test_server:timetrap_cancel(Dog),
    ok.
    


hidden_groups(suite) -> [];
hidden_groups(doc) -> ["Test hidden global groups."];
hidden_groups(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(200)),

    ?line Dir = ?config(priv_dir, Config),
    ?line File = filename:join(Dir, "global_group.config"),
    ?line {ok, Fd} = file:open(File, write),

    [Ncp1,Ncp2,Ncp3,Ncpx,Ncpy,Ncpz,Ncpq] = 
        node_names([cp1,cp2,cp3,cpx,cpy,cpz,cpq], Config),
    ?line config_hidden(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz, Ncpq),

    ?line {ok, Cp1} = start_node(Ncp1, Config),
    ?line {ok, Cp2} = start_node(Ncp2, Config),
    ?line {ok, Cp3} = start_node(Ncp3, Config),
    ?line {ok, Cpx} = start_node(Ncpx, Config),
    ?line {ok, Cpy} = start_node(Ncpy, Config),
    ?line {ok, Cpz} = start_node(Ncpz, Config),
    ?line {ok, Cpq} = start_node(Ncpq, Config),

    % sleep a while to make the global_groups to sync...
    test_server:sleep(1000),

    % check the global group names
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp1, global_group, global_groups, []),
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp2, global_group, global_groups, []),
    ?line {nc1, [nc2, nc3]} = rpc:call(Cp3, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpx, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpy, global_group, global_groups, []),
    ?line {nc2, [nc1, nc3]} = rpc:call(Cpz, global_group, global_groups, []),

    % check the global group nodes
    ?line [Cp1, Cp2, Cp3] = rpc:call(Cp1, global_group, own_nodes, []),
    ?line [Cp1, Cp2, Cp3] = rpc:call(Cp2, global_group, own_nodes, []),
    ?line [Cp1, Cp2, Cp3] = rpc:call(Cp3, global_group, own_nodes, []),
    ?line [Cpx, Cpy, Cpz] = rpc:call(Cpx, global_group, own_nodes, []),
    ?line [Cpx, Cpy, Cpz] = rpc:call(Cpy, global_group, own_nodes, []),
    ?line [Cpx, Cpy, Cpz] = rpc:call(Cpz, global_group, own_nodes, []),
    ?line [Cpq]           = rpc:call(Cpq, global_group, own_nodes, []),

    % Make some inter group connections
    ?line pong = rpc:call(Cp1, net_adm, ping, [Cpx]),
    ?line pong = rpc:call(Cpy, net_adm, ping, [Cp2]),
    ?line pong = rpc:call(Cp3, net_adm, ping, [Cpx]),
    ?line pong = rpc:call(Cpz, net_adm, ping, [Cp3]),
    ?line pong = rpc:call(Cpq, net_adm, ping, [Cp1]),
    ?line pong = rpc:call(Cpz, net_adm, ping, [Cpq]),

    % Check that no inter group connections are visible
    NC1Nodes = lists:sort([Cp1, Cp2, Cp3]),
    NC2Nodes = lists:sort([Cpx, Cpy, Cpz]),
    ?line NC1Nodes = lists:sort([Cp1|rpc:call(Cp1, erlang, nodes, [])]),
    ?line NC1Nodes = lists:sort([Cp2|rpc:call(Cp2, erlang, nodes, [])]),
    ?line NC1Nodes = lists:sort([Cp3|rpc:call(Cp3, erlang, nodes, [])]),
    ?line NC2Nodes = lists:sort([Cpx|rpc:call(Cpx, erlang, nodes, [])]),
    ?line NC2Nodes = lists:sort([Cpy|rpc:call(Cpy, erlang, nodes, [])]),
    ?line NC2Nodes = lists:sort([Cpz|rpc:call(Cpz, erlang, nodes, [])]),
    NC12Nodes = lists:append(NC1Nodes, NC2Nodes),
    ?line false = lists:any(fun(N) -> lists:member(N, NC12Nodes) end,
			    rpc:call(Cpq, erlang, nodes, [])),


    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),
    stop_node(Cpx),
    stop_node(Cpy),
    stop_node(Cpz),
    stop_node(Cpq),

    ?line test_server:timetrap_cancel(Dog),
    ok.
    

test_exit(suite) -> [];
test_exit(doc) -> ["Checks when the search process exits. "];
test_exit(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(120)),

    ?line NN = node_name(atom_to_list(node())),
    ?line Cp1nn = list_to_atom("cp1@" ++ NN),

    ?line {ok, Cp1} = start_node(cp1, Config),
    ?line {ok, Cp2} = start_node(cp2, Config),
    ?line {ok, Cp3} = start_node(cp3, Config),

    test_server:sleep(1000),

    ?line {error, illegal_function_call} = 
        rpc:call(Cp1, global_group, registered_names_test, [{node, Cp1nn}]),
    ?line {badarg,_} = 
        rpc:call(Cp1, global_group, send, [king, "The message"]),
    ?line undefined = rpc:call(Cp1, global_group, whereis_name, [king]),

    % stop the nodes, and make sure names are released.
    stop_node(Cp1),
    stop_node(Cp2),
    stop_node(Cp3),

    % sleep to let the nodes die
    test_server:sleep(1000),

    ?line test_server:timetrap_cancel(Dog),
    ok.
    

start_node(Name, Config) ->
    Pa=filename:dirname(code:which(?MODULE)),
    Dir=?config(priv_dir, Config),
    ConfFile = "  -config " ++ filename:join(Dir, "global_group"),
    test_server:start_node(Name, slave, [{args, "-pa " ++ Pa ++ ConfFile}]).

start_node_no(Name, Config) ->
    Pa=filename:dirname(code:which(?MODULE)),
    Dir=?config(priv_dir, Config),
    ConfFile = "  -config " ++ filename:join(Dir, "no_global_group"),
    test_server:start_node(Name, slave, [{args, "-pa " ++ Pa ++ ConfFile}]).

start_node_no2(Name, Config) ->
    Pa=filename:dirname(code:which(?MODULE)),
    Dir=?config(priv_dir, Config),
    ConfFile = "  -config " ++ filename:join(Dir, "no_global_group_sync"),
    test_server:start_node(Name, slave, [{args, "-pa " ++ Pa ++ ConfFile}]).

start_node_comp(Name, Config) ->
    Pa=filename:dirname(code:which(?MODULE)),
    Dir=?config(priv_dir, Config),
    ConfFile = "  -config " ++ filename:join(Dir, "global_group_comp"),
    test_server:start_node(Name, slave, [{args, "-pa " ++ Pa ++ ConfFile}]).

node_names(Names, Config) ->
    [node_name(Name, Config) || Name <- Names].

node_name(Name, Config) ->
    U = "_",
    Pid = os:getpid(),
    {{Y,M,D}, {H,Min,S}} = calendar:now_to_local_time(now()),
    Date = io_lib:format("~4w_~2..0w_~2..0w__~2..0w_~2..0w_~2..0w", 
                         [Y,M,D, H,Min,S]),
    L = lists:flatten(Date),
    lists:concat([Name,U,?testcase,U,Pid,U,U,L]).

stop_node(Node) ->
    ?t:stop_node(Node).


wait_for_ready_net() ->
    Nodes = lists:sort(?NODES),
    ?UNTIL(begin
               lists:all(fun(N) -> Nodes =:= get_known(N) end, Nodes) and
               lists:all(fun(N) -> 
                                 LNs = rpc:call(N, erlang, nodes, []),
                                 Nodes =:= lists:sort([N | LNs])
                         end, Nodes)
           end).

get_known(Node) ->
    Known = gen_server:call({global_name_server,Node}, get_known),
    lists:sort([Node | Known]).

config_hidden(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz, Ncpq) ->
    M = from($@, atom_to_list(node())),
    io:format(Fd, "[{kernel, [{sync_nodes_optional, ['~s@~s','~s@~s','~s@~s', "
                                                   " '~s@~s','~s@~s','~s@~s']},"
	                     "{sync_nodes_timeout, 1000},"
                             "{global_groups, [{nc1, hidden, ['~s@~s','~s@~s','~s@~s']}, "
                                              "{nc2, hidden, ['~s@~s','~s@~s','~s@~s']}, "
	                                      "{nc3, normal, ['~s@~s']}]} ] }]. ~n",
	      [Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M, 
               Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M, 
               Ncpq, M]).

config(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz, Ncpq) ->
    M = from($@, atom_to_list(node())),
    io:format(Fd, "[{kernel, [{sync_nodes_optional, ['~s@~s','~s@~s','~s@~s', "
                                                   " '~s@~s','~s@~s','~s@~s']},"
	          "{sync_nodes_timeout, 1000},"
                  "{global_groups, [{nc1, ['~s@~s','~s@~s','~s@~s']}, "
                                " {nc2, ['~s@~s','~s@~s','~s@~s']}, "
	                         "{nc3, ['~s@~s']}]} ] }]. ~n",
	      [Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M, 
               Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M, 
               Ncpq, M]).

config_no(Fd) ->
    io:format(Fd, "[{kernel, [{global_groups, []}]}]. ~n",[]).

config_sync(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz) ->
    M = from($@, atom_to_list(node())),
    io:format(Fd, "[{kernel, [{sync_nodes_optional, ['~s@~s','~s@~s','~s@~s', "
                                                   " '~s@~s','~s@~s','~s@~s']},"
	          "{sync_nodes_timeout, 1000},"
                  "{global_groups, []} ] }] .~n",
	      [Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M]).


config_comp(Fd, Ncp1, Ncp2, Ncp3, Ncpx, Ncpy, Ncpz) ->
    M = from($@, atom_to_list(node())),
    io:format(Fd, "[{kernel, [{sync_nodes_optional, ['~s@~s','~s@~s','~s@~s', "
                                                   " '~s@~s','~s@~s','~s@~s']},"
	          "{sync_nodes_timeout, 1000} ] }] .~n",
	      [Ncp1, M, Ncp2, M, Ncp3, M,  
               Ncpx, M, Ncpy, M, Ncpz, M]).

node_at(N) ->
    NN = node_name(atom_to_list(node())),
    list_to_atom(lists:concat([N, "@", NN])).

node_name(L) ->
    from($@, L).

from(H, [H | T]) -> T;
from(H, [_ | T]) -> from(H, T);
from(_, []) -> [].


start_proc(Name) ->
    Pid = spawn(?MODULE, init, [self(), Name]),
    receive
	{Pid, Res} -> {Pid, Res}
    end.
    
start_proc_rereg(Name) ->
    Pid = spawn(?MODULE, init2, [self(), Name]),
    receive
	Pid -> Pid
    end.







init(Parent) ->
    Parent ! self(),
    loop().

init(Parent, Name) ->
    X = global:register_name(Name, self()),
    Parent ! {self(),X},
    loop().

init2(Parent, Name) ->
    global:re_register_name(Name, self()),
    Parent ! self(),
    loop().

loop() ->
    receive
	monitor ->
	    global_group:monitor_nodes(true),
	    loop();
	stop_monitor ->
	    global_group:monitor_nodes(false),
	    loop();
	{wait_nodeup, Node} ->
	    loop_nodeup(Node);
	{wait_nodedown, Node} ->
	    loop_nodedown(Node);
	{io, _Msg} ->
	    loop();
	{ping, From} ->
	    From ! {pong, node()},
	    loop();
	{del_lock, Id} ->
	    global:del_lock({Id, self()}),
	    loop();
	{del_lock, Id, Nodes} ->
	    global:del_lock({Id, self()}, Nodes),
	    loop();
	{set_lock, Id, From} ->
	    Res = global:set_lock({Id, self()}, ?NODES, 1),
	    From ! Res,
	    loop();
	{set_lock, Id, From, Nodes} ->
	    Res = global:set_lock({Id, self()}, Nodes, 1),
	    From ! Res,
	    loop();
	{set_lock_loop, Id, From} ->
	    global:set_lock({Id, self()}, ?NODES),
	    From ! {got_lock, self()},
	    loop();
	{{got_notify, From}, Ref} ->
	    receive
		X when element(1, X) == global_name_conflict ->
		    From ! {Ref, yes}
	    after
		0 -> From ! {Ref, no}
	    end,
	    loop();
	{which_loop, From} ->
	    From ! loop,
	    loop();
	die ->
	    exit(normal)
    end.


loop_nodeup(Node) ->
    receive
	{nodeup, Node} ->
	    loop();
	to_loop ->
	    loop();
	{which_loop, From} ->
	    From ! loop_nodeup,
	    loop_nodeup(Node);
	die ->
	    exit(normal)
    end.


loop_nodedown(Node) ->
    receive
	{nodedown, Node} ->
	    loop();
	to_loop ->
	    loop();
	{which_loop, From} ->
	    From ! loop_nodedown,
	    loop_nodedown(Node);
	die ->
	    exit(normal)
    end.

assert_loop(Cp, CpName, Name, NamePid, Loop) ->
    M = {which_loop, self()},
    NamePid = rpc:call(Cp, global_group, send, [{node, CpName}, Name, M]),
    receive
        Loop ->
            ok;
        Other1 ->
            test_server:fail(Other1)
    after 5000 ->
            test_server:fail(timeout)
    end.

loop_until_true(Fun) ->
    case Fun() of
	true ->
	    ok;
	_ ->
	    loop_until_true(Fun)
    end.