1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is a part of the Phystrix Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2013-2015 oDesk Corporation. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* This file is licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
namespace Odesk\Bundle\PhystrixBundle\Tests\DependencyInjection; |
21
|
|
|
|
22
|
|
|
use Odesk\Bundle\PhystrixBundle\DependencyInjection\OdeskPhystrixExtension; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
24
|
|
|
|
25
|
|
|
class OdeskPhystrixExtensionTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var OdeskPhystrixExtension |
29
|
|
|
*/ |
30
|
|
|
private $extension; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
$this->extension = new OdeskPhystrixExtension(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function publicServicesNamesProvider() |
39
|
|
|
{ |
40
|
|
|
return array( |
41
|
|
|
array('phystrix.command_factory'), |
42
|
|
|
array('phystrix.service_locator'), |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $serviceName |
48
|
|
|
* @dataProvider publicServicesNamesProvider |
49
|
|
|
*/ |
50
|
|
|
public function testServiceIsPublic($serviceName) |
51
|
|
|
{ |
52
|
|
|
$container = new ContainerBuilder(); |
53
|
|
|
$this->extension->load(array(array('default' => array())), $container); |
54
|
|
|
|
55
|
|
|
$this->assertTrue($container->hasDefinition($serviceName), "Service $serviceName must be defined"); |
56
|
|
|
$definition = $container->getDefinition($serviceName); |
57
|
|
|
$this->assertTrue($definition->isPublic(), "Service $serviceName must be public"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testDefaultConfig() |
61
|
|
|
{ |
62
|
|
|
$container = new ContainerBuilder(); |
63
|
|
|
$this->extension->load(array(array('default' => array())), $container); |
64
|
|
|
|
65
|
|
|
$configArrayAll = $container->getParameter('phystrix.configuration.data'); |
66
|
|
|
|
67
|
|
|
$this->assertArrayHasKey('default', $configArrayAll); |
68
|
|
|
|
69
|
|
|
$defaultConfigArray = $configArrayAll['default']; |
70
|
|
|
|
71
|
|
|
// fallback |
72
|
|
|
$this->assertArrayHasKey('fallback', $defaultConfigArray); |
73
|
|
|
$this->assertEquals(false, $defaultConfigArray['fallback']['enabled']); |
74
|
|
|
|
75
|
|
|
// requestCache |
76
|
|
|
$this->assertArrayHasKey('requestCache', $defaultConfigArray); |
77
|
|
|
$this->assertEquals(true, $defaultConfigArray['requestCache']['enabled']); |
78
|
|
|
|
79
|
|
|
// requestLog |
80
|
|
|
$this->assertArrayHasKey('requestLog', $defaultConfigArray); |
81
|
|
|
$this->assertEquals(false, $defaultConfigArray['fallback']['enabled']); |
82
|
|
|
|
83
|
|
|
// circuitBreaker |
84
|
|
|
$this->assertArrayHasKey('circuitBreaker', $defaultConfigArray); |
85
|
|
|
$circuitBreakerConfigArray = $defaultConfigArray['circuitBreaker']; |
86
|
|
|
|
87
|
|
|
$this->assertArrayHasKey('errorThresholdPercentage', $circuitBreakerConfigArray); |
88
|
|
|
$this->assertEquals(50, $circuitBreakerConfigArray['errorThresholdPercentage']); |
89
|
|
|
|
90
|
|
|
$this->assertArrayHasKey('requestVolumeThreshold', $circuitBreakerConfigArray); |
91
|
|
|
$this->assertEquals(20, $circuitBreakerConfigArray['requestVolumeThreshold']); |
92
|
|
|
|
93
|
|
|
$this->assertArrayHasKey('sleepWindowInMilliseconds', $circuitBreakerConfigArray); |
94
|
|
|
$this->assertEquals(5000, $circuitBreakerConfigArray['sleepWindowInMilliseconds']); |
95
|
|
|
|
96
|
|
|
$this->assertArrayHasKey('forceOpen', $circuitBreakerConfigArray); |
97
|
|
|
$this->assertFalse($circuitBreakerConfigArray['forceOpen']); |
98
|
|
|
|
99
|
|
|
$this->assertArrayHasKey('forceClosed', $circuitBreakerConfigArray); |
100
|
|
|
$this->assertFalse($circuitBreakerConfigArray['forceClosed']); |
101
|
|
|
|
102
|
|
|
// metrics |
103
|
|
|
$this->assertArrayHasKey('metrics', $defaultConfigArray); |
104
|
|
|
$metricsConfigArray = $defaultConfigArray['metrics']; |
105
|
|
|
|
106
|
|
|
$this->assertArrayHasKey('healthSnapshotIntervalInMilliseconds', $metricsConfigArray); |
107
|
|
|
$this->assertEquals(1000, $metricsConfigArray['healthSnapshotIntervalInMilliseconds']); |
108
|
|
|
|
109
|
|
|
$this->assertArrayHasKey('rollingStatisticalWindowInMilliseconds', $metricsConfigArray); |
110
|
|
|
$this->assertEquals(1000, $metricsConfigArray['rollingStatisticalWindowInMilliseconds']); |
111
|
|
|
|
112
|
|
|
$this->assertArrayHasKey('rollingStatisticalWindowBuckets', $metricsConfigArray); |
113
|
|
|
$this->assertEquals(10, $metricsConfigArray['rollingStatisticalWindowBuckets']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testChangedConfig() |
117
|
|
|
{ |
118
|
|
|
$changedConfig = array( |
119
|
|
|
'fallback' => true, |
120
|
|
|
'requestCache' => false, |
121
|
|
|
'requestLog' => true, |
122
|
|
|
'circuitBreaker' => array( |
123
|
|
|
'errorThresholdPercentage' => 101, |
124
|
|
|
'forceOpen' => true, |
125
|
|
|
'forceClosed' => true, |
126
|
|
|
'requestVolumeThreshold' => 102, |
127
|
|
|
'sleepWindowInMilliseconds' => 103, |
128
|
|
|
), |
129
|
|
|
'metrics' => array( |
130
|
|
|
'healthSnapshotIntervalInMilliseconds' => 104, |
131
|
|
|
'rollingStatisticalWindowInMilliseconds' => 105, |
132
|
|
|
'rollingStatisticalWindowBuckets' => 106, |
133
|
|
|
), |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$container = new ContainerBuilder(); |
137
|
|
|
$this->extension->load(array(array('default' => $changedConfig)), $container); |
138
|
|
|
|
139
|
|
|
$configArrayAll = $container->getParameter('phystrix.configuration.data'); |
140
|
|
|
|
141
|
|
|
$this->assertArrayHasKey('default', $configArrayAll); |
142
|
|
|
|
143
|
|
|
$defaultConfigArray = $configArrayAll['default']; |
144
|
|
|
|
145
|
|
|
// fallback |
146
|
|
|
$this->assertArrayHasKey('fallback', $defaultConfigArray); |
147
|
|
|
$this->assertEquals(true, $defaultConfigArray['fallback']['enabled']); |
148
|
|
|
|
149
|
|
|
// requestCache |
150
|
|
|
$this->assertArrayHasKey('requestCache', $defaultConfigArray); |
151
|
|
|
$this->assertEquals(false, $defaultConfigArray['requestCache']['enabled']); |
152
|
|
|
|
153
|
|
|
// requestLog |
154
|
|
|
$this->assertArrayHasKey('requestLog', $defaultConfigArray); |
155
|
|
|
$this->assertEquals(true, $defaultConfigArray['fallback']['enabled']); |
156
|
|
|
|
157
|
|
|
// circuitBreaker |
158
|
|
|
$this->assertArrayHasKey('circuitBreaker', $defaultConfigArray); |
159
|
|
|
$circuitBreakerConfigArray = $defaultConfigArray['circuitBreaker']; |
160
|
|
|
|
161
|
|
|
$this->assertArrayHasKey('errorThresholdPercentage', $circuitBreakerConfigArray); |
162
|
|
|
$this->assertEquals(101, $circuitBreakerConfigArray['errorThresholdPercentage']); |
163
|
|
|
|
164
|
|
|
$this->assertArrayHasKey('requestVolumeThreshold', $circuitBreakerConfigArray); |
165
|
|
|
$this->assertEquals(102, $circuitBreakerConfigArray['requestVolumeThreshold']); |
166
|
|
|
|
167
|
|
|
$this->assertArrayHasKey('sleepWindowInMilliseconds', $circuitBreakerConfigArray); |
168
|
|
|
$this->assertEquals(103, $circuitBreakerConfigArray['sleepWindowInMilliseconds']); |
169
|
|
|
|
170
|
|
|
$this->assertArrayHasKey('forceOpen', $circuitBreakerConfigArray); |
171
|
|
|
$this->assertTrue($circuitBreakerConfigArray['forceOpen']); |
172
|
|
|
|
173
|
|
|
$this->assertArrayHasKey('forceClosed', $circuitBreakerConfigArray); |
174
|
|
|
$this->assertTrue($circuitBreakerConfigArray['forceClosed']); |
175
|
|
|
|
176
|
|
|
// metrics |
177
|
|
|
$this->assertArrayHasKey('metrics', $defaultConfigArray); |
178
|
|
|
$metricsConfigArray = $defaultConfigArray['metrics']; |
179
|
|
|
|
180
|
|
|
$this->assertArrayHasKey('healthSnapshotIntervalInMilliseconds', $metricsConfigArray); |
181
|
|
|
$this->assertEquals(104, $metricsConfigArray['healthSnapshotIntervalInMilliseconds']); |
182
|
|
|
|
183
|
|
|
$this->assertArrayHasKey('rollingStatisticalWindowInMilliseconds', $metricsConfigArray); |
184
|
|
|
$this->assertEquals(105, $metricsConfigArray['rollingStatisticalWindowInMilliseconds']); |
185
|
|
|
|
186
|
|
|
$this->assertArrayHasKey('rollingStatisticalWindowBuckets', $metricsConfigArray); |
187
|
|
|
$this->assertEquals(106, $metricsConfigArray['rollingStatisticalWindowBuckets']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
192
|
|
|
*/ |
193
|
|
|
public function testConfigMustHaveDefault() |
194
|
|
|
{ |
195
|
|
|
$container = new ContainerBuilder(); |
196
|
|
|
$this->extension->load(array(array()), $container); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|