Skip to content

Conversation

@MartinMystikJonas
Copy link
Contributor

In current implementation when Environment::lock is called multiple times only last acquired lock is saved. Previous lock are sometimes randomly garbage collected. This results in race conditions and random test failures.

@JanTvrdik
Copy link
Contributor

Why do you need multiple locks?

@MartinMystikJonas
Copy link
Contributor Author

In my case one for database (to prevent parallel accesses to database) and second for testing API server (also to prevent parellel access to API). Some tests need just database, some needs just API but some needs both.

@JanTvrdik
Copy link
Contributor

Ok, that makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinMystikJonas Can you remove the initialization? No big deal, it's just shorter.

@milo
Copy link
Member

milo commented Jan 23, 2015

@MartinMystikJonas And can you rename the commit? Something like Environment: allow multiple locks... I'll merge it.

@MartinMystikJonas
Copy link
Contributor Author

@milo Done.

milo added a commit that referenced this pull request Jan 23, 2015
@milo milo merged commit 16b70b5 into nette:master Jan 23, 2015
@milo
Copy link
Member

milo commented Jan 23, 2015

@MartinMystikJonas Thank you!

@mrtnzlml
Copy link

Hi. Are you really sure this is a good idea? I don't know the original use case, but with this commit is even very simple test failing (based on sandbox default test case):

<?php

namespace Test;

use Nette,
    Tester,
    Tester\Assert;

$container = require __DIR__ . '/bootstrap.php';

class ExampleTest extends Tester\TestCase {

    private $container;

    function __construct(Nette\DI\Container $container) {
        $this->container = $container;
    }

    function setUp() {
        Tester\Environment::lock('db', __DIR__);
    }

    function testSomething1() {
        Assert::true( true );
    }

    function testSomething2() {
        Assert::true( true );
    }

}

$test = new ExampleTest($container);
$test->run();

Well it's actually not failing, it freezes...

@fprochazka
Copy link
Contributor

The lock is never released, because you're not running the test case in multiple processes. To run the test case in multiple processes you have to add @testCase annotation above the namespace. That said, I think the locking is broken now and should be either fixed or reverted.

@mrtnzlml
Copy link

It's not true. I have every testcase with this annotation. This example also doesn't work with the @testCase. So you are saying my implementation is wrong? I have to use different lock names for each test? Doesn't make much sense to me.

milo added a commit to milo/tester that referenced this pull request Feb 21, 2015
milo added a commit to milo/tester that referenced this pull request Feb 21, 2015
Contents of static $locks exists only in one process. The acquiring can be skipped when lock already exists in this process.
@milo
Copy link
Member

milo commented Feb 21, 2015

@mrtnzlml Multiple locks is desired feature. E.g. lock('db'); lock('api');. You should call lock() ouside the TestCase to prevent freezing. But I understand it is unexpected.

See #210

@milo
Copy link
Member

milo commented Feb 21, 2015

@mrtnzlml Are you sure you use @testCase in the very first file annotation? Imho, it should not freeze with @testCase because every test.... method runs in own process.

@mrtnzlml
Copy link

@milo Yeah, I understood the request and it make sense. But the implementation was odd and I wasn't able to run all tests without freeze.

Oh sorry. It actually works with this annotation. It doesn't work only in my project (that's why I reported this behavior). But to be honest I am not sure if it actually run in 8 threads or not. Apparently not.

@fprochazka What did you mean with "You should use different names."?

@milo
Copy link
Member

milo commented Feb 21, 2015

Easy to check. Run vendor/bin/tester path/to/one/Test.phpt and you should get as many results as you have test methods.

@mrtnzlml
Copy link

Yeah I know that and it returns more dots (which is not very good test :-)). But still it doesn't work with the annotation. But I am not very good in testing and it works well with your patch, so - problem solved I guess. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants