Skip to content

Commit

Permalink
Merge pull request #44 from gregorg/test_connect_only_once
Browse files Browse the repository at this point in the history
Test connect only once
  • Loading branch information
gregorg authored Feb 6, 2018
2 parents eb78161 + 17d1c5a commit e96c1c7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,16 @@ public function slaveReplicationIsStopped($connectionName)
$connection->isSlaveOk();
}

/**
* @Then there is :n connections established on :connectionName
*/
public function thereIsConnections($connectionName, $n)
{
$connection = $this->getConnection($connectionName);
$connections = $connection->query("SELECT count(*) as n FROM pg_stat_activity")->fetch()['n'];
assert($n == $connections, "There is $connections active connection(s) on the test server");
}


abstract protected function retryStrategy($n);
}
1 change: 0 additions & 1 deletion features/master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ Feature: Master / Slaves
When I query "SELECT 1" on "conn"
Then the last query succeeded on "conn"
And "conn" is on slave

11 changes: 10 additions & 1 deletion features/retry-master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,14 @@ Feature: Retry Master/Slaves
And "conn" retry limit should be 1
And "conn" should have 1 slaves


@skip-mysqli @skip-mysql-replic @skip-travis @skip-pdo-mysql
Scenario: Connect only once
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
When I query "SELECT 1" on "conn"
Then I query "SELECT 2" on "conn"
Then I query "SELECT 3" on "conn"
Then I query "SELECT 4" on "conn"
And "conn" retry limit should be 1
And "conn" should have 2 slaves
And there is 1 connections established on "conn"

3 changes: 3 additions & 0 deletions src/Driver/Connection/MasterSlavesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private function chooseASlave()
}
$weightTarget = mt_rand(1, $totalSlavesWeight);
foreach ($this->slaves as $n => $slave) {
if ($slave['weight'] <= 0) {
continue;
}
$weightTarget -= $slave['weight'];
if ($weightTarget <= 0) {
return $n;
Expand Down

0 comments on commit e96c1c7

Please sign in to comment.