ChannelTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. declare(strict_types=1);
  3. namespace tests;
  4. use InvalidArgumentException;
  5. use PHPUnit\Framework\TestCase;
  6. use ReflectionClass;
  7. use ReflectionException;
  8. use stdClass;
  9. use Workerman\Coroutine\Channel;
  10. use PHPUnit\Framework\Attributes\DataProvider;
  11. use Workerman\Coroutine\Channel\Memory;
  12. use Workerman\Coroutine;
  13. class ChannelTest extends TestCase
  14. {
  15. /**
  16. * Test initializing channel with valid capacity.
  17. */
  18. public function testInitializeWithValidCapacity()
  19. {
  20. $channel = new Channel(1);
  21. $this->assertInstanceOf(Channel::class, $channel);
  22. $this->assertEquals(1, $channel->getCapacity());
  23. }
  24. /**
  25. * Test initializing channel with invalid capacities.
  26. */
  27. #[DataProvider('invalidCapacitiesProvider')]
  28. public function testInitializeWithInvalidCapacity($capacity)
  29. {
  30. $this->expectException(InvalidArgumentException::class);
  31. new Channel($capacity);
  32. }
  33. /**
  34. * Data provider for invalid capacities.
  35. */
  36. public static function invalidCapacitiesProvider(): array
  37. {
  38. return [
  39. [0],
  40. [-1],
  41. [-100]
  42. ];
  43. }
  44. /**
  45. * Test pushing and popping data.
  46. */
  47. public function testPushAndPop()
  48. {
  49. $channel = new Channel(2);
  50. $data1 = 'test data 1';
  51. $data2 = 'test data 2';
  52. // Push data into the channel
  53. $this->assertTrue($channel->push($data1));
  54. $this->assertTrue($channel->push($data2));
  55. // Verify the length of the channel
  56. $this->assertEquals(2, $channel->length());
  57. // Pop data from the channel
  58. $this->assertEquals($data1, $channel->pop());
  59. $this->assertEquals($data2, $channel->pop());
  60. }
  61. /**
  62. * Test pushing data when the channel is full.
  63. * @throws ReflectionException
  64. */
  65. public function testPushWhenFull()
  66. {
  67. // Memory driver does not support push with timeout
  68. if ($this->driverIsMemory()) {
  69. $this->assertTrue(true);
  70. return;
  71. }
  72. $channel = new Channel(1);
  73. $this->assertTrue($channel->push('data1'));
  74. $timeout = 0.5;
  75. // Attempt to push when the channel is full with a timeout
  76. $startTime = microtime(true);
  77. $this->assertFalse($channel->push('data2', $timeout));
  78. $elapsedTime = microtime(true) - $startTime;
  79. // Verify that the push operation timed out
  80. $this->assertTrue(0.1 > abs($elapsedTime - $timeout));
  81. }
  82. /**
  83. * Test popping data when the channel is empty.
  84. * @throws ReflectionException
  85. */
  86. public function testPopWhenEmpty()
  87. {
  88. // Memory driver does not support push with timeout
  89. if ($this->driverIsMemory()) {
  90. $this->assertTrue(true);
  91. return;
  92. }
  93. $channel = new Channel(1);
  94. // Attempt to pop when the channel is empty with a timeout
  95. $startTime = microtime(true);
  96. $this->assertFalse($channel->pop(0.1));
  97. $elapsedTime = microtime(true) - $startTime;
  98. // Verify that the pop operation timed out
  99. $this->assertGreaterThanOrEqual(0.09, $elapsedTime);
  100. }
  101. /**
  102. * Test closing the channel and its effects.
  103. */
  104. public function testCloseChannel()
  105. {
  106. $channel = new Channel(1);
  107. $this->assertTrue($channel->push('data'));
  108. // Close the channel
  109. $channel->close();
  110. // Attempt to push after closing
  111. $this->assertFalse($channel->push('new data'));
  112. // Pop the remaining data
  113. $this->assertEquals('data', $channel->pop());
  114. // Attempt to pop after channel is empty and closed
  115. $this->assertFalse($channel->pop());
  116. }
  117. /**
  118. * Test that push and pop return false when channel is closed.
  119. */
  120. public function testPushAndPopReturnFalseWhenClosed()
  121. {
  122. $channel = new Channel(1);
  123. $channel->close();
  124. $this->assertFalse($channel->push('data'));
  125. $this->assertFalse($channel->pop());
  126. }
  127. /**
  128. * Test the length and capacity methods.
  129. */
  130. public function testLengthAndCapacity()
  131. {
  132. $channel = new Channel(5);
  133. $this->assertEquals(0, $channel->length());
  134. $this->assertEquals(5, $channel->getCapacity());
  135. $channel->push('data1');
  136. $channel->push('data2');
  137. $this->assertEquals(2, $channel->length());
  138. }
  139. /**
  140. * Test pushing and popping with different data types.
  141. */
  142. #[DataProvider('dataTypesProvider')]
  143. public function testPushAndPopWithDifferentDataTypes($data)
  144. {
  145. $channel = new Channel(1);
  146. $this->assertTrue($channel->push($data));
  147. $this->assertSame($data, $channel->pop());
  148. }
  149. /**
  150. * Data provider for different data types.
  151. */
  152. public static function dataTypesProvider(): array
  153. {
  154. return [
  155. ['string'],
  156. [123],
  157. [123.456],
  158. [true],
  159. [false],
  160. [null],
  161. [[]],
  162. [['key' => 'value']],
  163. [new stdClass()],
  164. [fopen('php://memory', 'r')],
  165. ];
  166. }
  167. /**
  168. * Test pushing to a closed channel immediately returns false.
  169. */
  170. public function testPushToClosedChannel()
  171. {
  172. $channel = new Channel(1);
  173. $channel->close();
  174. $this->assertFalse($channel->push('data', 0));
  175. }
  176. /**
  177. * Test popping from a closed and empty channel immediately returns false.
  178. */
  179. public function testPopFromClosedAndEmptyChannel()
  180. {
  181. $channel = new Channel(1);
  182. $channel->close();
  183. $this->assertFalse($channel->pop(0));
  184. }
  185. /**
  186. * @return bool
  187. * @throws ReflectionException
  188. */
  189. protected function driverIsMemory(): bool
  190. {
  191. $reflectionClass = new ReflectionClass(Channel::class);
  192. $instance = $reflectionClass->newInstance();
  193. $property = $reflectionClass->getProperty('driver');
  194. $driverValue = $property->getValue($instance);
  195. return $driverValue instanceof Memory;
  196. }
  197. /**
  198. * 测试 hasConsumers 当没有消费者时返回 false
  199. */
  200. public function testHasConsumersWhenNoConsumers()
  201. {
  202. if (!Coroutine::isCoroutine()) {
  203. $this->assertTrue(true);
  204. return;
  205. }
  206. $channel = new Channel(1);
  207. $this->assertFalse($channel->hasConsumers());
  208. $channel->close();
  209. }
  210. /**
  211. * 测试 hasConsumers 当有消费者等待时返回 true
  212. * @throws ReflectionException
  213. */
  214. public function testHasConsumersWhenConsumersWaiting()
  215. {
  216. if ($this->driverIsMemory()) {
  217. $this->assertTrue(true);
  218. return;
  219. }
  220. $channel = new Channel(1);
  221. $sync = new Channel(1);
  222. Coroutine::create(function () use ($channel, $sync) {
  223. $sync->push(true);
  224. $channel->pop();
  225. });
  226. $sync->pop();
  227. $this->assertTrue($channel->hasConsumers());
  228. Coroutine::create(function () use ($channel) {
  229. $channel->push('data');
  230. });
  231. $channel->close();
  232. }
  233. /**
  234. * 测试 hasProducers 当没有生产者时返回 false
  235. * @throws ReflectionException
  236. */
  237. public function testHasProducersWhenNoProducers()
  238. {
  239. if ($this->driverIsMemory()) {
  240. $this->assertTrue(true);
  241. return;
  242. }
  243. $channel = new Channel(1);
  244. $this->assertFalse($channel->hasProducers());
  245. $channel->close();
  246. }
  247. /**
  248. * 测试 hasProducers 当有生产者等待时返回 true
  249. * @throws ReflectionException
  250. */
  251. public function testHasProducersWhenProducersWaiting()
  252. {
  253. if ($this->driverIsMemory()) {
  254. $this->assertTrue(true);
  255. return;
  256. }
  257. $channel = new Channel(1);
  258. $channel->push('data1');
  259. $sync = new Channel(1);
  260. Coroutine::create(function () use ($channel, $sync) {
  261. $sync->push(true);
  262. $channel->push('data2');
  263. });
  264. $sync->pop();
  265. $this->assertTrue($channel->hasProducers());
  266. $channel->pop();
  267. $channel->close();
  268. }
  269. }