Completed
Push — master ( 70b352...622f18 )
by Harry
02:46
created

StreamWriterTest::readSocket()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Graze\DogStatsD\Test\Integration\Stream;
4
5
use Graze\DogStatsD\Stream\StreamWriter;
6
use Graze\DogStatsD\Test\TestCase;
7
use ReflectionProperty;
8
9
class StreamWriterTest extends TestCase
10
{
11
    /** @var StreamWriter */
12
    private $writer;
13
    /** @var resource */
14
    private $socket;
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->writer = new StreamWriter('test', 'echo');
21
22
        $this->writer->write('');
23
24
        // get the socket
25
        $reflector = new ReflectionProperty(StreamWriter::class, 'socket');
26
        $reflector->setAccessible(true);
27
        /** @var resource $socket */
28
        $this->socket = $reflector->getValue($this->writer);
29
    }
30
31
    /**
32
     * @param int $len
33
     *
34
     * @return string
35
     */
36
    private function readSocket($len)
37
    {
38
        $time = microtime(true);
39
        $out = '';
40
        do {
41
            $out .= fread($this->socket, $len);
42
        } while (strlen($out) < $len && microtime(true) < $time + 1);
43
44
        return $out;
45
    }
46
47
    public function testEcho()
48
    {
49
        $this->assertTrue($this->writer->write('echo'));
50
        $out = $this->readSocket(4);
51
52
        $this->assertEquals('echo', $out);
53
    }
54
55
    public function testLongMessage()
56
    {
57
        $this->assertTrue($this->writer->write(str_repeat('x', 10000)));
58
        $out = $this->readSocket(10000);
59
60
        $this->assertEquals(10000, strlen($out));
61
        $this->assertEquals(str_repeat('x', 10000), $out);
62
    }
63
}
64