Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: PHPUnit Tests

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Build the Docker image
run: make build

- name: Install dependencies
run: make install

- name: Run test suite (except long ones)
run: make test

- name: Run test suite (only long ones)
run: make test-long
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor/
composer.lock
.idea/
.php_cs.cache
.*.cache
25 changes: 25 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

return new PhpCsFixer\Config()
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@Symfony' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'string_implicit_backslashes' => true,
'list_syntax' => ['syntax' => 'short'],
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'],
'ordered_imports' => true,
'phpdoc_to_comment' => false,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters']],
'visibility_required' => ['elements' => ['property', 'method', 'const']],
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
)
;
35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM php:8.4-cli

# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
libssl-dev \
libcurl4-openssl-dev \
pkg-config \
openjdk-17-jre-headless \
&& rm -rf /var/lib/apt/lists/*

# Install MongoDB extension
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb

# Copy Composer from official image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /app

# Copy composer files
COPY composer.json composer.lock* ./

# Install dependencies including dev dependencies for testing
RUN composer install --optimize-autoloader

# Copy application code
COPY . .

# Set environment variable for Composer
ENV COMPOSER_ALLOW_SUPERUSER=1

CMD ["tail", "-f", "/dev/null"]
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.PHONY: build up down test test-long fix-cs install shell logs clean

# Build the Docker image
build:
docker compose build

# Start the services
up:
docker compose up -d

# Stop the services
down:
docker compose down

# Install dependencies
install:
docker compose run --rm php composer install

# Run all tests except the long ones
test: up
docker compose exec php vendor/bin/phpunit --exclude-group=long

# Run long tests specifically
test-long: up
docker compose exec php vendor/bin/phpunit --group=long

fix-cs: up
docker compose exec php vendor/bin/php-cs-fixer fix -v

# Open a shell in the PHP container
shell:
docker compose exec php bash

# View logs
logs:
docker compose logs -f php

# Clean up containers and volumes
clean:
docker compose down -v
docker compose rm -f
23 changes: 23 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
php:
build: .
working_dir: /app
volumes:
- .:/app
environment:
- COMPOSER_ALLOW_SUPERUSER=1
- MONGODB_URI=mongodb://mongodb:27017/concurrency
depends_on:
- mongodb

mongodb:
image: mongo:7
container_name: concurrency_mongodb
restart: unless-stopped
ports:
- "27017:27017"
volumes:
- mongodb_data:/data/db

volumes:
mongodb_data:
29 changes: 18 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
"description": "MongoDB-based locking system",
"license": "MIT",
"require": {
"php": "^7.2",
"ext-mongodb": ">=1.2",
"recruiterphp/clock": "^3",
"mongodb/mongodb": "^1.4"
"php": "^8.4",
"ext-mongodb": "*",
"mongodb/mongodb": "^2.1",
"recruiterphp/clock": "^4.1"
},
"require-dev": {
"phpunit/phpunit": "@stable",
"phake/phake": "~2.1",
"giorgiosironi/eris": "dev-master",
"symfony/process": "~4.0",
"friendsofphp/php-cs-fixer": "^2.13"
"ergebnis/composer-normalize": "^2.47",
"friendsofphp/php-cs-fixer": "^3.85",
"giorgiosironi/eris": "^1.0",
"phake/phake": "^4.6",
"phpunit/phpunit": "^12.3",
"symfony/process": "^7.3"
},
"replace": {
"easywelfare/onebip-concurrency": "self.version",
"onebip/onebip-concurrency": "self.version"
},
"autoload": {
"psr-0": {
"Recruiter\\": "src/"
}
},
"replace": {
"recruiterphp/concurrency": "self.version"
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
}
}
}
14 changes: 13 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<phpunit colors="true" backupGlobals="false">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.3/phpunit.xsd"
backupGlobals="false"
colors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnPhpunitDeprecations="true"
bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions src/Recruiter/Concurrency/Idle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Recruiter\Concurrency;

class Idle
Expand Down
9 changes: 4 additions & 5 deletions src/Recruiter/Concurrency/InProcessRetry.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace Recruiter\Concurrency;
declare(strict_types=1);

use Exception;
use InvalidArgumentException;
namespace Recruiter\Concurrency;

class InProcessRetry
{
Expand Down Expand Up @@ -35,12 +34,12 @@ public function __invoke()
for ($i = 0; $i <= $possibleRetries; ++$i) {
try {
return call_user_func($this->what);
} catch (Exception $e) {
} catch (\Exception $e) {
if (!($e instanceof $this->exceptionClass)) {
throw $e;
}
}
}
throw $e ?? new InvalidArgumentException("Invalid number of retries: $possibleRetries");
throw $e ?? new \InvalidArgumentException("Invalid number of retries: $possibleRetries");
}
}
2 changes: 2 additions & 0 deletions src/Recruiter/Concurrency/Leadership.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Recruiter\Concurrency;

class Leadership
Expand Down
13 changes: 6 additions & 7 deletions src/Recruiter/Concurrency/Lock.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Recruiter\Concurrency;

interface Lock
{
/**
* @throws LockNotAvailableException
*
* @param int $duration (in seconds)
*
* @throws LockNotAvailableException
*/
public function acquire($duration = 360): void;

Expand All @@ -17,9 +19,9 @@ public function acquire($duration = 360): void;
public function release($force = false): void;

/**
* @throws LockNotavailableexception
*
* @param int $duration (in seconds)
*
* @throws LockNotAvailableException
*/
public function refresh($duration = 3600): void;

Expand All @@ -34,8 +36,5 @@ public function show(): ?array;
*/
public function wait($polling = 30, $maximumWaitingTime = 3600): void;

/**
* @return string
*/
public function __toString(): string;
}
6 changes: 3 additions & 3 deletions src/Recruiter/Concurrency/LockNotAvailableException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Recruiter\Concurrency;
declare(strict_types=1);

use RuntimeException;
namespace Recruiter\Concurrency;

class LockNotAvailableException extends RuntimeException
class LockNotAvailableException extends \RuntimeException
{
}
Loading