3

Imagine that you have interfaces which describe the data access layer of your application. You haven't decided yet what kind of storing mechanism you want to use, you just want to make sure, that whatever you choose, it will handle concurrent requests well. For that you have to write concurrency tests against those interfaces.

I think a schematic concurrency test should be something like this:

public function testMoneyIsNotLostByConcurrentTransfers(){
    $accountRepository = DataAccessLayer::getBankAccountRepository();
    $accountOfTom = $accountRepository->create(array(
        'owner' => 'Tom',
        'balance' => new Money(10000)
    ));
    $accountOfBob = $accountRepository->create(array(
        'owner' => 'Bob',
        'balance' => new Money(10000)
    ));
    $accountOfSusanne = $accountRepository->create(array(
        'owner' => 'Susanne',
        'balance' => new Money(10000)
    ));

    $this->concurrentExecution(
        function () use ($accountOfTom, $accountOfBob){
            $accountOfTom->transfer($accountOfBob, new Money(5000));
        },
        function() use ($accountOfTom, $accountOfSusanne){
            $accountOfSusanne->transfer($accountOfTom, new Money(5000));
        }
    );

    $this->assertEquals($accountOfTom->getBalanceAmount(), 10000);
    $this->assertEquals($accountOfBob->getBalanceAmount(), 15000);
    $this->assertEquals($accountOfSusanne->getBalanceAmount(), 5000);
}

Is it possible to write such tests, test runner in PHP? Or is there any existing tool which can help by concurrency testing in PHP?

2 Answers 2

1

I could not find any test runner for such concurrency tests. I found only paratest, which can run independent tests, like unit tests parallel.

According to PHP - parallel task runner the best option I think is using pthreads with debug_backtrace. I think it will be hard even with that. I am looking forward the installing problems, thread safety, resource sharing difficulties, backtrace bugs, etc... I will have a great time I am sure...:S

I found async calls in the pthreads examples.

If I ever manage to solve this, I will share it on github and add a link here. Until then...

update

I just realized that I don't need multi thread or multi process applications to test concurrency. For example I can start two transactions with 2 database connections from the same php file. What I need is add event triggering for the statements the db driver does, so I can add breakpoints and wait the other task wherever I want. File locking is just the same... So coroutines or some hand made multi tasking and statement logging is just enough...

Sign up to request clarification or add additional context in comments.

Comments

-1

Concurrency should be built into your saving mechanism, not the execution layer.

For example, if you are using SQL, instead of setting the variable use += and -=.

1 Comment

Sorry, I don't care where concurrency should be built in. The question was about how to test it without knowing the implementation. For example I can save into json files instead of a database, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.