summaryrefslogtreecommitdiff
path: root/plugins/broadcast/root/tests/Broadcast/BroadcastTest.php
blob: d6014bf00b78aeec0ca4ffdcc341c8b2e06a125e (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
<?php

/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    Anton Titov (Wolfy-J)
 */

declare(strict_types=1);

namespace Spiral\Broadcast\Tests;

use PHPUnit\Framework\TestCase;
use Spiral\Broadcast\Broadcast;
use Spiral\Broadcast\Exception\BroadcastException;
use Spiral\Broadcast\Message;
use Spiral\Goridge\RPC;
use Spiral\Goridge\SocketRelay;

class BroadcastTest extends TestCase
{
    public function testBroadcast(): void
    {
        $rpc = new RPC(new SocketRelay('localhost', 6001));
        $br = new Broadcast($rpc);

        $br->publish(
            new Message('tests/topic', 'hello'),
            new Message('tests/123', ['key' => 'value'])
        );

        while (filesize(__DIR__ . '/../log.txt') < 40) {
            clearstatcache(true, __DIR__ . '/../log.txt');
            usleep(1000);
        }

        clearstatcache(true, __DIR__ . '/../log.txt');
        $content = file_get_contents(__DIR__ . '/../log.txt');

        $this->assertSame('tests/topic: "hello"
tests/123: {"key":"value"}
', $content);
    }

    public function testBroadcastException(): void
    {
        $rpc = new RPC(new SocketRelay('localhost', 6002));
        $br = new Broadcast($rpc);

        $this->expectException(BroadcastException::class);
        $br->publish(
            new Message('topic', 'hello')
        );
    }
}