UnixTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package ZendX_Console
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_ProgressBar_Adapter_ConsoleTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "ZendX_Console_Process_UnixTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * ZendX_Console_Process_Unix
  32. */
  33. require_once 'ZendX/Console/Process/Unix.php';
  34. /**
  35. * @category Zend
  36. * @package ZendX_Console
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class ZendX_Console_Process_UnixTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("ZendX_Console_Process_UnixTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. public function setUp()
  54. {
  55. if (substr(PHP_OS, 0, 3) === 'WIN') {
  56. $this->markTestSkipped('Cannot run on Windows');
  57. } else if (!in_array(substr(PHP_SAPI, 0, 3), array('cli', 'cgi'))) {
  58. $this->markTestSkipped('Can only run on CLI or CGI enviroment');
  59. } else if (!function_exists('shmop_open')) {
  60. $this->markTestSkipped('shmop_* functions are required');
  61. } else if (!function_exists('pcntl_fork')) {
  62. $this->markTestSkipped('pcntl_* functions are required');
  63. } else if (!function_exists('posix_kill')) {
  64. $this->markTestSkipped('posix_* functions are required');
  65. }
  66. }
  67. public function testStop()
  68. {
  69. $startTime = microtime(true);
  70. $process = new sleepingProcess();
  71. $process->start();
  72. $process->stop();
  73. $diffTime = round(microtime(true) - $startTime);
  74. $this->assertEquals(0, $diffTime);
  75. }
  76. public function testAutomaticEnding()
  77. {
  78. $startTime = microtime(true);
  79. $process = new simpleProcess();
  80. $process->start();
  81. do {
  82. usleep(10);
  83. $diffTime = round(microtime(true) - $startTime);
  84. } while ($process->isRunning() && $diffTime < 2);
  85. $process->stop();
  86. $this->assertEquals(1, $diffTime);
  87. }
  88. public function testParallel()
  89. {
  90. $startTime = microtime(true);
  91. $process1 = new sleepingProcess();
  92. $process2 = new sleepingProcess();
  93. $process1->start();
  94. $process2->start();
  95. do {
  96. usleep(10);
  97. $diffTime = round(microtime(true) - $startTime);
  98. } while (($process1->isRunning() || $process2->isRunning()) && $diffTime < 3);
  99. $process1->stop();
  100. $process2->stop();
  101. $this->assertEquals(2, $diffTime);
  102. }
  103. public function testVariables()
  104. {
  105. $startTime = microtime(true);
  106. $process = new variableProcess();
  107. $process->start();
  108. $process->setVariable('request', true);
  109. do {
  110. usleep(10);
  111. $diffTime = round(microtime(true) - $startTime);
  112. $response = $process->getVariable('response');
  113. } while ($response === null && $diffTime < 3);
  114. $process->stop();
  115. $this->assertTrue($response);
  116. }
  117. public function testAlive()
  118. {
  119. $startTime = microtime(true);
  120. $process = new aliveProcess();
  121. $process->start();
  122. sleep(2);
  123. $this->assertEquals(1, $process->getLastAlive());
  124. $process->stop();
  125. }
  126. }
  127. class simpleProcess extends ZendX_Console_Process_Unix
  128. {
  129. protected function _run()
  130. {
  131. }
  132. }
  133. class sleepingProcess extends ZendX_Console_Process_Unix
  134. {
  135. protected function _run()
  136. {
  137. sleep(1);
  138. }
  139. }
  140. class variableProcess extends ZendX_Console_Process_Unix
  141. {
  142. protected function _run()
  143. {
  144. $var = null;
  145. do {
  146. $var = $this->getVariable('request');
  147. } while ($var === NULL);
  148. $this->setVariable('response', true);
  149. }
  150. }
  151. class aliveProcess extends ZendX_Console_Process_Unix
  152. {
  153. protected function _run()
  154. {
  155. $this->_setAlive();
  156. }
  157. }
  158. // Call ZendX_Console_Process_UnixTest::main() if this source file is executed directly.
  159. if (PHPUnit_MAIN_METHOD == "ZendX_Console_Process_UnixTest::main") {
  160. ZendX_Console_Process_UnixTest::main();
  161. }