@@ -71,21 +71,6 @@ static void test_func_stream_protocol(ProcessPool &pool) {
71
71
test_func (pool);
72
72
}
73
73
74
- static int test_incr_shm_value (ProcessPool *pool) {
75
- auto shm_value = static_cast <int *>(pool->ptr );
76
- return sw_atomic_add_fetch (shm_value, 1 );
77
- }
78
-
79
- static MAYBE_UNUSED int test_get_shm_value (ProcessPool *pool) {
80
- auto shm_value = static_cast <int *>(pool->ptr );
81
- return *shm_value;
82
- }
83
-
84
- static void test_set_shm_value (ProcessPool *pool, int value) {
85
- auto shm_value = static_cast <int *>(pool->ptr );
86
- *shm_value = value;
87
- }
88
-
89
74
TEST (process_pool, tcp) {
90
75
ProcessPool pool{};
91
76
int svr_port = swoole::test::get_random_port ();
@@ -210,16 +195,15 @@ TEST(process_pool, shutdown) {
210
195
211
196
TEST (process_pool, reload) {
212
197
ProcessPool pool{};
213
- int *shm_value = ( int *) sw_mem_pool ()-> alloc ( sizeof ( int ) );
198
+ test::counter_init ( );
214
199
ASSERT_EQ (pool.create (2 ), SW_OK);
215
200
216
201
// init
217
202
pool.set_max_packet_size (8192 );
218
- pool.ptr = shm_value;
219
203
pool.max_wait_time = 1 ;
220
204
221
205
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
222
- test_incr_shm_value (pool );
206
+ test::counter_incr ( 0 );
223
207
224
208
sysv_signal (SIGTERM, SIG_IGN);
225
209
@@ -245,7 +229,7 @@ TEST(process_pool, reload) {
245
229
246
230
pool.destroy ();
247
231
248
- ASSERT_EQ (*shm_value , 4 );
232
+ ASSERT_EQ (test::counter_get ( 0 ) , 4 );
249
233
250
234
sysv_signal (SIGTERM, SIG_DFL);
251
235
}
@@ -257,23 +241,22 @@ static void test_async_pool() {
257
241
// init
258
242
pool.set_max_packet_size (8192 );
259
243
pool.set_protocol (SW_PROTOCOL_TASK);
260
- int *shm_value = (int *) sw_mem_pool ()->alloc (sizeof (int ));
261
- pool.ptr = shm_value;
262
244
pool.async = true ;
245
+ test::counter_init ();
263
246
264
247
pool.onStart = [](ProcessPool *pool) {
265
248
current_pool = pool;
266
249
sysv_signal (SIGTERM, [](int sig) { current_pool->running = false ; });
267
250
};
268
251
269
252
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
270
- test_set_shm_value (pool , magic_number);
253
+ test::counter_set ( 0 , magic_number);
271
254
current_worker = worker;
272
255
current_pool = pool;
273
256
sysv_signal (SIGTERM, [](int sig) { current_pool->running = false ; });
274
257
275
258
swoole_signal_set (SIGTERM, [](int sig) {
276
- DEBUG () << " value: " << test_incr_shm_value (current_pool ) << " ; "
259
+ DEBUG () << " value: " << test::counter_incr ( 0 ) << " ; "
277
260
<< " SIGTERM, stop worker\n " ;
278
261
current_pool->stop (current_worker);
279
262
});
@@ -282,7 +265,7 @@ static void test_async_pool() {
282
265
};
283
266
284
267
pool.onMessage = [](ProcessPool *pool, RecvData *msg) {
285
- DEBUG () << " value: " << test_incr_shm_value (current_pool ) << " ; "
268
+ DEBUG () << " value: " << test::counter_incr ( 0 ) << " ; "
286
269
<< " onMessage, kill\n " ;
287
270
kill (pool->master_pid , SIGTERM);
288
271
};
@@ -301,7 +284,7 @@ static void test_async_pool() {
301
284
302
285
pool.destroy ();
303
286
304
- ASSERT_EQ (*shm_value , magic_number + 2 );
287
+ ASSERT_EQ (test::counter_get ( 0 ) , magic_number + 2 );
305
288
306
289
swoole_signal_clear ();
307
290
sysv_signal (SIGTERM, SIG_DFL);
@@ -312,10 +295,6 @@ TEST(process_pool, async) {
312
295
// ASSERT_EQ(test::spawn_exec_and_wait([]() { test_async_pool(); }), 0);
313
296
}
314
297
315
- static void test_shm_value_incr_and_put_log (ProcessPool *pool, const char *msg) {
316
- DEBUG () << " PID: " << getpid () << " , VALUE: " << test_incr_shm_value (pool) << " ; " << msg << std::endl;
317
- }
318
-
319
298
static void test_async_pool_with_mb () {
320
299
ProcessPool pool{};
321
300
ASSERT_EQ (pool.create (1 , 0 , SW_IPC_UNIXSOCK), SW_OK);
@@ -329,19 +308,17 @@ static void test_async_pool_with_mb() {
329
308
// init
330
309
pool.set_max_packet_size (8192 );
331
310
pool.set_protocol (SW_PROTOCOL_TASK);
332
- auto shm_value = (int *) sw_mem_pool ()->alloc (sizeof (int ));
333
- *shm_value = 0 ;
334
- pool.ptr = shm_value;
311
+ test::counter_init ();
335
312
pool.async = true ;
336
313
337
314
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
338
315
current_worker = worker;
339
316
current_pool = pool;
340
317
341
- test_shm_value_incr_and_put_log (pool , " onWorkerStart" );
318
+ test::counter_incr_and_put_log ( 0 , " onWorkerStart" );
342
319
343
320
swoole_signal_set (SIGTERM, [](int sig) {
344
- test_shm_value_incr_and_put_log (current_pool , " SIGTERM, stop worker" );
321
+ test::counter_incr_and_put_log ( 0 , " SIGTERM, stop worker" );
345
322
current_pool->stop (sw_worker ());
346
323
});
347
324
@@ -352,19 +329,17 @@ static void test_async_pool_with_mb() {
352
329
current_worker = worker;
353
330
current_pool = pool;
354
331
355
- test_shm_value_incr_and_put_log (pool , " onWorkerStop" );
332
+ test::counter_incr_and_put_log ( 0 , " onWorkerStop" );
356
333
};
357
334
358
- pool.onWorkerExit = [](ProcessPool *pool, Worker *worker) {
359
- test_shm_value_incr_and_put_log (pool, " onWorkerExit" );
360
- };
335
+ pool.onWorkerExit = [](ProcessPool *pool, Worker *worker) { test::counter_incr_and_put_log (0 , " onWorkerExit" ); };
361
336
362
337
pool.onStart = [](ProcessPool *pool) {
363
338
current_pool = pool;
364
339
swoole_signal_set (SIGTERM, [](int sig) { current_pool->running = false ; });
365
340
swoole_signal_set (SIGIO, [](int sig) { current_pool->read_message = true ; });
366
341
367
- test_shm_value_incr_and_put_log (pool , " onStart" );
342
+ test::counter_incr_and_put_log ( 0 , " onStart" );
368
343
369
344
swoole_timer_after (100 , [pool](TIMER_PARAMS) {
370
345
pool->send_message (0 , SW_STRL (" detach" ));
@@ -373,16 +348,16 @@ static void test_async_pool_with_mb() {
373
348
});
374
349
};
375
350
376
- pool.onShutdown = [](ProcessPool *pool) { test_shm_value_incr_and_put_log (pool , " onShutdown" ); };
351
+ pool.onShutdown = [](ProcessPool *pool) { test::counter_incr_and_put_log ( 0 , " onShutdown" ); };
377
352
378
353
pool.onMessage = [](ProcessPool *pool, RecvData *msg) {
379
354
auto req = std::string (msg->data , msg->info .len );
380
355
381
356
if (req == " detach" ) {
382
- test_shm_value_incr_and_put_log (pool , " onMessage, detach" );
357
+ test::counter_incr_and_put_log ( 0 , " onMessage, detach" );
383
358
ASSERT_TRUE (pool->detach ());
384
359
} else if ((req == " shutdown" )) {
385
- test_shm_value_incr_and_put_log (pool , " onMessage, shutdown" );
360
+ test::counter_incr_and_put_log ( 0 , " onMessage, shutdown" );
386
361
pool->shutdown ();
387
362
}
388
363
};
@@ -394,7 +369,7 @@ static void test_async_pool_with_mb() {
394
369
395
370
pool.destroy ();
396
371
397
- ASSERT_GE (*shm_value , 8 );
372
+ ASSERT_GE (test::counter_get ( 0 ) , 8 );
398
373
399
374
swoole_signal_clear ();
400
375
sysv_signal (SIGTERM, SIG_DFL);
0 commit comments