3030#define STATUS_OK 1
3131#define STATUS_ERROR 0
3232#define STATUS_PENDING -1
33-
34- #define UNIT_TESTS_SIZE 5
3533#define CALLBACK_TIME 2 /* 2 seconds */
3634
3735#define SERVER_PORT "9092"
@@ -52,6 +50,8 @@ struct unit_test tests[] = {
5250 {4 , 0 , STATUS_PENDING , "plugin resumed from engine" },
5351};
5452
53+ #define UNIT_TESTS_SIZE (sizeof(tests) / sizeof(struct unit_test))
54+
5555struct event_test {
5656 flb_pipefd_t pipe [2 ];
5757 int server_fd ;
@@ -120,7 +120,7 @@ static int cb_collector_time(struct flb_input_instance *ins,
120120 flb_input_collector_pause (ut -> coll_id , ins );
121121
122122 /*
123- * before to return, trigger test 2 (collector_event_fd ) by writing a byte
123+ * before to return, trigger test 1 (collector_fd_event ) by writing a byte
124124 * to our local pipe.
125125 */
126126 val = 1 ;
@@ -147,7 +147,7 @@ static int cb_collector_fd(struct flb_input_instance *ins,
147147 bytes = read (ctx -> pipe [0 ], & val , sizeof (val ));
148148 if (bytes <= 0 ) {
149149 flb_errno ();
150- set_unit_test_status (ctx , 0 , STATUS_ERROR );
150+ set_unit_test_status (ctx , 1 , STATUS_ERROR );
151151 flb_engine_exit (config );
152152 }
153153 else {
@@ -190,8 +190,7 @@ static int cb_collector_server_socket(struct flb_input_instance *ins,
190190 flb_plg_info (ins , "test pause/resume in 5 seconds..." );
191191 flb_input_test_pause_resume (ins , 5 );
192192
193- /* force an exit */
194- //flb_engine_exit(config);
193+ /* return */
195194 FLB_INPUT_RETURN (0 );
196195}
197196
@@ -233,7 +232,7 @@ static struct event_test *config_create(struct flb_input_instance *ins)
233232 ctx -> tests = flb_malloc (size );
234233 if (!ctx -> tests ) {
235234 flb_errno ();
236- free (ctx );
235+ flb_free (ctx );
237236 return NULL ;
238237 }
239238 memcpy (ctx -> tests , & tests , size );
@@ -261,6 +260,7 @@ static int cb_event_test_init(struct flb_input_instance *ins,
261260 ret = flb_input_set_collector_time (ins , cb_collector_time ,
262261 CALLBACK_TIME , 0 , config );
263262 if (ret < 0 ) {
263+ config_destroy (ctx );
264264 return -1 ;
265265 }
266266 ut = & ctx -> tests [0 ];
@@ -270,13 +270,15 @@ static int cb_event_test_init(struct flb_input_instance *ins,
270270 ret = flb_pipe_create (ctx -> pipe );
271271 if (ret == -1 ) {
272272 flb_errno ();
273+ config_destroy (ctx );
273274 return -1 ;
274275 }
275276 ret = flb_input_set_collector_event (ins ,
276277 cb_collector_fd ,
277278 ctx -> pipe [0 ],
278279 config );
279280 if (ret < 0 ) {
281+ config_destroy (ctx );
280282 return -1 ;
281283 }
282284 ut = & ctx -> tests [1 ];
@@ -286,6 +288,7 @@ static int cb_event_test_init(struct flb_input_instance *ins,
286288 fd = flb_net_server (SERVER_PORT , SERVER_IFACE );
287289 if (fd < 0 ) {
288290 flb_errno ();
291+ config_destroy (ctx );
289292 return -1 ;
290293 }
291294 flb_net_socket_nonblocking (fd );
@@ -297,6 +300,7 @@ static int cb_event_test_init(struct flb_input_instance *ins,
297300 ctx -> server_fd ,
298301 config );
299302 if (ret == -1 ) {
303+ config_destroy (ctx );
300304 return -1 ;
301305 }
302306 ut = & ctx -> tests [2 ];
@@ -306,6 +310,7 @@ static int cb_event_test_init(struct flb_input_instance *ins,
306310 ret = flb_input_set_collector_time (ins , cb_collector_server_client ,
307311 CALLBACK_TIME * 2 , 0 , config );
308312 if (ret < 0 ) {
313+ config_destroy (ctx );
309314 return -1 ;
310315 }
311316 ctx -> client_coll_id = ret ;
@@ -314,6 +319,7 @@ static int cb_event_test_init(struct flb_input_instance *ins,
314319 upstream = flb_upstream_create (config , "127.0.0.1" , atoi (SERVER_PORT ),
315320 FLB_IO_TCP , NULL );
316321 if (!upstream ) {
322+ config_destroy (ctx );
317323 return -1 ;
318324 }
319325 ctx -> upstream = upstream ;
@@ -350,6 +356,7 @@ static void cb_event_test_resume(void *data, struct flb_config *config)
350356static int in_event_test_exit (void * data , struct flb_config * config )
351357{
352358 int i ;
359+ int failed = FLB_FALSE ;
353360 struct event_test * ctx = data ;
354361 struct unit_test * ut ;
355362 (void ) * config ;
@@ -360,13 +367,19 @@ static int in_event_test_exit(void *data, struct flb_config *config)
360367 if (ut -> status != STATUS_OK ) {
361368 flb_plg_error (ctx -> ins , "unit test #%i '%s' failed" ,
362369 i , ut -> desc );
370+ failed = FLB_TRUE ;
363371 }
364372 else {
365373 flb_plg_info (ctx -> ins , "unit test #%i '%s' succeeded" ,
366374 i , ut -> desc );
367375 }
368376 }
369377
378+ /* if one test failed, perform an abrupt exit with proper error */
379+ if (failed ) {
380+ exit (EXIT_FAILURE );
381+ }
382+
370383 config_destroy (ctx );
371384 return 0 ;
372385}
0 commit comments