\Codeception\TestCase\Test does not close database connection when running haveFixtures on Yii2 #3456

Closed
igawarodrigo opened this Issue Aug 20, 2016 · 0 comments

Comments

Projects
None yet
2 participants
@igawarodrigo

What are you trying to achieve?

Hello. In one of the lastest updates (2.2.3), improvements like haveFixtures() method was added to use along Yii2. However, it seems that database connection is not closed and, when running a large set of unit tests we get the Too Many Connections error from Mysql.

What do you get instead?

mysql> show full processlist; (while "vendor/bin/codecept run unit" is running )

| 1372 | root | localhost:40518 | unilaudos_admtest | Sleep   |    8 |           | NULL                                                                              |
| 1374 | root | localhost:40522 | unilaudos_admtest | Sleep   |    8 |           | NULL                                                                              |
| 1376 | root | localhost:40526 | unilaudos_admtest | Sleep   |    7 |           | NULL                                                                              |
| 1378 | root | localhost:40530 | unilaudos_admtest | Sleep   |    6 |           | NULL                                                                              |
| 1380 | root | localhost:40534 | unilaudos_admtest | Sleep   |    5 |           | NULL                                                                              |
| 1382 | root | localhost:40538 | unilaudos_admtest | Sleep   |    4 |           | NULL                                                                              |
| 1383 | root | localhost:40540 | unilab_wtest      | Sleep   |    4 |           | NULL                                                                              |
| 1385 | root | localhost:40544 | unilaudos_admtest | Sleep   |    4 |           | NULL                                                                              |
| 1386 | root | localhost:40546 | unilab_wtest      | Sleep   |    4 |           | NULL                                                                              |
| 1388 | root | localhost:40550 | unilaudos_admtest | Sleep   |    4 |           | NULL                                                                              |
| 1389 | root | localhost:40552 | unilab_wtest      | Sleep   |    4 |           | NULL                                                                              |
| 1391 | root | localhost:40556 | unilaudos_admtest | Sleep   |    3 |           | NULL                                                                              |
| 1392 | root | localhost:40558 | unilab_wtest      | Sleep   |    3 |           | NULL                                                                              |
| 1394 | root | localhost:40562 | unilaudos_admtest | Sleep   |    3 |           | NULL                                                                              |
| 1395 | root | localhost:40564 | unilab_wtest      | Sleep   |    3 |           | NULL                                                                              |
| 1397 | root | localhost:40568 | unilaudos_admtest | Sleep   |    1 |           | NULL                                                                              |
| 1399 | root | localhost:40572 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1401 | root | localhost:40576 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1403 | root | localhost:40580 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1405 | root | localhost:40584 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1407 | root | localhost:40588 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1409 | root | localhost:40592 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1411 | root | localhost:40596 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1413 | root | localhost:40600 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1415 | root | localhost:40604 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1417 | root | localhost:40608 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1419 | root | localhost:40612 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1421 | root | localhost:40616 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1423 | root | localhost:40620 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1425 | root | localhost:40624 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1427 | root | localhost:40628 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1429 | root | localhost:40632 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1431 | root | localhost:40636 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1433 | root | localhost:40640 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1435 | root | localhost:40644 | unilaudos_admtest | Sleep   |    0 |           | NULL                                                                              |
| 1437 | root | localhost:40648 | unilaudos_admtest | Sleep   |    0 |           | NULL  

final output from "vendor/bin/codecept run unit"

 Test  codeception/unit/PostoTest.php:testAlterarSenhaPosto

  [yii\db\Exception] SQLSTATE[08004] [1040] Too many connections  

