-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified how drivers handle query timeout settings #1037
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
--TEST-- | ||
GitHub issue 1027 - PDO::SQLSRV_ATTR_QUERY_TIMEOUT had no effect on PDO::exec() | ||
--DESCRIPTION-- | ||
This test verifies that setting PDO::SQLSRV_ATTR_QUERY_TIMEOUT correctly should affect PDO::exec() as in the case for PDO::prepare() (as statement attribute or option). | ||
--ENV-- | ||
PHPT_EXEC=true | ||
--SKIPIF-- | ||
<?php require('skipif_mid-refactor.inc'); ?> | ||
--FILE-- | ||
<?php | ||
require_once("MsSetup.inc"); | ||
require_once("MsCommon_mid-refactor.inc"); | ||
|
||
const _DELAY = 5; | ||
|
||
$message = '*Invalid value timeout specified for option PDO::SQLSRV_ATTR_QUERY_TIMEOUT.'; | ||
$delay = _DELAY; | ||
$query = "WAITFOR DELAY '00:00:$delay'; SELECT 1"; | ||
$error = '*Query timeout expired'; | ||
|
||
function testTimeoutAttribute($conn, $timeout, $statementLevel = false) | ||
{ | ||
global $message; | ||
|
||
$error = str_replace('timeout', $timeout, $message); | ||
|
||
try { | ||
if ($statementLevel) { | ||
trace("statement option expects error: $error\n"); | ||
$options = array(PDO::SQLSRV_ATTR_QUERY_TIMEOUT => $timeout); | ||
$sql = 'SELECT 1'; | ||
$stmt = $conn->prepare($sql, $options); | ||
} else { | ||
trace("connection attribute expects error: $error\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused, do we expect a timeout error on 'SELECT 1'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method set invalid timeout values so the setting should have already failed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it wouldn't return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh... here in this function there's a local variable $error as well. My bad I should have used a diff name for this local variable |
||
$conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, $timeout); | ||
} | ||
} catch (PDOException $e) { | ||
if (!fnmatch($error, $e->getMessage())) { | ||
echo "Unexpected error returned setting invalid $timeout for SQLSRV_ATTR_QUERY_TIMEOUT\n"; | ||
var_dump($e->getMessage()); | ||
} | ||
} | ||
} | ||
|
||
function testErrors($conn) | ||
{ | ||
testTimeoutAttribute($conn, 1.8); | ||
testTimeoutAttribute($conn, 'xyz'); | ||
testTimeoutAttribute($conn, -99, true); | ||
testTimeoutAttribute($conn, 'abc', true); | ||
} | ||
|
||
function checkTimeElapsed($message, $t0, $t1, $expectedDelay) | ||
{ | ||
$elapsed = $t1 - $t0; | ||
$diff = abs($elapsed - $expectedDelay); | ||
$leeway = 1.0; | ||
david-puglielli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$missed = ($diff > $leeway); | ||
trace("$message $elapsed secs elapsed\n"); | ||
|
||
if ($missed) { | ||
echo $message; | ||
echo "Expected $expectedDelay but $elapsed secs elapsed\n"; | ||
} | ||
} | ||
|
||
function connectionTest($timeout, $asAttribute) | ||
{ | ||
global $query, $error; | ||
$keyword = ''; | ||
|
||
if ($asAttribute) { | ||
$conn = connect($keyword); | ||
$conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, $timeout); | ||
} else { | ||
$options = array(PDO::SQLSRV_ATTR_QUERY_TIMEOUT => $timeout); | ||
$conn = connect($keyword, $options); | ||
} | ||
|
||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
// if timeout is 0 it means no timeout | ||
$delay = ($timeout > 0) ? $timeout : _DELAY; | ||
|
||
$result = null; | ||
$t0 = microtime(true); | ||
|
||
try { | ||
$result = $conn->exec($query); | ||
david-puglielli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ($timeout > 0) { | ||
echo "connectionTest $timeout, $asAttribute: "; | ||
echo "this should have timed out!\n"; | ||
} | ||
} catch (PDOException $e) { | ||
if (!fnmatch($error, $e->getMessage())) { | ||
echo "Connection test error expected $timeout, $asAttribute:\n"; | ||
var_dump($e->getMessage()); | ||
} | ||
} | ||
|
||
$t1 = microtime(true); | ||
checkTimeElapsed("connectionTest ($timeout, $asAttribute): ", $t0, $t1, $delay); | ||
|
||
return $conn; | ||
} | ||
|
||
function queryTest($conn, $timeout) | ||
{ | ||
global $query, $error; | ||
|
||
// if timeout is 0 it means no timeout | ||
$delay = ($timeout > 0) ? $timeout : _DELAY; | ||
|
||
$t0 = microtime(true); | ||
try { | ||
$conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, $timeout); | ||
$stmt = $conn->query($query); | ||
|
||
if ($timeout > 0) { | ||
echo "Query test $timeout: should have timed out!\n"; | ||
} | ||
} catch (PDOException $e) { | ||
if (!fnmatch($error, $e->getMessage())) { | ||
echo "Query test error expected $timeout:\n"; | ||
var_dump($e->getMessage()); | ||
} | ||
} | ||
|
||
$t1 = microtime(true); | ||
|
||
checkTimeElapsed("Query test ($timeout): ", $t0, $t1, $delay); | ||
|
||
unset($stmt); | ||
} | ||
|
||
function statementTest($conn, $timeout, $asAttribute) | ||
{ | ||
global $query, $error; | ||
|
||
// if timeout is 0 it means no timeout | ||
$delay = ($timeout > 0) ? $timeout : _DELAY; | ||
|
||
$result = null; | ||
$t0 = microtime(true); | ||
|
||
try { | ||
if ($asAttribute) { | ||
$stmt = $conn->prepare($query); | ||
$stmt->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, $timeout); | ||
} else { | ||
$options = array(PDO::SQLSRV_ATTR_QUERY_TIMEOUT => $timeout); | ||
$stmt = $conn->prepare($query, $options); | ||
} | ||
|
||
$result = $stmt->execute(); | ||
|
||
if ($timeout > 0) { | ||
echo "statementTest $timeout: should have timed out!\n"; | ||
} | ||
} catch (PDOException $e) { | ||
if (!fnmatch($error, $e->getMessage())) { | ||
echo "Statement test error expected $timeout, $asAttribute:\n"; | ||
var_dump($e->getMessage()); | ||
} | ||
} | ||
|
||
$t1 = microtime(true); | ||
|
||
checkTimeElapsed("statementTest ($timeout, $asAttribute): ", $t0, $t1, $delay); | ||
|
||
unset($stmt); | ||
} | ||
|
||
try { | ||
$rand = rand(1, 100); | ||
$timeout = $rand % 3; | ||
$asAttribute = $rand % 2; | ||
|
||
$conn = connectionTest($timeout, $asAttribute); | ||
testErrors($conn); | ||
unset($conn); | ||
|
||
$conn = connectionTest(0, !$asAttribute); | ||
queryTest($conn, $timeout); | ||
|
||
for ($i = 0; $i < 2; $i++) { | ||
statementTest($conn, $timeout, $i); | ||
} | ||
unset($conn); | ||
|
||
echo "Done\n"; | ||
} catch (PdoException $e) { | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remind me again why it is not necessary to set lock_timeout on the PDO side?