@@ -333,8 +333,9 @@ TEST(coroutine_hook, exists) {
333
333
const int fd = 100 ; // fake fd
334
334
ASSERT_EQ (swoole_coroutine_socket_create (fd), 0 );
335
335
ASSERT_TRUE (swoole_coroutine_socket_exists (fd));
336
- Socket * sock = swoole_coroutine_get_socket_object (fd);
336
+ auto sock = swoole_coroutine_get_socket_object (fd);
337
337
ASSERT_EQ (sock->get_fd (), fd);
338
+ swoole_coroutine_close (fd);
338
339
});
339
340
}
340
341
@@ -477,3 +478,134 @@ TEST(coroutine_hook, socket_close) {
477
478
ASSERT_TRUE (results[" read" ]);
478
479
});
479
480
}
481
+
482
+ TEST (coroutine_hook, poll) {
483
+ coroutine::run ([&](void *arg) {
484
+ auto pair = create_socket_pair ();
485
+
486
+ auto buffer = sw_tg_buffer ();
487
+ buffer->clear ();
488
+ buffer->append_random_bytes (256 * 1024 , false );
489
+
490
+ std::map<std::string, bool > results;
491
+ auto _sock0 = pair.first ;
492
+ auto _fd0 = _sock0->move_fd ();
493
+ swoole_coroutine_socket_create (_fd0);
494
+
495
+ auto _sock1 = pair.second ;
496
+ auto _fd1 = _sock1->move_fd ();
497
+ swoole_coroutine_socket_create (_fd1);
498
+
499
+ Coroutine::create ([&](void *) {
500
+ ssize_t result;
501
+ result = swoole_coroutine_write (_fd0, buffer->value (), buffer->get_length ());
502
+ ASSERT_GT (result, 0 );
503
+ System::sleep (0.01 );
504
+ result = swoole_coroutine_write (_fd1, buffer->value (), 16 * 1024 );
505
+ ASSERT_GT (result, 0 );
506
+ });
507
+
508
+ struct pollfd fds[2 ];
509
+ char buf[4096 ];
510
+
511
+ bzero (fds, sizeof (pollfd));
512
+ fds[0 ].fd = _fd0;
513
+ fds[0 ].events = POLLIN;
514
+ fds[1 ].fd = _fd1;
515
+ fds[1 ].events = POLLIN;
516
+
517
+ ASSERT_EQ (swoole_coroutine_poll (fds, 2 , 1000 ), 1 );
518
+ ASSERT_TRUE (fds[1 ].revents & POLLIN);
519
+
520
+ ssize_t result = swoole_coroutine_read (_fd1, buf, sizeof (buf));
521
+ ASSERT_GT (result, 1024 );
522
+
523
+ System::sleep (0.02 );
524
+
525
+ bzero (fds, sizeof (pollfd));
526
+ fds[0 ].fd = _fd0;
527
+ fds[0 ].events = POLLIN;
528
+ fds[1 ].fd = _fd1;
529
+ fds[1 ].events = POLLIN;
530
+
531
+ ASSERT_EQ (swoole_coroutine_poll (fds, 2 , 1000 ), 2 );
532
+ ASSERT_TRUE (fds[0 ].revents & POLLIN);
533
+ ASSERT_TRUE (fds[1 ].revents & POLLIN);
534
+ result = swoole_coroutine_read (_fd0, buf, sizeof (buf));
535
+ ASSERT_GT (result, 1024 );
536
+ result = swoole_coroutine_read (_fd1, buf, sizeof (buf));
537
+ ASSERT_GT (result, 1024 );
538
+
539
+ System::sleep (0.02 );
540
+
541
+ bzero (fds, sizeof (pollfd));
542
+ fds[0 ].fd = _fd0;
543
+ fds[0 ].events = POLLIN | POLLOUT;
544
+ fds[1 ].fd = _fd1;
545
+ fds[1 ].events = POLLIN | POLLOUT;
546
+
547
+ ASSERT_EQ (swoole_coroutine_poll (fds, 2 , 1000 ), 2 );
548
+ ASSERT_TRUE (fds[0 ].revents & POLLIN);
549
+ ASSERT_TRUE (fds[1 ].revents & POLLIN);
550
+ ASSERT_FALSE (fds[0 ].revents & POLLOUT); // not writable
551
+ ASSERT_TRUE (fds[1 ].revents & POLLOUT);
552
+ result = swoole_coroutine_read (_fd0, buf, sizeof (buf));
553
+ ASSERT_GT (result, 1024 );
554
+ result = swoole_coroutine_read (_fd1, buf, sizeof (buf));
555
+ ASSERT_GT (result, 1024 );
556
+ });
557
+ }
558
+
559
+ TEST (coroutine_hook, poll_fake) {
560
+ coroutine::run ([&](void *arg) {
561
+ auto pair = create_socket_pair ();
562
+
563
+ auto buffer = sw_tg_buffer ();
564
+ buffer->clear ();
565
+ buffer->append_random_bytes (256 * 1024 , false );
566
+
567
+ std::map<std::string, bool > results;
568
+ auto _sock0 = pair.first ;
569
+ auto _fd0 = _sock0->move_fd ();
570
+ swoole_coroutine_socket_create (_fd0);
571
+
572
+ auto _sock1 = pair.second ;
573
+ auto _fd1 = _sock1->move_fd ();
574
+ swoole_coroutine_socket_create (_fd1);
575
+
576
+ Coroutine::create ([&](void *) {
577
+ ssize_t result;
578
+ result = swoole_coroutine_write (_fd0, buffer->value (), buffer->get_length ());
579
+ ASSERT_GT (result, 0 );
580
+ System::sleep (0.01 );
581
+ result = swoole_coroutine_write (_fd1, buffer->value (), 16 * 1024 );
582
+ ASSERT_GT (result, 0 );
583
+ });
584
+
585
+ struct pollfd fds[2 ];
586
+ char buf[4096 ];
587
+
588
+ bzero (fds, sizeof (pollfd));
589
+ fds[0 ].fd = _fd1;
590
+ fds[0 ].events = POLLIN;
591
+
592
+ ASSERT_EQ (swoole_coroutine_poll_fake (fds, 1 , 1000 ), 1 );
593
+ ASSERT_TRUE (fds[0 ].revents & POLLIN);
594
+
595
+ ssize_t result = swoole_coroutine_read (_fd1, buf, sizeof (buf));
596
+ ASSERT_GT (result, 1024 );
597
+
598
+ bzero (fds, sizeof (pollfd));
599
+ ASSERT_EQ (swoole_coroutine_poll_fake (fds, 2 , 1000 ), -1 );
600
+ ASSERT_EQ (swoole_get_last_error (), SW_ERROR_INVALID_PARAMS);
601
+
602
+ System::sleep (0.02 );
603
+
604
+ bzero (fds, sizeof (pollfd));
605
+ fds[0 ].fd = _fd0;
606
+ fds[0 ].events = POLLIN | POLLOUT;
607
+ ASSERT_EQ (swoole_coroutine_poll_fake (fds, 1 , 1000 ), 1 );
608
+ ASSERT_TRUE (fds[0 ].revents & POLLIN);
609
+ ASSERT_TRUE (fds[0 ].revents & POLLOUT);
610
+ });
611
+ }
0 commit comments