Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
d56401c3
Commit
d56401c3
authored
Mar 13, 2012
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated loadbalancer plugin unittests
parent
f36394a4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
145 additions
and
141 deletions
+145
-141
tests/Solarium/Tests/Plugin/Loadbalancer/LoadbalancerTest.php
...s/Solarium/Tests/Plugin/Loadbalancer/LoadbalancerTest.php
+145
-141
No files found.
tests/Solarium/Tests/Plugin/Loadbalancer/LoadbalancerTest.php
View file @
d56401c3
...
...
@@ -50,13 +50,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
*/
protected
$client
;
protected
$serverOptions
=
array
(
'host'
=>
'nonexistinghostname'
);
public
function
setUp
()
{
$this
->
plugin
=
new
Loadbalancer
();
$this
->
client
=
new
Client
();
$options
=
array
(
'endpoint'
=>
array
(
'server1'
=>
array
(
'host'
=>
'host1'
),
'server2'
=>
array
(
'host'
=>
'host2'
),
),
);
$this
->
client
=
new
Client
(
$options
);
$adapter
=
$this
->
getMock
(
'Solarium\Core\Client\Adapter\Http'
);
$adapter
->
expects
(
$this
->
any
())
->
method
(
'execute'
)
...
...
@@ -68,19 +77,9 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
public
function
testConfigMode
()
{
$options
=
array
(
'server'
=>
array
(
'server1'
=>
array
(
'options'
=>
array
(
'host'
=>
'host1'
),
'weight'
=>
10
,
),
'server2'
=>
array
(
'options'
=>
array
(
'host'
=>
'host2'
),
'weight'
=>
5
,
),
'endpoint'
=>
array
(
'server1'
=>
10
,
'server2'
=>
5
,
),
'blockedquerytype'
=>
array
(
Client
::
QUERY_UPDATE
,
Client
::
QUERY_MORELIKETHIS
)
);
...
...
@@ -88,17 +87,8 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
plugin
->
setOptions
(
$options
);
$this
->
assertEquals
(
array
(
'server1'
=>
array
(
'options'
=>
array
(
'host'
=>
'host1'
),
'weight'
=>
10
,
),
'server2'
=>
array
(
'options'
=>
array
(
'host'
=>
'host2'
),
'weight'
=>
5
,
)
),
$this
->
plugin
->
getServers
()
array
(
'server1'
=>
10
,
'server2'
=>
5
),
$this
->
plugin
->
getEndpoints
()
);
$this
->
assertEquals
(
...
...
@@ -119,124 +109,139 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
16
,
$this
->
plugin
->
getFailoverMaxRetries
());
}
public
function
testAdd
Server
()
public
function
testAdd
Endpoint
()
{
$this
->
plugin
->
add
Server
(
's1'
,
$this
->
serverOptions
,
10
);
$this
->
plugin
->
add
Endpoint
(
's1'
,
10
);
$this
->
assertEquals
(
array
(
's1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
,
)
),
$this
->
plugin
->
getServers
()
array
(
's1'
=>
10
),
$this
->
plugin
->
getEndpoints
()
);
}
public
function
testAdd
ServerWithDuplicateKey
()
public
function
testAdd
EndpointWithObject
()
{
$this
->
plugin
->
add
Server
(
's1'
,
$this
->
serverOptions
,
10
);
$this
->
plugin
->
add
Endpoint
(
$this
->
client
->
getEndpoint
(
'server1'
)
,
10
);
$this
->
setExpectedException
(
'Solarium\Core\Exception'
);
$this
->
plugin
->
addServer
(
's1'
,
$this
->
serverOptions
,
20
);
$this
->
assertEquals
(
array
(
'server1'
=>
10
),
$this
->
plugin
->
getEndpoints
()
);
}
public
function
test
GetServer
()
public
function
test
AddEndpoints
()
{
$this
->
plugin
->
addServer
(
's1'
,
$this
->
serverOptions
,
10
);
$endpoints
=
array
(
's1'
=>
10
,
's2'
=>
8
);
$this
->
plugin
->
addEndpoints
(
$endpoints
);
$this
->
assertEquals
(
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
$this
->
plugin
->
get
Server
(
's1'
)
$endpoints
,
$this
->
plugin
->
get
Endpoints
(
)
);
}
public
function
test
GetInvalidServer
()
public
function
test
AddEndpointWithDuplicateKey
()
{
$this
->
plugin
->
add
Server
(
's1'
,
$this
->
serverOptions
,
10
);
$this
->
plugin
->
add
Endpoint
(
's1'
,
10
);
$this
->
setExpectedException
(
'Solarium\Core\Exception'
);
$this
->
plugin
->
getServer
(
's2'
);
$this
->
plugin
->
addEndpoint
(
's1'
,
20
);
}
public
function
testClear
Server
s
()
public
function
testClear
Endpoint
s
()
{
$this
->
plugin
->
add
Server
(
's1'
,
$this
->
serverOptions
,
10
);
$this
->
plugin
->
clear
Server
s
();
$this
->
assertEquals
(
array
(),
$this
->
plugin
->
get
Server
s
());
$this
->
plugin
->
add
Endpoint
(
's1'
,
10
);
$this
->
plugin
->
clear
Endpoint
s
();
$this
->
assertEquals
(
array
(),
$this
->
plugin
->
get
Endpoint
s
());
}
public
function
test
AddServers
()
public
function
test
RemoveEndpoint
()
{
$
server
s
=
array
(
's1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
's2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
,
$
endpoint
s
=
array
(
's1'
=>
10
,
's2'
=>
20
,
);
$this
->
plugin
->
addServers
(
$servers
);
$this
->
assertEquals
(
$servers
,
$this
->
plugin
->
getServers
());
$this
->
plugin
->
addEndpoints
(
$endpoints
);
$this
->
plugin
->
removeEndpoint
(
's1'
);
$this
->
assertEquals
(
array
(
's2'
=>
20
),
$this
->
plugin
->
getEndpoints
()
);
}
public
function
testRemove
Server
()
public
function
testRemove
EndpointWithObject
()
{
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
,
$
endpoint
s
=
array
(
's
erver1'
=>
10
,
's
erver2'
=>
20
,
);
$this
->
plugin
->
add
Servers
(
$server
s
);
$this
->
plugin
->
remove
Server
(
's1'
);
$this
->
plugin
->
add
Endpoints
(
$endpoint
s
);
$this
->
plugin
->
remove
Endpoint
(
$this
->
client
->
getEndpoint
(
'server1'
)
);
$this
->
assertEquals
(
array
(
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
),
$this
->
plugin
->
get
Server
s
()
array
(
's
erver2'
=>
20
),
$this
->
plugin
->
get
Endpoint
s
()
);
}
public
function
testSet
Server
s
()
public
function
testSet
Endpoint
s
()
{
$
server
s1
=
array
(
's1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
's2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
,
$
endpoint
s1
=
array
(
's1'
=>
10
,
's2'
=>
20
,
);
$
server
s2
=
array
(
's3'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
50
)
,
's4'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
30
)
,
$
endpoint
s2
=
array
(
's3'
=>
50
,
's4'
=>
30
,
);
$this
->
plugin
->
add
Servers
(
$server
s1
);
$this
->
plugin
->
set
Servers
(
$server
s2
);
$this
->
plugin
->
add
Endpoints
(
$endpoint
s1
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s2
);
$this
->
assertEquals
(
$
server
s2
,
$this
->
plugin
->
get
Server
s
()
$
endpoint
s2
,
$this
->
plugin
->
get
Endpoint
s
()
);
}
public
function
testSetAndGetForced
Server
ForNextQuery
()
public
function
testSetAndGetForced
Endpoint
ForNextQuery
()
{
$
server
s1
=
array
(
's1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
's2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
,
$
endpoint
s1
=
array
(
's1'
=>
10
,
's2'
=>
20
,
);
$this
->
plugin
->
add
Servers
(
$server
s1
);
$this
->
plugin
->
add
Endpoints
(
$endpoint
s1
);
$this
->
plugin
->
setForced
Server
ForNextQuery
(
's2'
);
$this
->
assertEquals
(
's2'
,
$this
->
plugin
->
getForced
Server
ForNextQuery
());
$this
->
plugin
->
setForced
Endpoint
ForNextQuery
(
's2'
);
$this
->
assertEquals
(
's2'
,
$this
->
plugin
->
getForced
Endpoint
ForNextQuery
());
}
public
function
testSet
ForcedServerForNextQueryWithInvalidKey
()
public
function
testSet
AndGetForcedEndpointForNextQueryWithObject
()
{
$
server
s1
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
10
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
20
)
,
$
endpoint
s1
=
array
(
's
erver1'
=>
10
,
's
erver2'
=>
20
,
);
$this
->
plugin
->
addServers
(
$servers1
);
$this
->
plugin
->
addEndpoints
(
$endpoints1
);
$this
->
plugin
->
setForcedEndpointForNextQuery
(
$this
->
client
->
getEndpoint
(
'server2'
));
$this
->
assertEquals
(
'server2'
,
$this
->
plugin
->
getForcedEndpointForNextQuery
());
}
public
function
testSetForcedEndpointForNextQueryWithInvalidKey
()
{
$endpoints1
=
array
(
's1'
=>
10
,
's2'
=>
20
,
);
$this
->
plugin
->
addEndpoints
(
$endpoints1
);
$this
->
setExpectedException
(
'Solarium\Core\Exception'
);
$this
->
plugin
->
setForced
Server
ForNextQuery
(
's3'
);
$this
->
plugin
->
setForced
Endpoint
ForNextQuery
(
's3'
);
}
public
function
testAddBlockedQueryType
()
...
...
@@ -293,45 +298,45 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
);
}
public
function
testPreExecuteRequestWithForced
Server
()
public
function
testPreExecuteRequestWithForced
Endpoint
()
{
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
100
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$
endpoint
s
=
array
(
's
erver1'
=>
100
,
's
erver2'
=>
1
,
);
$query
=
new
SelectQuery
();
$request
=
new
Request
();
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
setForced
ServerForNextQuery
(
's
2'
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$this
->
plugin
->
setForced
EndpointForNextQuery
(
'server
2'
);
$this
->
plugin
->
preCreateRequest
(
$query
);
$this
->
plugin
->
preExecuteRequest
(
$request
);
$this
->
assertEquals
(
's2'
,
$this
->
plugin
->
getLast
ServerKey
()
's
erver
2'
,
$this
->
plugin
->
getLast
Endpoint
()
);
}
public
function
test
AdapterPrese
tRestore
()
public
function
test
DefaultEndpoin
tRestore
()
{
$originalHost
=
$this
->
client
->
get
Adapter
()
->
getHost
();
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
100
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$originalHost
=
$this
->
client
->
get
Endpoint
()
->
getHost
();
$
endpoint
s
=
array
(
's
erver1'
=>
100
,
's
erver2'
=>
1
,
);
$request
=
new
Request
();
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
setForced
ServerForNextQuery
(
's
2'
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$this
->
plugin
->
setForced
EndpointForNextQuery
(
'server
2'
);
$query
=
new
SelectQuery
();
$this
->
plugin
->
preCreateRequest
(
$query
);
$this
->
plugin
->
preExecuteRequest
(
$request
);
$this
->
assertEquals
(
's2'
,
$this
->
plugin
->
getLast
ServerKey
()
's
erver
2'
,
$this
->
plugin
->
getLast
Endpoint
()
);
$query
=
new
SelectQuery
();
// this is a blocked querytype that should trigger a restore
...
...
@@ -340,18 +345,18 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
$originalHost
,
$this
->
client
->
get
Adapter
()
->
getHost
()
$this
->
client
->
get
Endpoint
()
->
getHost
()
);
}
public
function
testBlockedQueryTypeNotLoadbalanced
()
{
$originalHost
=
$this
->
client
->
get
Adapter
()
->
getHost
();
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
100
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$originalHost
=
$this
->
client
->
get
Endpoint
()
->
getHost
();
$
endpoint
s
=
array
(
's
erver1'
=>
100
,
's
erver2'
=>
1
,
);
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$request
=
new
Request
();
$query
=
new
UpdateQuery
();
// this is a blocked querytype that should not be loadbalanced
...
...
@@ -360,22 +365,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
$originalHost
,
$this
->
client
->
get
Adapter
()
->
getHost
()
$this
->
client
->
get
Endpoint
()
->
getHost
()
);
$this
->
assertEquals
(
null
,
$this
->
plugin
->
getLast
ServerKey
()
$this
->
plugin
->
getLast
Endpoint
()
);
}
public
function
testLoadbalancerRandomizing
()
{
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$
endpoint
s
=
array
(
's
erver1'
=>
1
,
's
erver2'
=>
1
,
);
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$request
=
new
Request
();
$query
=
new
SelectQuery
();
//
...
...
@@ -383,23 +388,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
plugin
->
preExecuteRequest
(
$request
);
$this
->
assertTrue
(
in_array
(
$this
->
plugin
->
getLast
ServerKey
(),
array
(
's1'
,
's
2'
))
in_array
(
$this
->
plugin
->
getLast
Endpoint
(),
array
(
'server1'
,
'server
2'
))
);
}
public
function
testFailover
()
{
$this
->
plugin
=
new
TestLoadbalancer
();
// special loadbalancer that returns servers in fixed order
$this
->
client
=
new
Client
();
$this
->
plugin
=
new
TestLoadbalancer
();
// special loadbalancer that returns endpoints in fixed order
$this
->
client
->
setAdapter
(
new
TestAdapterForFailover
());
// set special mock that fails once
$this
->
plugin
->
initPlugin
(
$this
->
client
,
array
());
$request
=
new
Request
();
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$
endpoint
s
=
array
(
's
erver1'
=>
1
,
's
erver2'
=>
1
,
);
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$this
->
plugin
->
setFailoverEnabled
(
true
);
$query
=
new
SelectQuery
();
...
...
@@ -407,26 +411,26 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this
->
plugin
->
preExecuteRequest
(
$request
);
$this
->
assertEquals
(
's2'
,
$this
->
plugin
->
getLast
ServerKey
()
's
erver
2'
,
$this
->
plugin
->
getLast
Endpoint
()
);
}
public
function
testFailoverMaxRetries
()
{
$this
->
plugin
=
new
TestLoadbalancer
();
// special loadbalancer that returns
server
s in fixed order
$this
->
client
=
new
Client
();
$this
->
plugin
=
new
TestLoadbalancer
();
// special loadbalancer that returns
endpoint
s in fixed order
$adapter
=
new
TestAdapterForFailover
();
$adapter
->
setFailCount
(
10
);
$this
->
client
->
setAdapter
(
$adapter
);
// set special mock that fails for all
server
s
$this
->
client
->
setAdapter
(
$adapter
);
// set special mock that fails for all
endpoint
s
$this
->
plugin
->
initPlugin
(
$this
->
client
,
array
());
$request
=
new
Request
();
$
server
s
=
array
(
's
1'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
's
2'
=>
array
(
'options'
=>
$this
->
serverOptions
,
'weight'
=>
1
)
,
$
endpoint
s
=
array
(
's
erver1'
=>
1
,
's
erver2'
=>
1
,
);
$this
->
plugin
->
set
Servers
(
$server
s
);
$this
->
plugin
->
set
Endpoints
(
$endpoint
s
);
$this
->
plugin
->
setFailoverEnabled
(
true
);
$query
=
new
SelectQuery
();
...
...
@@ -446,18 +450,18 @@ class TestLoadbalancer extends Loadbalancer{
protected
$counter
=
0
;
/**
* Get options array for a randomized
server
* Get options array for a randomized
endpoint
*
* @return array
*/
protected
function
getRandom
ServerOptions
()
protected
function
getRandom
Endpoint
()
{
$this
->
counter
++
;
$
serverKey
=
's
'
.
$this
->
counter
;
$
endpointKey
=
'server
'
.
$this
->
counter
;
$this
->
serverExcludes
[]
=
$server
Key
;
$this
->
last
ServerKey
=
$server
Key
;
return
$this
->
servers
[
$serverKey
][
'options'
]
;
$this
->
endpointExcludes
[]
=
$endpoint
Key
;
$this
->
last
Endpoint
=
$endpoint
Key
;
return
$this
->
client
->
getEndpoint
(
$endpointKey
)
;
}
}
...
...
@@ -473,7 +477,7 @@ class TestAdapterForFailover extends HttpAdapter{
$this
->
failCount
=
$count
;
}
public
function
execute
(
$request
)
public
function
execute
(
$request
,
$endpoint
)
{
$this
->
counter
++
;
if
(
$this
->
counter
<=
$this
->
failCount
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment