@@ -34,6 +34,7 @@ test() ->
34
34
ok = test_terminate_delete_child (),
35
35
ok = test_terminate_timeout (),
36
36
ok = test_which_children (),
37
+ ok = test_count_children (),
37
38
ok .
38
39
39
40
test_basic_supervisor () ->
@@ -88,6 +89,55 @@ test_start_child() ->
88
89
exit (SupPid , shutdown ),
89
90
ok .
90
91
92
+ test_count_children () ->
93
+ % Test with no children - all counts should be zero
94
+ {ok , SupPid } = supervisor :start_link (? MODULE , {test_no_child , self ()}),
95
+ [{specs , 0 }, {active , 0 }, {supervisors , 0 }, {workers , 0 }] = supervisor :count_children (SupPid ),
96
+
97
+ % Add a worker child and verify counts
98
+ {ok , _ChildPid } = supervisor :start_child (SupPid , #{
99
+ id => test_worker ,
100
+ start => {ping_pong_server , start_link , [self ()]},
101
+ restart => permanent ,
102
+ shutdown => 5000 ,
103
+ type => worker
104
+ }),
105
+
106
+ % Check count_children with one active worker
107
+ [{specs , 1 }, {active , 1 }, {supervisors , 0 }, {workers , 1 }] = supervisor :count_children (SupPid ),
108
+
109
+ % Add a supervisor child and verify counts
110
+ {ok , _SupervisorPid } = supervisor :start_child (SupPid , #{
111
+ id => test_supervisor ,
112
+ start => {? MODULE , start_link , [self ()]},
113
+ restart => permanent ,
114
+ shutdown => infinity ,
115
+ type => supervisor
116
+ }),
117
+
118
+ % Check count_children with one worker and one supervisor
119
+ [{specs , 2 }, {active , 2 }, {supervisors , 1 }, {workers , 1 }] = supervisor :count_children (SupPid ),
120
+
121
+ % Terminate the worker child - spec remains but child becomes inactive
122
+ ok = supervisor :terminate_child (SupPid , test_worker ),
123
+ [{specs , 2 }, {active , 1 }, {supervisors , 1 }, {workers , 1 }] = supervisor :count_children (SupPid ),
124
+
125
+ % Delete the worker child - removes the spec completely
126
+ ok = supervisor :delete_child (SupPid , test_worker ),
127
+ [{specs , 1 }, {active , 1 }, {supervisors , 1 }, {workers , 0 }] = supervisor :count_children (SupPid ),
128
+
129
+ % Terminate the supervisor child - spec remains but child becomes inactive
130
+ ok = supervisor :terminate_child (SupPid , test_supervisor ),
131
+ [{specs , 1 }, {active , 0 }, {supervisors , 1 }, {workers , 0 }] = supervisor :count_children (SupPid ),
132
+
133
+ % Delete the supervisor child - removes the spec completely
134
+ ok = supervisor :delete_child (SupPid , test_supervisor ),
135
+ [{specs , 0 }, {active , 0 }, {supervisors , 0 }, {workers , 0 }] = supervisor :count_children (SupPid ),
136
+
137
+ unlink (SupPid ),
138
+ exit (SupPid , shutdown ),
139
+ ok .
140
+
91
141
test_terminate_delete_child () ->
92
142
{ok , SupPid } = supervisor :start_link (? MODULE , {test_no_child , self ()}),
93
143
{ok , Pid } = supervisor :start_child (SupPid , #{
0 commit comments