summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/tests/Jaeger/ThriftUdpTransportTest.php
blob: abc4e32aa77f4852dc990a793103d6ec3b130371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

namespace Jaeger\Tests;

use Jaeger\Config;
use Jaeger\Tests\Logger\StackLogger;
use Jaeger\ThriftUdpTransport;
use Jaeger\Tracer;
use PHPUnit\Framework\TestCase;
use Thrift\Exception\TTransportException;

class ThriftUdpTransportTest extends TestCase
{
    /**
     * @var ThriftUdpTransport
     */
    private $transport;

    /**
     * @var StackLogger
     */
    private $logger;

    public function setUp(): void
    {
        $this->logger = new StackLogger();
        $this->transport = new ThriftUdpTransport('127.0.0.1', 12345, $this->logger);
    }

    public function testisOpenWhenOpen()
    {
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->assertTrue($this->transport->isOpen());
        $this->assertEquals($this->logger->getMessagesCount(), 0);
    }

    public function testisOpenWhenClosed()
    {
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->close();
        $this->assertFalse($this->transport->isOpen());
        $this->assertEquals($this->logger->getMessagesCount(), 0);
    }

    public function testClose()
    {
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->close();

        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->write('hello');
        $this->assertEquals($this->logger->getMessagesCount(), 1);
        $this->assertEquals($this->logger->getLastMessage(), 'transport is closed');
        $this->assertEquals($this->logger->getMessagesCount(), 0);
    }

    public function testDoubleClose()
    {
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->close();
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->close();
        $this->assertEquals($this->logger->getMessagesCount(), 1);
        $this->assertEquals(
            $this->logger->getLastMessage(),
            "can't close empty socket"
        );
    }

    public function testException()
    {
        $this->assertEquals($this->logger->getMessagesCount(), 0);
        $this->transport->open();
        $this->assertEquals($this->logger->getMessagesCount(), 0);

        $this->transport->write(str_repeat("some string", 10000));

        $this->assertEquals($this->logger->getMessagesCount(), 1);
        $msg = $this->logger->getLastMessage();
        $pattern = "/socket_write failed: \[code - \d+\] Message too long/";

        if (method_exists($this, "assertMatchesRegularExpression")) {
            $this->assertMatchesRegularExpression($pattern, $msg);
        } else {
            $this->assertRegExp($pattern, $msg);
        }
    }

    public function testProtocolVersionIPv4()
    {
        $config = new Config([
            Config::IP_VERSION => Config::IPV4
        ], "testServiceName");

        $transport = new ThriftUdpTransport('127.0.0.1', 12345, $this->logger, $config);

        $reflectionTransport = new \ReflectionClass($transport);
        $ipProtocolVersionMethod = $reflectionTransport->getMethod("ipProtocolVersion");
        $ipProtocolVersionMethod->setAccessible(true);

        $this->assertEquals(Config::IPV4, $ipProtocolVersionMethod->invoke($transport));
    }

    public function testProtocolVersionIPv6()
    {
        $config = new Config([
            Config::IP_VERSION => Config::IPV6
        ], "testServiceName");

        $transport = new ThriftUdpTransport('127.0.0.1', 12345, $this->logger, $config);
//
        $reflectionTransport = new \ReflectionClass($transport);
        $ipProtocolVersionMethod = $reflectionTransport->getMethod("ipProtocolVersion");
        $ipProtocolVersionMethod->setAccessible(true);
//
        $this->assertEquals(Config::IPV6, $ipProtocolVersionMethod->invoke($transport));
    }

    public function testProtocolVersionDefault()
    {
        $config = new Config([
        ], "testServiceName");

        $transport = new ThriftUdpTransport('127.0.0.1', 12345, $this->logger, $config);

        $reflectionTransport = new \ReflectionClass($transport);
        $ipProtocolVersionMethod = $reflectionTransport->getMethod("ipProtocolVersion");
        $ipProtocolVersionMethod->setAccessible(true);

        $this->assertEquals(Config::IPV4, $ipProtocolVersionMethod->invoke($transport));
    }

    public function testCreateSocket()
    {
        $transport = $this->getMockBuilder(ThriftUdpTransport::class)
            ->disableOriginalConstructor()
            ->getMock();

        $reflectionClass = new \ReflectionClass($transport);
        $method = $reflectionClass->getMethod("setLogger");
        $method->setAccessible(true);
        $method->invokeArgs($transport, [$this->logger]);

        $method = $reflectionClass->getMethod("createSocket");
        $method->setAccessible(true);
        $res = $method->invokeArgs($transport, [Config::IPV6]);

        $this->assertNotFalse($res);


        $transport = $this->getMockBuilder(ThriftUdpTransport::class)
            ->disableOriginalConstructor()
            ->getMock();

        $reflectionClass = new \ReflectionClass($transport);
        $method = $reflectionClass->getMethod("setLogger");
        $method->setAccessible(true);
        $method->invokeArgs($transport, [$this->logger]);

        $method = $reflectionClass->getMethod("createSocket");
        $method->setAccessible(true);
        $res = $method->invokeArgs($transport, [Config::IPV4]);

        $this->assertNotFalse($res);
    }
}