Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/LoadMonitorTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use Wikimedia\ObjectCache\HashBagOStuff;
22 use Wikimedia\ObjectCache\WANObjectCache;
23 use Wikimedia\Rdbms\DBUnexpectedError;
24 use Wikimedia\Rdbms\FakeResultWrapper;
25 use Wikimedia\Rdbms\IDatabase;
26 use Wikimedia\Rdbms\ILoadBalancer;
27 use Wikimedia\Rdbms\LoadMonitor;
28
29 /**
30 * @covers \Wikimedia\Rdbms\LoadMonitor
31 */
32 class LoadMonitorTest extends MediaWikiUnitTestCase {
33 public function testWithReplica() {
34 $dbArray = $this->newMultiServerLocalLoadBalancer();
35 $dbs = [];
36 foreach ( $dbArray as $i => $info ) {
37 $dbMock = $this->createMock( IDatabase::class );
38 $dbMock->method( 'getType' )
39 ->willReturn( 'mysql' );
40 $dbMock->method( 'query' )
41 ->willReturn( new FakeResultWrapper( [ [ 'pcount' => $i * 50 ] ] ) );
42 $dbs[] = $dbMock;
43 }
44 $lbMock = $this->createMock( ILoadBalancer::class );
45 $lbMock->method( 'getServerInfo' )
46 ->willReturnCallback( static function ( $i ) use ( $dbArray ) {
47 return $dbArray[$i] ?? false;
48 } );
49 $lbMock->method( 'getServerName' )
50 ->willReturnCallback( static function ( $i ) use ( $dbArray ) {
51 return $dbArray[$i]['serverName'] ?? false;
52 } );
53
54 $lbMock->method( 'getServerConnection' )
55 ->willReturnCallback( static function ( $i ) use ( $dbs ) {
56 return $dbs[$i];
57 } );
58
59 $lbMock->method( 'getClusterName' )
60 ->willReturn( 'test-cluster' );
61
62 $wanMock = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
63
64 $loadMonitor = new LoadMonitor( $lbMock, new HashBagOStuff(), $wanMock, [] );
65
66 $weights = [ 0, 100, 100, 100, 100 ];
67 $loadMonitor->scaleLoads( $weights );
68 $this->assertSame( [ 0, 130, 111, 91, 72 ], $weights );
69 }
70
71 public function testCircuitBreaking() {
72 $dbArray = $this->newMultiServerLocalLoadBalancer();
73 $dbs = [];
74 foreach ( $dbArray as $i => $info ) {
75 $dbMock = $this->createMock( IDatabase::class );
76 $dbMock->method( 'getType' )
77 ->willReturn( 'mysql' );
78 $dbMock->method( 'query' )
79 ->willReturn( new FakeResultWrapper( [ [ 'pcount' => $i * 500 ] ] ) );
80 $dbs[] = $dbMock;
81 }
82 $lbMock = $this->createMock( ILoadBalancer::class );
83 $lbMock->method( 'getServerInfo' )
84 ->willReturnCallback( static function ( $i ) use ( $dbArray ) {
85 return $dbArray[$i] ?? false;
86 } );
87 $lbMock->method( 'getServerName' )
88 ->willReturnCallback( static function ( $i ) use ( $dbArray ) {
89 return $dbArray[$i]['serverName'] ?? false;
90 } );
91
92 $lbMock->method( 'getServerConnection' )
93 ->willReturnCallback( static function ( $i ) use ( $dbs ) {
94 return $dbs[$i];
95 } );
96
97 $lbMock->method( 'getClusterName' )
98 ->willReturn( 'test-cluster' );
99
100 $wanMock = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
101
102 $loadMonitor = new LoadMonitor( $lbMock, new HashBagOStuff(), $wanMock, [ 'maxConnCount' => 400 ] );
103
104 $weights = [ 0, 100, 100, 100, 100 ];
105 $this->expectException( DBUnexpectedError::class );
106 $loadMonitor->scaleLoads( $weights );
107 }
108
109 private function newMultiServerLocalLoadBalancer() {
110 $DBserver = 'localhost';
111 $DBport = '3306';
112 $DBname = 'wikidb';
113 $DBuser = 'wikiuser';
114 $DBpassword = 'wikidbpassword';
115 $DBtype = 'mysql';
116 $prefix = '';
117
118 return [
119 // Primary DB
120 0 => [
121 'serverName' => 'db0',
122 'host' => $DBserver,
123 'port' => $DBport,
124 'dbname' => $DBname,
125 'tablePrefix' => $prefix,
126 'user' => $DBuser,
127 'password' => $DBpassword,
128 'type' => $DBtype,
129 'flags' => DBO_TRX,
130 'load' => 0,
131 ],
132 1 => [
133 'serverName' => 'db1',
134 'host' => $DBserver,
135 'port' => $DBport,
136 'dbname' => $DBname,
137 'tablePrefix' => $prefix,
138 'user' => $DBuser,
139 'password' => $DBpassword,
140 'type' => $DBtype,
141 'flags' => DBO_TRX,
142 'load' => 100,
143 ],
144 2 => [
145 'serverName' => 'db2',
146 'host' => $DBserver,
147 'port' => $DBport,
148 'dbname' => $DBname,
149 'tablePrefix' => $prefix,
150 'user' => $DBuser,
151 'password' => $DBpassword,
152 'type' => $DBtype,
153 'flags' => DBO_TRX,
154 'load' => 100,
155 ],
156 3 => [
157 'serverName' => 'db3',
158 'host' => $DBserver,
159 'port' => $DBport,
160 'dbname' => $DBname,
161 'tablePrefix' => $prefix,
162 'user' => $DBuser,
163 'password' => $DBpassword,
164 'type' => $DBtype,
165 'load' => 100,
166 'flags' => DBO_TRX,
167 ],
168 4 => [
169 'serverName' => 'db4',
170 'host' => $DBserver,
171 'port' => $DBport,
172 'dbname' => $DBname,
173 'tablePrefix' => $prefix,
174 'user' => $DBuser,
175 'password' => $DBpassword,
176 'type' => $DBtype,
177 'load' => 100,
178 'flags' => DBO_TRX,
179 ],
180 ];
181 }
182 }