/home/rodrigoigawa/workspace/unilaudos/app/vendor/yiisoft/yii2/db/Connection.php:864
/home/rodrigoigawa/workspace/unilaudos/app/vendor/yiisoft/yii2/db/Command.php:221
/home/rodrigoigawa/workspace/unilaudos/app/vendor/yiisoft/yii2/db/Command.php:838
/home/rodrigoigawa/workspace/unilaudos/app/tests/codeception/unit/fixtures/PostoFixture.php:15
/home/rodrigoigawa/workspace/unilaudos/app/vendor/yiisoft/yii2/test/FixtureTrait.php:90
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Module/Yii2.php:233
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Step.php:259
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Scenario.php:69
/home/rodrigoigawa/workspace/unilaudos/app/tests/codeception/_support/_generated/UnitTesterActions.php:442
/home/rodrigoigawa/workspace/unilaudos/app/tests/codeception/unit/PostoTest.php:53
/home/rodrigoigawa/workspace/unilaudos/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:1085
/home/rodrigoigawa/workspace/unilaudos/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:936
/home/rodrigoigawa/workspace/unilaudos/app/vendor/phpunit/phpunit/src/Framework/TestResult.php:701
/home/rodrigoigawa/workspace/unilaudos/app/vendor/phpunit/phpunit/src/Framework/TestCase.php:892
/home/rodrigoigawa/workspace/unilaudos/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:753
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:98
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/SuiteManager.php:162
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Codecept.php:209
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Codecept.php:178
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Command/Run.php:329
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Command/Run.php:256
/home/rodrigoigawa/workspace/unilaudos/app/vendor/symfony/console/Command/Command.php:256
/home/rodrigoigawa/workspace/unilaudos/app/vendor/symfony/console/Application.php:818
/home/rodrigoigawa/workspace/unilaudos/app/vendor/symfony/console/Application.php:186
/home/rodrigoigawa/workspace/unilaudos/app/vendor/symfony/console/Application.php:117
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/src/Codeception/Application.php:103
/home/rodrigoigawa/workspace/unilaudos/app/vendor/codeception/codeception/codecept:33

I believe that a possible solution is to add some code in Yii2 class in Codeception\Module namespace. In _after() method around the line 168 we currently have the following code:

        if (\Yii::$app->has('session', true)) {
            \Yii::$app->session->close();
        }

Perhaps, a solution would be to close the connection on this part as well:

        if (\Yii::$app->has('session', true)) {
            \Yii::$app->session->close();
        }
        if (\Yii::$app->has('db', true)) {
             \Yii::$app->db->close();
        }

Details

  • Codeception version: 2.2.4
  • PHP Version: 7.0.9
  • Operating System: Xubuntu 16.04
  • Installation type: Composer
  • List of installed packages (composer show)

behat/gherkin v4.4.1 Gherkin DSL parser for PHP 5.3
bower-asset/bootstrap v3.3.7 The most popular front-end framework for developing responsive, mobile first projects on the web.
bower-asset/jquery 2.2.4
bower-asset/jquery.inputmask 3.2.7 jquery.inputmask is a jquery plugin which create an input mask.
bower-asset/punycode v1.3.2
bower-asset/typeahead.js v0.11.1
bower-asset/yii2-pjax v2.0.6
cebe/markdown 1.1.0 A super fast, highly extensible markdown parser for PHP
codeception/codeception 2.2.3 BDD-style testing framework
codeception/specify 0.4.3 BDD code blocks for PHPUnit and Codeception
codeception/verify 0.3.1 BDD assertion library for PHPUnit
dnoegel/php-xdg-base-dir 0.1 implementation of xdg base directory specification for php
doctrine/instantiator 1.0.5 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
ezyang/htmlpurifier v4.8.0 Standards compliant HTML filter written in PHP
facebook/webdriver 1.1.3 A PHP client for WebDriver
fzaninotto/faker v1.6.0 Faker is a PHP library that generates fake data for you.
guzzlehttp/guzzle 6.2.1 Guzzle is a PHP HTTP client library
guzzlehttp/promises 1.2.0 Guzzle promises library
guzzlehttp/psr7 1.3.1 PSR-7 message implementation
jakub-onderka/php-console-color 0.1
jakub-onderka/php-console-highlighter v0.3.2
myclabs/deep-copy 1.5.1 Create deep copies (clones) of your objects
nikic/php-parser v2.1.0 A PHP parser written in PHP
phpdocumentor/reflection-common 1.0 Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock 3.1.0 With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.
phpdocumentor/type-resolver 0.2
phpspec/php-diff v1.1.0 A comprehensive library for generating differences between two hashable objects (strings or arrays).
phpspec/prophecy v1.6.1 Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage 4.0.1 Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-file-iterator 1.4.1 FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template 1.2.1 Simple template engine.
phpunit/php-timer 1.0.8 Utility class for timing
phpunit/php-token-stream 1.4.8 Wrapper around PHP's tokenizer extension.
phpunit/phpunit 5.4.8 The PHP Unit Testing framework.
phpunit/phpunit-mock-objects 3.2.3 Mock Object library for PHPUnit
psr/http-message 1.0.1 Common interface for HTTP messages
psy/psysh v0.7.2 An interactive shell for modern PHP.
sebastian/code-unit-reverse-lookup 1.0.0 Looks up which function or method a line of code belongs to
sebastian/comparator 1.2.0 Provides the functionality to compare PHP values for equality
sebastian/diff 1.4.1 Diff implementation
sebastian/environment 1.3.7 Provides functionality to handle HHVM/PHP environments
sebastian/exporter 1.2.2 Provides the functionality to export PHP variables for visualization
sebastian/global-state 1.1.1 Snapshotting of global state
sebastian/object-enumerator 1.0.0 Traverses array structures and object graphs to enumerate all referenced objects
sebastian/recursion-context 1.0.2 Provides functionality to recursively process PHP variables
sebastian/resource-operations 1.0.0 Provides a list of PHP built-in functions that operate on resources
sebastian/version 2.0.0 Library that helps with managing the version number of Git-hosted PHP projects
symfony/browser-kit v3.1.3 Symfony BrowserKit Component
symfony/console v3.1.3 Symfony Console Component
symfony/css-selector v3.1.3 Symfony CssSelector Component
symfony/dom-crawler v3.1.3 Symfony DomCrawler Component
symfony/event-dispatcher v3.1.3 Symfony EventDispatcher Component
symfony/finder v3.1.3 Symfony Finder Component
symfony/polyfill-mbstring v1.2.0 Symfony polyfill for the Mbstring extension
symfony/var-dumper v3.1.3 Symfony mechanism for exploring and dumping PHP variables
symfony/yaml v3.1.3 Symfony Yaml Component
webmozart/assert 1.1.0 Assertions to validate method input/output with nice error messages.
yiisoft/yii2 2.0.9 Yii PHP Framework Version 2
yiisoft/yii2-bootstrap 2.0.6 The Twitter Bootstrap extension for the Yii framework
yiisoft/yii2-codeception 2.0.5 The Codeception integration for the Yii framework
yiisoft/yii2-composer 2.0.4 The composer plugin for Yii extension installer
yiisoft/yii2-debug 2.0.6 The debugger extension for the Yii framework
yiisoft/yii2-faker 2.0.3 Fixture generator. The Faker integration for the Yii framework.
yiisoft/yii2-gii 2.0.5 The Gii extension for the Yii framework
yiisoft/yii2-shell dev-master 2baeb0a The interactive shell extension for Yii framework

@Naktibalda Naktibalda added the Yii label Aug 20, 2016

Ni-san pushed a commit to Ni-san/Codeception that referenced this issue Oct 6, 2016

p.chapl
refs #3456 \Codeception\TestCase\Test does not close database connect…
…ion when running haveFixtures on Yii2

Close db connections if exists after Yii2-tests (\Codeception\Module\Yii2::_after)

Ni-san pushed a commit to Ni-san/Codeception that referenced this issue Oct 6, 2016

p.chapl
refs #3456 \Codeception\TestCase\Test does not close database connect…
…ion when running haveFixtures on Yii2

Close db connections if exists after Yii2-tests (\Codeception\Module\Yii2::_after)

Ni-san pushed a commit to Ni-san/Codeception that referenced this issue Oct 6, 2016

p.chapl
refs #3456 \Codeception\TestCase\Test does not close database connect…
…ion when running haveFixtures on Yii2

Close db connections if exists after Yii2-tests (\Codeception\Module\Yii2::_after)

@DavertMik DavertMik closed this in #3586 Oct 10, 2016

DavertMik added a commit that referenced this issue Oct 10, 2016

refs #3456 \Codeception\TestCase\Test does not close database connect…
…ion when running haveFixtures on Yii2 (#3586)

Close db connections if exists after Yii2-tests (\Codeception\Module\Yii2::_after)

chris1312 added a commit to chris1312/Codeception that referenced this issue Jun 16, 2017

refs #3456 \Codeception\TestCase\Test does not close database connect…
…ion when running haveFixtures on Yii2 (#3586)

Close db connections if exists after Yii2-tests (\Codeception\Module\Yii2::_after)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment