From 49e96bfd50c53144bad1977c9d9148bda7fd7303 Mon Sep 17 00:00:00 2001 From: Hill Date: Sun, 21 Mar 2021 19:54:09 +0800 Subject: [PATCH] update supervisor --- .gitignore | 1 + docker-compose.yml | 8 ++ include_test.php | 23 ++++++ phpunit.sh | 12 +++ phpunit.xml | 6 +- src/Signal.php | 4 +- supervisor.php | 4 +- test.php => tests-legacy/SupervisorTest.php | 4 +- tests/SupervisorTest.php | 84 +++++++++++++++++++++ 9 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yml create mode 100644 include_test.php create mode 100755 phpunit.sh rename test.php => tests-legacy/SupervisorTest.php (94%) create mode 100644 tests/SupervisorTest.php diff --git a/.gitignore b/.gitignore index 400294a..721877b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor composer.lock .php_cs.cache +.phpunit.result.cache .*.sw? diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9a29fe0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: "3" + +services: + phpunit: + image: hillliu/pmvc-phpunit:5.6 + volumes: + - .:/var/www/html + - .:/var/www/supervisor diff --git a/include_test.php b/include_test.php new file mode 100644 index 0000000..f6565df --- /dev/null +++ b/include_test.php @@ -0,0 +1,23 @@ +assertStringContainsString($needle, $haystack); + } else { + $this->assertContains($needle, $haystack); + } + } +} diff --git a/phpunit.sh b/phpunit.sh new file mode 100755 index 0000000..e939220 --- /dev/null +++ b/phpunit.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +DIR="$( cd "$(dirname "$0")" ; pwd -P )" +PLUGIN_NAME="supervisor" + +docker run --rm \ + -v $DIR:/var/www/html \ + -v $DIR:/var/www/${PLUGIN_NAME} \ + --name phpunit hillliu/pmvc-phpunit:5.6 \ + phpunit --no-configuration --bootstrap ./include_test.php ./tests-legacy/ + +# docker-compose run --rm phpunit phpunit --no-configuration --bootstrap ./include_test.php ./tests-legacy/test.php diff --git a/phpunit.xml b/phpunit.xml index e58818f..0fbb0af 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,16 +2,16 @@ + > - test.php + ./tests/ diff --git a/src/Signal.php b/src/Signal.php index 7f77879..f735d7e 100644 --- a/src/Signal.php +++ b/src/Signal.php @@ -1,12 +1,14 @@ _createPidFile(); } } - \PMVC\l(__DIR__.'/src/Monitor.php'); foreach ($this[CALLBACKS] as $callbackId=>$callback) { $trigger = \PMVC\get($callback, TRIGGER); if (strlen($trigger)) { @@ -107,6 +106,7 @@ public function process(callable $monitorCallBack = null) $this->start($callbackId); } } + \PMVC\l(__DIR__.'/src/Monitor.php'); new Monitor($monitorCallBack); } } diff --git a/test.php b/tests-legacy/SupervisorTest.php similarity index 94% rename from test.php rename to tests-legacy/SupervisorTest.php index d6b04f7..50ac8b1 100644 --- a/test.php +++ b/tests-legacy/SupervisorTest.php @@ -1,7 +1,5 @@ _plug); + } + + function testPlugin() + { + ob_start(); + print_r(PMVC\plug($this->_plug)); + $output = ob_get_contents(); + ob_end_clean(); + $this->haveString($this->_plug,$output); + } + + function testScript() + { + $plug = PMVC\plug($this->_plug); + $s = 'helloScript'; + @$plug->script(new fakeChild(), array($s, 0)); + $self = $this; + @$plug->process(function() use($plug, $self, $s){ + $plug->stop(); + $self->assertEquals($s,$plug['callbacks'][0]['args'][0]); + }); + } + + function testDaemon() + { + $s = 'helloDaemon'; + $plug = PMVC\plug($this->_plug); + @$plug->daemon(new fakeDaemon(), [$s, 1]); + @$plug->process(function() use ($plug) { + $plug->forceStop(); + $this->assertEquals('daemon',$plug['callbacks'][0]['type']); + }); + } + + function testTrigger() + { + $plug = PMVC\plug($this->_plug); + $s = 'helloTrigger'; + $self = $this; + @$childKey = $plug->script(new fakeChild(), array($s.'3', 3)); + @$second = $plug->script(new fakeChild(), array($s.'4', 4), $childKey); + @$third = $plug->script(new fakeChild(), array($s.'5', 5), $second); + @$plug->process(function($callbackId, $pid) use($plug, $self, $second){ + static $i = 0; + if (!$i) { + $self->assertTrue(empty($plug['callbacks'][$second]['startTime']), 'Test first'); + } else { + $self->assertFalse(empty($plug['callbacks'][$second]['startTime']), 'Test second'); + } + $i++; + }); + } +} + +class fakeChild +{ + function __invoke($s, $exit) + { + echo $s."\n"; + exit($exit); + } +} + +/** + * fakeDaemon without exit + */ +class fakeDaemon +{ + function __invoke($s, $exit) + { + echo $s."\n"; + } +}