1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Test\Manager; |
4
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Event\Events; |
6
|
|
|
use Jalle19\StatusManager\Event\InstanceStateEvent; |
7
|
|
|
use Jalle19\StatusManager\Event\InstanceStatusCollectionRequestEvent; |
8
|
|
|
use Jalle19\StatusManager\Instance\InstanceState; |
9
|
|
|
use Jalle19\StatusManager\Manager\InstanceStateManager; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class InstanceStateManagerTest |
14
|
|
|
* @package Jalle19\StatusManager\Test\Manager |
15
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
16
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
17
|
|
|
*/ |
18
|
|
|
class InstanceStateManagerTest extends AbstractManagerTest |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Mocks the instance state manager to test that events are actually handled |
23
|
|
|
*/ |
24
|
|
|
public function testEventHandling() |
25
|
|
|
{ |
26
|
|
|
/* @var EventSubscriberInterface|\PHPUnit_Framework_MockObject_MockObject|InstanceStateManager $instanceStateManager */ |
27
|
|
|
$instanceStateManager = $this->getMockBuilder('\Jalle19\StatusManager\Manager\InstanceStateManager') |
|
|
|
|
28
|
|
|
->setConstructorArgs([$this->configuration, $this->logger, $this->eventDispatcher]) |
29
|
|
|
->setMethods([ |
30
|
|
|
'onInstanceStatusCollectionRequest', |
31
|
|
|
'onInstanceReachable', |
32
|
|
|
'onInstanceUnreachable', |
33
|
|
|
'onInstanceMaybeReachable', |
34
|
|
|
]) |
35
|
|
|
->getMock(); |
36
|
|
|
|
37
|
|
|
$instanceStateManager->expects($this->once()) |
|
|
|
|
38
|
|
|
->method('onInstanceStatusCollectionRequest'); |
39
|
|
|
$instanceStateManager->expects($this->once()) |
40
|
|
|
->method('onInstanceReachable'); |
41
|
|
|
$instanceStateManager->expects($this->once()) |
42
|
|
|
->method('onInstanceUnreachable'); |
43
|
|
|
$instanceStateManager->expects($this->once()) |
44
|
|
|
->method('onInstanceMaybeReachable'); |
45
|
|
|
|
46
|
|
|
// Start triggering events and assert that the instance state is as expected |
47
|
|
|
$this->eventDispatcher->addSubscriber($instanceStateManager); |
48
|
|
|
|
49
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATUS_COLLECTION_REQUEST, |
50
|
|
|
new InstanceStatusCollectionRequestEvent()); |
51
|
|
|
|
52
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE, |
53
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
54
|
|
|
|
55
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_UNREACHABLE, |
56
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
57
|
|
|
|
58
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_MAYBE_REACHABLE, |
59
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests that instance state events are reflected properly in the actual instance stateF |
65
|
|
|
*/ |
66
|
|
|
public function testInstanceStateHandling() |
67
|
|
|
{ |
68
|
|
|
$instanceStateManager = new InstanceStateManager($this->configuration, $this->logger, $this->eventDispatcher); |
69
|
|
|
$this->eventDispatcher->addSubscriber($instanceStateManager); |
70
|
|
|
|
71
|
|
|
// Assert that triggered events actually trigger a change in state |
72
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE, |
73
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
74
|
|
|
|
75
|
|
|
// Assert that the instance is now reachable |
76
|
|
|
$this->assertEquals(InstanceState::REACHABLE, |
77
|
|
|
$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability()); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable()); |
80
|
|
|
|
81
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_UNREACHABLE, |
82
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
83
|
|
|
|
84
|
|
|
// Assert that the instance is now unreachable |
85
|
|
|
$this->assertEquals(InstanceState::UNREACHABLE, |
86
|
|
|
$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability()); |
87
|
|
|
|
88
|
|
|
$this->assertFalse($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable()); |
89
|
|
|
|
90
|
|
|
// The instance should be maybe reachable after a full retry cycle |
91
|
|
|
for ($i = 0; $i < InstanceStateManager::UNREACHABLE_CYCLES_UNTIL_RETRY; $i++) |
92
|
|
|
{ |
93
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_MAYBE_REACHABLE, |
94
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->assertEquals(InstanceState::MAYBE_REACHABLE, |
98
|
|
|
$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability()); |
99
|
|
|
|
100
|
|
|
$this->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable()); |
101
|
|
|
|
102
|
|
|
// And now if we mark it reachable it should be that |
103
|
|
|
$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE, |
104
|
|
|
new InstanceStateEvent($this->getTestInstance())); |
105
|
|
|
|
106
|
|
|
$this->assertEquals(InstanceState::REACHABLE, |
107
|
|
|
$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability()); |
108
|
|
|
|
109
|
|
|
$this->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.