summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/ShutdownTest.php
blob: 93d6e32bd74c68777e0d212713dcd8b2c97794f0 (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
<?php

use Elastica\Test\Base as BaseTest;

/**
 * These tests shuts down node/cluster, so can't be executed with rest testsuite
 * Please use `sudo service elasticsearch restart` after every run of these tests.
 */
class ShutdownTest extends BaseTest
{
    /**
     * @group shutdown
     */
    public function testNodeShutdown()
    {
        // Get cluster nodes
        $client = $this->_getClient();
        $cluster = $client->getCluster();
        $nodes = $cluster->getNodes();

        $nodesCount = count($nodes);

        if ($nodesCount < 2) {
            $this->markTestIncomplete('At least two nodes have to be running, because 1 node is shutdown');
        }

        $portFound = false;
        // sayonara, wolverine, we'd never love you
        foreach ($nodes as $node) {
            if ((int) $node->getInfo()->getPort() === 9201) {
                $portFound = true;
                $node->shutdown('1s');
                break;
            }
        }

        if (!$portFound) {
            $this->markTestSkipped('This test was skipped as in the new docker environment all elasticsearch instances run on the same port');
        }

        // Wait until node is shutdown
        sleep(5);

        // Get nodes again
        $client = $this->_getClient();
        $cluster = $client->getCluster();
        $nodes = $cluster->getNodes();

        // Only one left
        $this->assertCount($nodesCount - 1, $nodes);
    }

    /**
     * @group shutdown
     * @depends testNodeShutdown
     * @expectedException \Elastica\Exception\Connection\HttpException
     */
    public function testClusterShutdown()
    {
        // Get cluster nodes
        $client = $this->_getClient();
        $cluster = $client->getCluster();
        $nodes = $cluster->getNodes();

        // Shutdown cluster
        $cluster->shutdown('1s');

        // Wait...
        sleep(5);

        // Now exception must be thrown
        $client->getStatus();
    }
}