All Projects → recca0120 → laravel-parallel

recca0120 / laravel-parallel

Licence: MIT license
Laravel parallel

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-parallel

parallel
Lwt-enabled distributed computing library
Stars: ✭ 36 (-12.2%)
Mutual labels:  parallel
hatrack
Fast, multi-reader, multi-writer, lockless data structures for parallel programming
Stars: ✭ 55 (+34.15%)
Mutual labels:  parallel
laravel-searchzy
Package que te permite buscar y filtrar registros de Eloquent Models en Laravel de una manera simple y sencilla.
Stars: ✭ 15 (-63.41%)
Mutual labels:  eloquent
FoldsCUDA.jl
Data-parallelism on CUDA using Transducers.jl and for loops (FLoops.jl)
Stars: ✭ 48 (+17.07%)
Mutual labels:  parallel
eloquent-has-by-join
Convert has() and whereHas() constraints to join() ones for single-result relations.
Stars: ✭ 21 (-48.78%)
Mutual labels:  eloquent
laravel-mass-update
Update multiple Laravel Model records, each with its own set of values, sending a single query to your database!
Stars: ✭ 65 (+58.54%)
Mutual labels:  eloquent
wasm-bindgen-rayon
An adapter for enabling Rayon-based concurrency on the Web with WebAssembly.
Stars: ✭ 257 (+526.83%)
Mutual labels:  parallel
laravel-eloquent-spatial
Laravel Eloquent spatial package.
Stars: ✭ 90 (+119.51%)
Mutual labels:  eloquent
go-worker-thread-pool
A visual working example of a Thread Pool pattern, based on a known blog article.
Stars: ✭ 24 (-41.46%)
Mutual labels:  parallel
laravel-route-model-autobinding
THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.
Stars: ✭ 14 (-65.85%)
Mutual labels:  eloquent
zmq
ZeroMQ based distributed patterns
Stars: ✭ 27 (-34.15%)
Mutual labels:  parallel
reactphp-parallel
ReactPHP bindings around ext-parallel
Stars: ✭ 15 (-63.41%)
Mutual labels:  parallel
laravel-tmdb
Interact with TMDB data in your Laravel application.
Stars: ✭ 25 (-39.02%)
Mutual labels:  eloquent
NPB-CPP
NAS Parallel Benchmark Kernels in C/C++. The parallel versions are in FastFlow, TBB, and OpenMP.
Stars: ✭ 18 (-56.1%)
Mutual labels:  parallel
pimpable
No description or website provided.
Stars: ✭ 102 (+148.78%)
Mutual labels:  eloquent
orchestrator
The orchestrator executes all cypress specs across parallel docker containers based on a configuration file.
Stars: ✭ 61 (+48.78%)
Mutual labels:  parallel
web-scraping-engine
A simple web scraping engine supporting concurrent and anonymous scraping
Stars: ✭ 27 (-34.15%)
Mutual labels:  parallel
Parallel.GAMIT
Python wrapper to parallelize GAMIT executions
Stars: ✭ 22 (-46.34%)
Mutual labels:  parallel
laravel-local-class-scope
A tiny macro that reuse a global scope class as a local scope
Stars: ✭ 16 (-60.98%)
Mutual labels:  eloquent
LaraPersonate
Login as a different user quickly
Stars: ✭ 121 (+195.12%)
Mutual labels:  eloquent

Laravel Parallel

Latest Version on Packagist Tests Total Downloads

Laravel Parallel

Requirements

  • Laravel versions 5.7, 6.x, 7.x and 8.x
  • PHP 7.0 or greater

Installation

Install the package with composer:

composer require recca0120/laravel-parallel

Usage

  1. set a real database in phpunit.xml
<phpunit>
    <php>
        <!-- DB_CONNECTION can't be %memory% -->
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value="database/database.sqlite"/>
    </php>
</phpunit>
  1. make a product migration
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('quantity')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}
  1. define product model App\Models\Product
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Product.
 * @property int id
 * @property string name
 * @property int quantity
 * @package App\Models
 * @mixin Builder
 */
class Product extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'quantity'];

    protected $casts = ['quantity' => 'int'];
}
  1. define router in routes/web.php
<?php

use App\Models\Product;
use Illuminate\Support\Facades\Route;

Route::get('/product/{productId}', function ($productId) {
    return Product::findOrFail($productId);
});

Route::post('/product/{productId}', function ($productId) {
    $product = Product::findOrFail($productId);
    if ($product->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();
});
  1. testing
<?php

namespace Tests\Feature;

use App\Models\Product;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Recca0120\LaravelParallel\Tests\ParallelRequest;
use Tests\TestCase;

class RaceConditionTest extends TestCase
{
    use DatabaseMigrations;

    private $product;
    private $quantity = 10;

    public function setUp(): void
    {
        parent::setUp();
        $this->product = Product::create(['name' => 'test', 'quantity' => $this->quantity]);
    }

    public function test_race_condition()
    {
        $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_use_times_to_test_race_condition()
    {
        $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 for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].