PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tine20/Tinebase/Redis/Worker/DaemonTest.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 198 lines | 132 code | 23 blank | 43 comment | 0 complexity | 23cad638349d7ceb08c4ef9b05942b54 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. * @package Redis
  5. * @subpackage Worker
  6. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  7. * @author Züleyha Toptas <z.toptas@hotmail.de>
  8. * @copyright Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
  9. */
  10. /**
  11. * Test helper
  12. */
  13. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  14. class Tinebase_Redis_Worker_DaemonTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var array default daemon config
  18. */
  19. protected $_defaultConfig;
  20. /**
  21. * @var Zend_Config action queue config
  22. */
  23. protected $_actionQueueConfigBackup;
  24. /**
  25. * @var Redis
  26. */
  27. protected $_redis;
  28. /**
  29. * @var string path to log file
  30. */
  31. protected $_daemonLog;
  32. /**
  33. * @var string path to pid file
  34. */
  35. protected $_daemonPid;
  36. /**
  37. * @var string path to config ini
  38. */
  39. protected $_configIni;
  40. public function setUp()
  41. {
  42. $this->_defaultConfig = Tinebase_Redis_Worker_Daemon::getDefaultConfig();
  43. // config actionqueue
  44. $this->_actionQueueConfigBackup = Tinebase_Core::getConfig()->actionqueue;
  45. Tinebase_Core::getConfig()->actionqueue = array(
  46. 'adapter' => 'Redis'
  47. );
  48. $this->_redis = new Redis;
  49. $this->_redis->connect($this->_defaultConfig['host'], $this->_defaultConfig['port'], $this->_defaultConfig['timeout']);
  50. // start daemon
  51. $this->_daemonLog = tempnam("/tmp", "tine20daemonttestdaemonlog_");
  52. $this->_daemonPid = tempnam("/tmp", "tine20daemonttestdaemonpid_");
  53. $this->_configIni = tempnam("/tmp", "tine20daemontestconfigini_");
  54. file_put_contents($this->_configIni, <<<EOT
  55. loglevel = 7
  56. EOT
  57. );
  58. $cmd = realpath(__DIR__ . '/../../../../../tine20/Tinebase/Redis/Worker/Daemon.php') . " -v -d -p {$this->_daemonPid} --config {$this->_configIni} > {$this->_daemonLog} 2>&1 &";
  59. $cmd = TestServer::assembleCliCommand($cmd);
  60. exec($cmd);
  61. sleep(1);
  62. }
  63. public function tearDown()
  64. {
  65. // kill daemon
  66. $daemonpid = file_get_contents($this->_daemonPid);
  67. $killCommand = 'kill ' . $daemonpid;
  68. exec($killCommand);
  69. unlink($this->_daemonLog);
  70. unlink($this->_configIni);
  71. // restore actionqueueconfig
  72. Tinebase_Core::getConfig()->actionqueue = $this->_actionQueueConfigBackup;
  73. }
  74. /**
  75. * test whether the daemon has been started
  76. */
  77. public function testDaemonRuns()
  78. {
  79. $daemonPid = file_get_contents($this->_daemonPid);
  80. $this->assertTrue(!! $daemonPid, 'pid file ' . $this->_daemonPid . ' is empty');
  81. $daemonLog = file_get_contents($this->_daemonLog);
  82. $this->assertTrue(!! strstr($daemonLog, 'We are master. Exiting main process now...'), 'We are master. Exiting main process now...');
  83. $this->assertTrue(!! strstr($daemonLog, 'Starting Console_Daemon ...'), 'Starting Console_Daemon ...');
  84. // NOTE error free log setup contains 22 words in debug mode
  85. $this->assertLessThan(23, str_word_count($daemonLog), "There where errors:\n" . $daemonLog);
  86. }
  87. /**
  88. * test the execution of a job in dispatch mode
  89. */
  90. public function testExecuteJobDispatch()
  91. {
  92. $filepath = tempnam("/tmp", __METHOD__);
  93. Tinebase_ActionQueue::getInstance()->queueAction('Tinebase.testSpy', $filepath);
  94. sleep(3);
  95. $result = file_get_contents($filepath);
  96. unlink($filepath);
  97. $this->assertEquals(1, $result, file_get_contents($this->_daemonLog));
  98. }
  99. /**
  100. * test the execution of a job in exec mode
  101. */
  102. public function testExecuteJobExec()
  103. {
  104. $this->markTestIncomplete(
  105. "It's not clear how to config deamon"
  106. );
  107. }
  108. /**
  109. * test retry of a failed job
  110. */
  111. public function testExecuteRetry()
  112. {
  113. $filepath = tempnam("/tmp", __METHOD__);
  114. Tinebase_ActionQueue::getInstance()->queueAction('Tinebase.testSpy', $filepath, 0, 1);
  115. sleep(2);
  116. $result = file_get_contents($filepath);
  117. unlink($filepath);
  118. $this->assertEquals(2, $result, file_get_contents($this->_daemonLog));
  119. }
  120. /**
  121. * test max retry attempts must not exceed configured maxRetry
  122. */
  123. public function testExecuteMaxRetry()
  124. {
  125. $maxRetry = $this->_defaultConfig['maxRetry'];
  126. $filepath = tempnam("/tmp", __METHOD__);
  127. Tinebase_ActionQueue::getInstance()->queueAction('Tinebase.testSpy', $filepath, 0, $maxRetry*2);
  128. sleep(ceil($maxRetry/2));
  129. $daemonLog = file_get_contents($this->_daemonLog);
  130. $result = file_get_contents($filepath);
  131. unlink($filepath);
  132. $this->assertTrue(!! strstr($daemonLog, "ERR (3)"), $daemonLog);
  133. $this->assertTrue(!! strstr($daemonLog, "giving up after $maxRetry retries"), $daemonLog);
  134. $this->assertEquals($maxRetry, $result, file_get_contents($this->_daemonLog));
  135. }
  136. /**
  137. * test number of children must not exceed configured maxChilds
  138. */
  139. public function testMaxChildren()
  140. {
  141. $maxCilds = $this->_defaultConfig['maxChildren'];
  142. for ($i=0; $i<$maxCilds+1; $i++) {
  143. $filepath = $filepaths[] = tempnam("/tmp", __METHOD__);
  144. Tinebase_ActionQueue::getInstance()->queueAction('Tinebase.testSpy', $filepath, 10);
  145. }
  146. sleep(5);
  147. // collect states after 5 seconds of execution
  148. foreach($filepaths as $i => $filepath) {
  149. $counter[$i] = (int) file_get_contents($filepath);
  150. }
  151. // wait for children to finish
  152. sleep(20);
  153. $daemonLog = file_get_contents($this->_daemonLog);
  154. foreach($filepaths as $i => $filepath) {
  155. unlink($filepath);
  156. if ($i < $maxCilds) {
  157. $this->assertEquals(1, $counter[$i], "$i was not executed but should be \n$daemonLog");
  158. } else {
  159. $this->assertEquals(0, $counter[$i], "$i was executed but shouldn't \n$daemonLog");
  160. }
  161. }
  162. }
  163. }