Initial commit

This commit is contained in:
2018-04-15 16:41:46 +02:00
commit 86ff40274b
29 changed files with 1368 additions and 0 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace NoccyLabs\Ipc\Interop\Channel;
class StreamChannelTest extends \PhpUnit\Framework\TestCase
{
public function testCreatingFromStreams()
{
$fd = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$sc1 = new StreamChannel($fd[0]);
$sc2 = new StreamChannel($fd[1]);
$frame = "Hello World";
$sc1->send($frame);
$rcvd = $sc2->receive();
$this->assertEquals($frame, $rcvd);
}
public function testCreatingPair()
{
[ $sc1, $sc2 ] = StreamChannel::createPair();
$frame = "Hello World";
$sc1->send($frame);
$rcvd = $sc2->receive();
$this->assertEquals($frame, $rcvd);
}
}

39
tests/Key/FileKeyTest.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace NoccyLabs\Ipc\Key;
class FileKeyTest extends \PhpUnit\Framework\TestCase
{
public function testKeyGenerationFromExistingFile()
{
$keyfile = new FileKey(__FILE__);
$key = $keyfile->getKey();
$this->assertGreaterThan(0, $key);
}
public function testKeyGenerationFromTemporaryFile()
{
$tempfile = "/tmp/key.tmp";
file_exists($tempfile) && unlink($tempfile);
$keyfile = new FileKey($tempfile, "a", true);
$key = $keyfile->getKey();
$this->assertGreaterThan(0, $key);
}
public function testCloningShouldIncreaseProject()
{
$keyfile = new FileKey(__FILE__, "a");
$this->assertEquals("a", $keyfile->getProjectIdentifier());
$keyfile2 = clone $keyfile;
$this->assertEquals("b", $keyfile2->getProjectIdentifier());
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace NoccyLabs\Ipc\Lock;
class FileLockTest extends \PhpUnit\Framework\TestCase
{
public function testTheLockingShouldBeExclusive()
{
$lock1 = new FileLock(__FILE__);
$lock2 = new FileLock(__FILE__);
$this->assertEquals(true, $lock1->acquire(0));
$this->assertEquals(false, $lock2->acquire(0));
}
}

70
tests/Msg/QueueTest.php Normal file
View File

@ -0,0 +1,70 @@
<?php
namespace NoccyLabs\Ipc\Msg;
use NoccyLabs\Ipc\Key\FileKey;
class QueueTest extends \PhpUnit\Framework\TestCase
{
public function testSendingAndReceiving()
{
$key = new FileKey(__FILE__);
$queue = new Queue($key);
$queue->send(1, "Hello World");
$ret = $queue->receive(1, $type);
$this->assertEquals("Hello World", $ret);
$this->assertEquals(1, $type);
}
public function testSendingAndReceivingWithTypes()
{
$key = new FileKey(__FILE__);
$queue = new Queue($key);
$queue->send(1, "Hello World");
$queue->send(2, "Hello World");
$queue->send(3, "Hello World");
$ret = $queue->receive(1, $type);
$this->assertEquals(1, $type);
$ret = $queue->receive(2, $type);
$this->assertEquals(2, $type);
$ret = $queue->receive(3, $type);
$this->assertEquals(3, $type);
}
public function testReceivingFromFront()
{
$key = new FileKey(__FILE__);
$queue = new Queue($key);
$queue->send(1, "Hello World");
$queue->send(2, "Hello World");
$queue->send(3, "Hello World");
$ret = $queue->receive(0, $type);
$this->assertEquals(1, $type);
$ret = $queue->receive(0, $type);
$this->assertEquals(2, $type);
$ret = $queue->receive(0, $type);
$this->assertEquals(3, $type);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace NoccyLabs\Ipc\Shm;
use NoccyLabs\Ipc\Key\FileKey;
class SharedDataTest extends \PhpUnit\Framework\TestCase
{
public function testOpeningSharedData()
{
$key = new FileKey(__DIR__);
$shm = new SharedData($key);
$this->assertNull($shm->get("foo"));
$this->assertTrue($shm->set("foo", "hello"));
$this->assertEquals("hello", $shm->get("foo"));
$this->assertNull($shm->get("bar"));
$this->assertTrue($shm->set("bar", "world"));
$this->assertEquals("hello", $shm->get("foo"));
$this->assertEquals("world", $shm->get("bar"));
$shm->destroy();
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class SignalHandlerTest extends \PhpUnit\Framework\TestCase
{
public function testSignalHandlerWithSingleHandlerInConstructor()
{
$callbacks = [
function () use (&$handler1) { $handler1 = true; }
];
$handler = new SignalHandler(SIGUSR1, $callbacks);
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $handler1);
}
public function testSignalHandlerWithMultipleHandlersInConstructor()
{
$callbacks = [
function () use (&$handler1) { $handler1 = true; },
function () use (&$handler2) { $handler2 = true; },
function () use (&$handler3) { $handler3 = true; }
];
$handler = new SignalHandler(SIGUSR1, $callbacks);
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $handler1);
$this->assertEquals(true, $handler2);
$this->assertEquals(true, $handler3);
}
public function testSignalHandlerWithAddedHandlers()
{
$callbacks = [
function () use (&$handler1) { $handler1 = true; },
function () use (&$handler2) { $handler2 = true; }
];
$handler = new SignalHandler(SIGUSR1, $callbacks);
$handler->addHandler(function () use (&$handler3) { $handler3 = true; });
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $handler1);
$this->assertEquals(true, $handler2);
$this->assertEquals(true, $handler3);
}
public function testSignalHandlerWithBlockingHandler()
{
$callbacks = [
function () use (&$handler1) { $handler1 = true; return true; },
function () use (&$handler2) { $handler2 = true; },
function () use (&$handler3) { $handler3 = true; }
];
$handler = new SignalHandler(SIGUSR1, $callbacks);
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $handler1);
$this->assertEquals(false, $handler2);
$this->assertEquals(false, $handler3);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class SignalTest extends \PhpUnit\Framework\TestCase
{
public function testSimpleSignalHandler()
{
$callback = function () use (&$handler1) { $handler1 = true; };
$handler = new Signal(SIGUSR1);
$handler->setHandler($callback);
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $handler1);
}
public function testSimpleSignalDispatcher()
{
$callback = function () use (&$handler1) { $handler1 = true; };
$handler = new Signal(SIGUSR1);
$handler->setHandler($callback);
$handler->dispatch(posix_getpid());
usleep(1000);
$this->assertEquals(true, $handler1);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class SignalTrapTest extends \PhpUnit\Framework\TestCase
{
public function testSignalTrap()
{
$trap = new SignalTrap(SIGUSR1);
$this->assertEquals(false, $trap->isTrapped());
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $trap->isTrapped());
$this->assertEquals(false, $trap->isTrapped());
}
public function testSignalTrapWithoutResetting()
{
$trap = new SignalTrap(SIGUSR1);
$this->assertEquals(false, $trap->isTrapped());
posix_kill(posix_getpid(), SIGUSR1);
usleep(1000);
$this->assertEquals(true, $trap->isTrapped(false));
$this->assertEquals(true, $trap->isTrapped());
}
}