Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/recca0120/laravel-parallel
Laravel parallel
https://github.com/recca0120/laravel-parallel
async eloquent laravel library parallel testing
Last synced: 9 days ago
JSON representation
Laravel parallel
- Host: GitHub
- URL: https://github.com/recca0120/laravel-parallel
- Owner: recca0120
- License: mit
- Created: 2021-06-09T20:25:43.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-03T16:12:13.000Z (6 months ago)
- Last Synced: 2024-10-20T08:04:24.487Z (19 days ago)
- Topics: async, eloquent, laravel, library, parallel, testing
- Language: PHP
- Homepage:
- Size: 1.02 MB
- Stars: 63
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Parallel
[![Latest Version on Packagist](https://img.shields.io/packagist/v/recca0120/laravel-parallel.svg?style=flat-square)](https://packagist.org/packages/recca0120/laravel-parallel)
![Tests](https://github.com/recca0120/laravel-parallel/workflows/tests/badge.svg)
[![Total Downloads](https://img.shields.io/packagist/dt/recca0120/laravel-parallel.svg?style=flat-square)](https://packagist.org/packages/recca0120/laravel-parallel)![Laravel Parallel](screenshots/laravel-parallel.png "Laravel Parallel")
## Requirements
- **Laravel** versions 5.7, 6.x, 7.x, 8.x, 9.x, 10.x and 11.x
- **PHP** 7.0 or greater## Installation
Install the package with composer:
```bash
composer require recca0120/laravel-parallel
```## Usage
- make a product migration
```php
id();
$table->string('name');
$table->integer('quantity')->default(0);
$table->timestamps();
});
}/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}```
- define product model `App\Models\Product`
```php
'int'];
}```
- define router in `routes/web.php`
```php
quantity > 0) {
// wrong, it will make test fail
// $product->fill(['quantity' => $product->quantity - 1])->save();// correct
$product->where('id', $product->id)
->where('quantity', '>', 0)
->update(['quantity' => DB::raw('quantity - 1')]);
}return $product->fresh();
});```
- testing
```php
useParallelPhyiscalDatabase();$this->product = Product::create(['name' => 'test', 'quantity' => $this->quantity]);
$request = $this->app->make(ParallelRequest::class);$promises = collect();
for ($i = 0; $i < $this->quantity; $i++) {
// you will get \GuzzleHttp\Promise\PromiseInterface
$promise = $request->post('/product/'.$this->product->id);
$promises->add($promise);
}
// you need wait response
$promises->map->wait()->each->assertOk();$this->get('/product/'.$this->product->id)
->assertOk()
->assertJsonPath('quantity', 0);
}public function test_multiple_times_to_test_race_condition()
{
$this->useParallelPhyiscalDatabase();$this->product = Product::create(['name' => 'test', 'quantity' => $this->quantity]);
$request = $this->app->make(ParallelRequest::class);
$promises = collect($request->times(10)->post('/product/'.$this->product->id));
// you need wait response
$promises->map->wait()->each->assertOk();$this->get('/product/'.$this->product->id)
->assertOk()
->assertJsonPath('quantity', 0);
}
}
```## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.