From 1a8f7c1e33444645969b38cc63cfe0d52f4da978 Mon Sep 17 00:00:00 2001 From: coderstephen Date: Mon, 10 Aug 2015 14:28:47 -0500 Subject: [PATCH] Add tests over multiple processes --- tests/Sync/SharedObjectTest.php | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Sync/SharedObjectTest.php b/tests/Sync/SharedObjectTest.php index 88b4126..3decc8a 100644 --- a/tests/Sync/SharedObjectTest.php +++ b/tests/Sync/SharedObjectTest.php @@ -76,4 +76,48 @@ class SharedObjectTest extends TestCase $clone->free(); $shared->free(); } + + /** + * @group posix + */ + public function testSetInSeparateProcess() + { + $object = new SharedObject(42); + + $this->doInFork(function () use ($object) { + $object->set(43); + }); + + $this->assertEquals(43, $object->deref()); + $object->free(); + } + + /** + * @group posix + */ + public function testFreeInSeparateProcess() + { + $object = new SharedObject(42); + + $this->doInFork(function () use ($object) { + $object->free(); + }); + + $this->assertTrue($object->isFreed()); + } + + private function doInFork(callable $function) + { + switch (pcntl_fork()) { + case -1: + $this->fail('Failed to fork process.'); + break; + case 0: + $status = (int)$function(); + exit(0); + default: + pcntl_wait($status); + return $status; + } + } }