RetryOnExceptionTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace YdbPlatform\Ydb\Test;
  3. use PHPUnit\Framework\TestCase;
  4. use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
  5. use YdbPlatform\Ydb\Exceptions\Grpc\ResourceExhaustedException;
  6. use YdbPlatform\Ydb\Retry\RetryParams;
  7. use YdbPlatform\Ydb\Session;
  8. use YdbPlatform\Ydb\Table;
  9. use YdbPlatform\Ydb\Ydb;
  10. class SessionManager extends \YdbPlatform\Ydb\Session{
  11. public static function setSessionId(\YdbPlatform\Ydb\Session $session, string $id){
  12. $session->session_id = $id;
  13. return $session;
  14. }
  15. public static function getSessionId(\YdbPlatform\Ydb\Session $session){
  16. return $session->session_id;
  17. }
  18. }
  19. class RetryOnExceptionTest extends TestCase
  20. {
  21. /**
  22. * @var string
  23. */
  24. private $oldSessionId;
  25. public function testRetryOnExceptionInRetry(){
  26. $config = [
  27. // Database path
  28. 'database' => '/local',
  29. // Database endpoint
  30. 'endpoint' => 'localhost:2136',
  31. // Auto discovery (dedicated server only)
  32. 'discovery' => false,
  33. // IAM config
  34. 'iam_config' => [
  35. 'insecure' => true,
  36. ],
  37. 'credentials' => new AnonymousAuthentication()
  38. ];
  39. $ydb = new Ydb($config);
  40. $table = $ydb->table();
  41. $session = $table->createSession();
  42. $this->oldSessionId = SessionManager::getSessionId($session);
  43. $session->delete();
  44. $this->retryTest($table);
  45. }
  46. private function retryTest(Table $table)
  47. {
  48. $i = 0;
  49. $table->retrySession(function (Session $session) use (&$i){
  50. $i++;
  51. if($i==1)SessionManager::setSessionId($session, $this->oldSessionId);
  52. $tres = $session->query('select 1 as res')->rows()[0]['res'];
  53. self::assertEquals(
  54. 1,
  55. $tres
  56. );
  57. }, true, new RetryParams(2000));
  58. $i = 0;
  59. $table->retryTransaction(function (Session $session) use (&$i) {
  60. if($i == 0){
  61. throw new ResourceExhaustedException('Test exception');
  62. }
  63. self::assertEquals(5, $i);
  64. }, null, null, [
  65. 'idempotent' => true,
  66. 'callback_on_error' => function (\Exception $exception) use (&$i) {$i=5;}
  67. ]);
  68. }
  69. }