Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Tests\Socket;
4
 
5
use React\Socket\Server;
6
use React\EventLoop\StreamSelectLoop;
7
 
8
class ServerTest extends TestCase
9
{
10
    private $loop;
11
    private $server;
12
    private $port;
13
 
14
    private function createLoop()
15
    {
16
        return new StreamSelectLoop();
17
    }
18
 
19
    /**
20
     * @covers React\Socket\Server::__construct
21
     * @covers React\Socket\Server::listen
22
     * @covers React\Socket\Server::getPort
23
     */
24
    public function setUp()
25
    {
26
        $this->loop = $this->createLoop();
27
        $this->server = new Server($this->loop);
28
        $this->server->listen(0);
29
 
30
        $this->port = $this->server->getPort();
31
    }
32
 
33
    /**
34
     * @covers React\EventLoop\StreamSelectLoop::tick
35
     * @covers React\Socket\Server::handleConnection
36
     * @covers React\Socket\Server::createConnection
37
     */
38
    public function testConnection()
39
    {
40
        $client = stream_socket_client('tcp://localhost:'.$this->port);
41
 
42
        $this->server->on('connection', $this->expectCallableOnce());
43
        $this->loop->tick();
44
    }
45
 
46
    /**
47
     * @covers React\EventLoop\StreamSelectLoop::tick
48
     * @covers React\Socket\Server::handleConnection
49
     * @covers React\Socket\Server::createConnection
50
     */
51
    public function testConnectionWithManyClients()
52
    {
53
        $client1 = stream_socket_client('tcp://localhost:'.$this->port);
54
        $client2 = stream_socket_client('tcp://localhost:'.$this->port);
55
        $client3 = stream_socket_client('tcp://localhost:'.$this->port);
56
 
57
        $this->server->on('connection', $this->expectCallableExactly(3));
58
        $this->loop->tick();
59
        $this->loop->tick();
60
        $this->loop->tick();
61
    }
62
 
63
    /**
64
     * @covers React\EventLoop\StreamSelectLoop::tick
65
     * @covers React\Socket\Connection::handleData
66
     */
67
    public function testDataWithNoData()
68
    {
69
        $client = stream_socket_client('tcp://localhost:'.$this->port);
70
 
71
        $mock = $this->expectCallableNever();
72
 
73
        $this->server->on('connection', function ($conn) use ($mock) {
74
            $conn->on('data', $mock);
75
        });
76
        $this->loop->tick();
77
        $this->loop->tick();
78
    }
79
 
80
    /**
81
     * @covers React\EventLoop\StreamSelectLoop::tick
82
     * @covers React\Socket\Connection::handleData
83
     */
84
    public function testData()
85
    {
86
        $client = stream_socket_client('tcp://localhost:'.$this->port);
87
 
88
        fwrite($client, "foo\n");
89
 
90
        $mock = $this->createCallableMock();
91
        $mock
92
            ->expects($this->once())
93
            ->method('__invoke')
94
            ->with("foo\n");
95
 
96
        $this->server->on('connection', function ($conn) use ($mock) {
97
            $conn->on('data', $mock);
98
        });
99
        $this->loop->tick();
100
        $this->loop->tick();
101
    }
102
 
103
    /**
104
     * Test data sent from python language
105
     *
106
     * @covers React\EventLoop\StreamSelectLoop::tick
107
     * @covers React\Socket\Connection::handleData
108
     */
109
    public function testDataSentFromPy()
110
    {
111
        $client = stream_socket_client('tcp://localhost:' . $this->port);
112
        fwrite($client, "foo\n");
113
        stream_socket_shutdown($client, STREAM_SHUT_WR);
114
 
115
        $mock = $this->createCallableMock();
116
        $mock
117
            ->expects($this->once())
118
            ->method('__invoke')
119
            ->with("foo\n");
120
 
121
 
122
        $this->server->on('connection', function ($conn) use ($mock) {
123
            $conn->on('data', $mock);
124
        });
125
        $this->loop->tick();
126
        $this->loop->tick();
127
    }
128
 
129
    public function testFragmentedMessage()
130
    {
131
        $client = stream_socket_client('tcp://localhost:' . $this->port);
132
 
133
        fwrite($client, "Hello World!\n");
134
 
135
        $mock = $this->createCallableMock();
136
        $mock
137
            ->expects($this->once())
138
            ->method('__invoke')
139
            ->with("He");
140
 
141
        $this->server->on('connection', function ($conn) use ($mock) {
142
            $conn->bufferSize = 2;
143
            $conn->on('data', $mock);
144
        });
145
        $this->loop->tick();
146
        $this->loop->tick();
147
    }
148
 
149
    /**
150
     * @covers React\EventLoop\StreamSelectLoop::tick
151
     */
152
    public function testDisconnectWithoutDisconnect()
153
    {
154
        $client = stream_socket_client('tcp://localhost:'.$this->port);
155
 
156
        $mock = $this->expectCallableNever();
157
 
158
        $this->server->on('connection', function ($conn) use ($mock) {
159
            $conn->on('end', $mock);
160
        });
161
        $this->loop->tick();
162
        $this->loop->tick();
163
    }
164
 
165
    /**
166
     * @covers React\EventLoop\StreamSelectLoop::tick
167
     * @covers React\Socket\Connection::end
168
     */
169
    public function testDisconnect()
170
    {
171
        $client = stream_socket_client('tcp://localhost:'.$this->port);
172
 
173
        fclose($client);
174
 
175
        $mock = $this->expectCallableOnce();
176
 
177
        $this->server->on('connection', function ($conn) use ($mock) {
178
            $conn->on('end', $mock);
179
        });
180
        $this->loop->tick();
181
        $this->loop->tick();
182
    }
183
 
184
    /**
185
     * @covers React\Socket\Server::shutdown
186
     */
187
    public function tearDown()
188
    {
189
        if ($this->server) {
190
            $this->server->shutdown();
191
        }
192
    }
193
}