Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/recca0120/laravel-parallel

Laravel parallel
https://github.com/recca0120/laravel-parallel

async eloquent laravel library parallel testing

Last synced: 1 day ago
JSON representation

Laravel parallel

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.