1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 21:26:43 +02:00

Fixing the socket mess, maybe.

This commit is contained in:
Jordi Boggiano
2016-09-19 22:01:19 +02:00
parent a0f59df992
commit 3dc7a79a3e
9 changed files with 311 additions and 353 deletions

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Monolog\Test\TestCase;
use Monolog\Handler\SyslogUdp\UdpSocket;
use Monolog\Util\LocalSocket;
/**
* @requires extension sockets
@@ -23,10 +24,9 @@ class UdpSocketTest extends TestCase
{
$this->initSocket();
$socket = new UdpSocket('127.0.0.1', 51984);
$socket = new UdpSocket('127.0.0.1', 51983);
$socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
$this->closeSocket();
$this->assertEquals('HEADER: The quick brown fox jumps over the lazy dog', $this->socket->getOutput());
}
@@ -34,14 +34,12 @@ class UdpSocketTest extends TestCase
{
$this->initSocket();
$socket = new UdpSocket('127.0.0.1', 51984);
$socket = new UdpSocket('127.0.0.1', 51983);
$longString = str_repeat("derp", 20000);
$socket->write($longString, "HEADER");
$truncatedString = str_repeat("derp", 16254).'d';
$this->closeSocket();
$this->assertEquals('HEADER'.$truncatedString, $this->socket->getOutput());
}
@@ -64,43 +62,11 @@ class UdpSocketTest extends TestCase
private function initSocket()
{
$tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
file_put_contents($tmpFile, <<<'SCRIPT'
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, getprotobyname('udp'));
socket_bind($sock, '127.0.0.1', 51984);
echo 'INIT';
while (true) {
socket_recvfrom($sock, $read, 100*1024, 0, $ip, $port);
echo $read;
}
SCRIPT
);
$this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
$this->socket->start();
while (true) {
if ($this->socket->getOutput() === 'INIT') {
$this->socket->clearOutput();
break;
}
usleep(100);
}
}
private function closeSocket()
{
usleep(100);
$this->socket->stop();
$this->socket = LocalSocket::initSocket(51983, LocalSocket::UDP);
}
public function tearDown()
{
if (isset($this->socket)) {
$this->closeSocket();
unset($this->socket);
}
unset($this->socket, $this->handler);
}
}