@@ -34,6 +34,7 @@ test() ->
3434 ok = test_terminate_delete_child (),
3535 ok = test_terminate_timeout (),
3636 ok = test_which_children (),
37+ ok = test_count_children (),
3738 ok .
3839
3940test_basic_supervisor () ->
@@ -88,6 +89,55 @@ test_start_child() ->
8889 exit (SupPid , shutdown ),
8990 ok .
9091
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+
91141test_terminate_delete_child () ->
92142 {ok , SupPid } = supervisor :start_link (? MODULE , {test_no_child , self ()}),
93143 {ok , Pid } = supervisor :start_child (SupPid , #{
0 commit comments