Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Aug 21, 2024
1 parent 671d750 commit 5cd282d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/IdResolvers/CacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Glhd\Bits\IdResolvers;

use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\DateFactory;
use RuntimeException;

Expand All @@ -11,7 +12,7 @@ class CacheResolver extends IdResolver
protected ?int $value = null;

public function __construct(
protected CacheManager $cache,
protected Store&LockProvider $cache,
protected DateFactory $dates,
protected int $max = 0b1111111111,
) {
Expand All @@ -30,9 +31,10 @@ protected function acquire(): int

return $this->cache->lock('glhd-bits-ids:lock')
->block(5, function() {
$reserved = $this->cache->get('glhd-bits-ids:reserved', fn() => []);
$reserved = $this->cache->get('glhd-bits-ids:reserved') ?? [];
$id = $this->firstAvailable($reserved) ?? $this->findExpired($reserved);

if ($id = $this->firstAvailable($reserved) ?? $this->findExpired($reserved)) {
if (null !== $id) {
$reserved[$id] = $this->dates->now()->addHour()->unix();
$this->cache->forever('glhd-bits-ids:reserved', $reserved);
return $id;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/BitsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function register()

$this->app->singleton(CacheResolver::class, function(Container $container) {
$config = $container->make(Repository::class);
$cache = $container->make(CacheManager::class);
$cache = $container->make(CacheManager::class)->store();
$dates = $container->make(DateFactory::class);

$format = $config->get('bits.format', 'snowflake');
Expand Down
65 changes: 65 additions & 0 deletions tests/Unit/CacheIdResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Glhd\Bits\Tests\Unit;

use Glhd\Bits\IdResolvers\CacheResolver;
use Glhd\Bits\Tests\TestCase;
use Illuminate\Cache\ArrayStore;
use Illuminate\Support\DateFactory;
use Illuminate\Support\Facades\Date;
use RuntimeException;

class CacheIdResolverTest extends TestCase
{
public function test_it_increments_sequences(): void
{
Date::setTestNow(now());

$store = new ArrayStore();

// Should get 0b00
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$this->assertEquals(0b00, $resolver->get(1, 1)->first());
$this->assertEquals(0b00, $resolver->get(1, 1)->second());

Date::setTestNow(now()->addMinutes(10));

// Should get 0b01
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$this->assertEquals(0b00, $resolver->get(1, 1)->first());
$this->assertEquals(0b01, $resolver->get(1, 1)->second());

Date::setTestNow(now()->addMinutes(10));

// Should get 0b10
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$this->assertEquals(0b01, $resolver->get(1, 1)->first());
$this->assertEquals(0b00, $resolver->get(1, 1)->second());

Date::setTestNow(now()->addMinutes(10));

// Should get 0b11
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$this->assertEquals(0b01, $resolver->get(1, 1)->first());
$this->assertEquals(0b01, $resolver->get(1, 1)->second());

// Now we've run out of IDs… should throw
$this->assertThrows(function() use ($store) {
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$resolver->get(1, 1);
}, RuntimeException::class);

Date::setTestNow(now()->addMinutes(30));

// 0b00 has not expired, so we should be able to acquire it
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$this->assertEquals(0b00, $resolver->get(1, 1)->first());
$this->assertEquals(0b00, $resolver->get(1, 1)->second());

// But everything else is locked, so we should throw again
$this->assertThrows(function() use ($store) {
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11);
$resolver->get(1, 1);
}, RuntimeException::class);
}
}

0 comments on commit 5cd282d

Please sign in to comment.