diff --git a/composer.json b/composer.json index 0bcc4f5..b36b2bb 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,7 @@ "nikic/fast-route": "^1", "rdlowrey/auryn": "^1", "amphp/aerys": "^0.4.4", - "amphp/artax": "^2", - "amphp/redis": "^0.2.5" + "amphp/artax": "^2" }, "require-dev": { "phpunit/phpunit": "^5", diff --git a/composer.lock b/composer.lock index fb20e91..244b50f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "d48375fab4f366658bf074eed8f1aae5", + "content-hash": "0cc837c225ee09e27bb393acd076df43", "packages": [ { "name": "amphp/aerys", @@ -422,56 +422,6 @@ "homepage": "https://github.com/amphp/process", "time": "2016-09-24T10:49:26+00:00" }, - { - "name": "amphp/redis", - "version": "v0.2.5", - "source": { - "type": "git", - "url": "https://github.com/amphp/redis.git", - "reference": "a957abe7d257a205746e12dd29cb94dd6264fe46" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/amphp/redis/zipball/a957abe7d257a205746e12dd29cb94dd6264fe46", - "reference": "a957abe7d257a205746e12dd29cb94dd6264fe46", - "shasum": "" - }, - "require": { - "amphp/amp": "^1", - "amphp/socket": "^0.9" - }, - "require-dev": { - "fabpot/php-cs-fixer": "^1.9", - "phpunit/phpunit": "^4.8|^5.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "Amp\\Redis\\": "lib" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Niklas Keller", - "email": "me@kelunik.com", - "role": "Creator / Lead Developer" - } - ], - "description": "Async Redis client.", - "homepage": "https://github.com/amphp/redis", - "keywords": [ - "amp", - "amphp", - "async", - "client", - "redis" - ], - "time": "2016-05-06T19:03:43+00:00" - }, { "name": "amphp/socket", "version": "v0.9.9", diff --git a/server.php b/server.php index 76c7ceb..4aad439 100644 --- a/server.php +++ b/server.php @@ -5,7 +5,7 @@ use Auryn\Injector; use ekinhbayar\GitAmp\Github\Credentials; use ekinhbayar\GitAmp\Storage\Counter; -use ekinhbayar\GitAmp\Storage\RedisCounter; +use ekinhbayar\GitAmp\Storage\NativeCounter; use ekinhbayar\GitAmp\Websocket\Handler; use Monolog\Logger; use Monolog\Handler\StreamHandler; @@ -14,13 +14,11 @@ use function Aerys\router; use function Aerys\websocket; -//require_once __DIR__ . '/vendor/autoload.php'; - $configuration = require_once __DIR__ . '/config.php'; $injector = new Injector; -$injector->alias(Counter::class, RedisCounter::class); +$injector->alias(Counter::class, NativeCounter::class); $injector->alias(Credentials::class, get_class($configuration['github'])); diff --git a/src/Storage/Counter.php b/src/Storage/Counter.php index 112cd84..c0a99c5 100644 --- a/src/Storage/Counter.php +++ b/src/Storage/Counter.php @@ -4,9 +4,13 @@ use Amp\Promise; -interface Counter { - public function increment(string $key): Promise; - public function decrement(string $key): Promise; - public function get(string $key): Promise; - public function set(string $key, int $val): Promise; +interface Counter +{ + public function increment(); + + public function decrement(); + + public function get(): int; + + public function set(int $val); } diff --git a/src/Storage/NativeCounter.php b/src/Storage/NativeCounter.php new file mode 100644 index 0000000..35c8301 --- /dev/null +++ b/src/Storage/NativeCounter.php @@ -0,0 +1,28 @@ +counter++; + } + + public function decrement() + { + $this->counter--; + } + + public function get(): int + { + return $this->counter; + } + + public function set(int $val) + { + $this->counter = $val; + } +} diff --git a/src/Storage/RedisCounter.php b/src/Storage/RedisCounter.php deleted file mode 100644 index fea08fc..0000000 --- a/src/Storage/RedisCounter.php +++ /dev/null @@ -1,59 +0,0 @@ -redis = $redis; - } - - public function increment(string $key): Promise { - return resolve(function() use ($key) { - try { - return yield $this->redis->incr($key); - } catch (RedisException $e) { - throw new StorageFailedException('Failed to increment counter.', 0, $e); - } - }); - } - - public function decrement(string $key): Promise { - return resolve(function() use ($key) { - try { - return yield $this->redis->decr($key); - } catch (RedisException $e) { - throw new StorageFailedException('Failed to decrement counter.', 0, $e); - } - }); - } - - public function get(string $key): Promise { - return resolve(function() use ($key) { - try { - $result = yield $this->redis->get($key); - - return empty($result) ? 0 : (int) $result; - } catch (RedisException $e) { - throw new StorageFailedException('Failed to get counter.', 0, $e); - } - }); - } - - public function set(string $key, int $val): Promise { - return resolve(function() use ($key, $val) { - try { - return yield $this->redis->set($key, $val); - } catch (RedisException $e) { - throw new StorageFailedException('Failed to set counter value.', 0, $e); - } - }); - } -} diff --git a/src/Storage/StorageFailedException.php b/src/Storage/StorageFailedException.php deleted file mode 100644 index 3023099..0000000 --- a/src/Storage/StorageFailedException.php +++ /dev/null @@ -1,5 +0,0 @@ -endpoint = $endpoint; - $this->counter->set('connected_users', 0); + $this->counter->set(0); repeat(function() { $this->emit(yield $this->gitamp->listen()); @@ -55,9 +55,9 @@ public function onOpen(int $clientId, $handshakeData) { $this->emit(yield $this->gitamp->listen()); - yield $this->counter->increment('connected_users'); + $this->counter->increment(); - $this->sendConnectedUsersCount(yield $this->counter->get('connected_users')); + $this->sendConnectedUsersCount($this->counter->get()); } private function emit(Results $events) @@ -81,9 +81,9 @@ public function onData(int $clientId, Websocket\Message $msg) public function onClose(int $clientId, int $code, string $reason) { - yield $this->counter->decrement('connected_users'); + $this->counter->decrement(); - $this->sendConnectedUsersCount(yield $this->counter->get('connected_users')); + $this->sendConnectedUsersCount($this->counter->get()); } public function onStop() diff --git a/tests/Storage/NativeCounterTest.php b/tests/Storage/NativeCounterTest.php new file mode 100644 index 0000000..dde8846 --- /dev/null +++ b/tests/Storage/NativeCounterTest.php @@ -0,0 +1,61 @@ +assertSame(0, $nativeCounter->get()); + } + + public function testSet() + { + $nativeCounter = new NativeCounter(); + + $nativeCounter->set(5); + + $this->assertSame(5, $nativeCounter->get()); + } + + public function testIncrement() + { + $nativeCounter = new NativeCounter(); + + $nativeCounter->increment(); + + $this->assertSame(1, $nativeCounter->get()); + + $nativeCounter->increment(); + + $this->assertSame(2, $nativeCounter->get()); + + $nativeCounter->increment(); + + $this->assertSame(3, $nativeCounter->get()); + } + + public function testDecrement() + { + $nativeCounter = new NativeCounter(); + + $nativeCounter->set(5); + + $nativeCounter->decrement(); + + $this->assertSame(4, $nativeCounter->get()); + + $nativeCounter->decrement(); + + $this->assertSame(3, $nativeCounter->get()); + + $nativeCounter->decrement(); + + $this->assertSame(2, $nativeCounter->get()); + } +} diff --git a/tests/Storage/RedisCounterTest.php b/tests/Storage/RedisCounterTest.php deleted file mode 100644 index 90353a6..0000000 --- a/tests/Storage/RedisCounterTest.php +++ /dev/null @@ -1,141 +0,0 @@ -client = $this->createMock(Client::class); - } - - public function testGet() - { - $this->client - ->expects($this->exactly(2)) - ->method('get') - ->with($this->equalTo('test')) - ->will($this->returnValue(new Success(10))) - ; - - $redisCounter = new RedisCounter($this->client); - - $this->assertInstanceOf(Promise::class, $redisCounter->get('test')); - - $this->assertSame(10, wait($redisCounter->get('test'))); - } - - public function testSet() - { - $this->client - ->expects($this->once()) - ->method('set') - ->with($this->equalTo('test'), $this->equalTo(5)) - ; - - $redisCounter = new RedisCounter($this->client); - - wait($redisCounter->set('test', 5)); - } - - public function testIncrement() - { - $this->client - ->expects($this->once()) - ->method('incr') - ->with($this->equalTo('test')) - ; - - $redisCounter = new RedisCounter($this->client); - - wait($redisCounter->increment('test')); - } - - public function testDecrement() - { - $this->client - ->expects($this->once()) - ->method('decr') - ->with($this->equalTo('test')) - ; - - $redisCounter = new RedisCounter($this->client); - - wait($redisCounter->decrement('test')); - } - - public function testSetThrowsOnFailedStorage() - { - $this->client - ->expects($this->once()) - ->method('set') - ->will($this->throwException(new RedisException())) - ; - - $redisCounter = new RedisCounter($this->client); - - $this->expectException(StorageFailedException::class); - $this->expectExceptionMessage('Failed to set counter value.'); - - wait($redisCounter->set('foo', 2)); - } - - public function testGetThrowsOnFailedStorage() - { - $this->client - ->expects($this->once()) - ->method('get') - ->will($this->throwException(new RedisException())) - ; - - $redisCounter = new RedisCounter($this->client); - - $this->expectException(StorageFailedException::class); - $this->expectExceptionMessage('Failed to get counter.'); - - wait($redisCounter->get('foo')); - } - - public function testIncrementThrowsOnFailedStorage() - { - $this->client - ->expects($this->once()) - ->method('incr') - ->will($this->throwException(new RedisException())) - ; - - $redisCounter = new RedisCounter($this->client); - - $this->expectException(StorageFailedException::class); - $this->expectExceptionMessage('Failed to increment counter.'); - - wait($redisCounter->increment('foo')); - } - - public function testDecrementThrowsOnFailedStorage() - { - $this->client - ->expects($this->once()) - ->method('decr') - ->will($this->throwException(new RedisException())) - ; - - $redisCounter = new RedisCounter($this->client); - - $this->expectException(StorageFailedException::class); - $this->expectExceptionMessage('Failed to decrement counter.'); - - wait($redisCounter->decrement('foo')); - } -} diff --git a/tests/Websocket/HandlerTest.php b/tests/Websocket/HandlerTest.php index 32d86d8..32b8f8c 100644 --- a/tests/Websocket/HandlerTest.php +++ b/tests/Websocket/HandlerTest.php @@ -24,15 +24,15 @@ class HandlerTest extends TestCase public function setUp() { $this->counter = $this->createMock(Counter::class); - $this->origin = 'https://gitamp.audio'; - $this->gitamp = $this->createMock(GitAmp::class); + $this->origin = 'https://gitamp.audio'; + $this->gitamp = $this->createMock(GitAmp::class); } public function testOnHandshakeReturnsForbiddenOnInvalidOrigin() { $handler = new Handler($this->counter, $this->origin, $this->gitamp); - $request = $this->createMock(Request::class); + $request = $this->createMock(Request::class); $response = $this->createMock(Response::class); $request @@ -40,20 +40,17 @@ public function testOnHandshakeReturnsForbiddenOnInvalidOrigin() ->method('getHeader') // this is testing the actual implementation which is actually not needed ->with('origin') - ->will($this->returnValue('https://notgitamp.audio')) - ; + ->will($this->returnValue('https://notgitamp.audio')); $response ->expects($this->once()) ->method('setStatus') - ->with(403) - ; + ->with(403); $response ->expects($this->once()) ->method('end') - ->with('

origin not allowed

') - ; + ->with('

origin not allowed

'); $this->assertNull($handler->onHandshake($request, $response)); } @@ -62,7 +59,7 @@ public function testOnHandshakeReturnsClientAddress() { $handler = new Handler($this->counter, $this->origin, $this->gitamp); - $request = $this->createMock(Request::class); + $request = $this->createMock(Request::class); $response = $this->createMock(Response::class); $request @@ -70,14 +67,12 @@ public function testOnHandshakeReturnsClientAddress() ->method('getHeader') // this is testing the actual implementation which is actually not needed ->with('origin') - ->will($this->returnValue($this->origin)) - ; + ->will($this->returnValue($this->origin)); $request ->expects($this->once()) ->method('getConnectionInfo') - ->will($this->returnValue(['client_addr' => '127.0.0.1'])) - ; + ->will($this->returnValue(['client_addr' => '127.0.0.1'])); $this->assertSame('127.0.0.1', $handler->onHandshake($request, $response)); } @@ -87,26 +82,23 @@ public function testOnStartResetsConnectedUserCounter() $this->counter ->expects($this->once()) ->method('set') - ->with('connected_users', 0) - ; + ->with(0); $results = $this->createMock(Results::class); $results ->expects($this->once()) ->method('hasEvents') - ->will($this->returnValue(false)) - ; + ->will($this->returnValue(false)); $this->gitamp ->expects($this->once()) ->method('listen') - ->will($this->returnValue(new Success($results))) - ; + ->will($this->returnValue(new Success($results))); $handler = new Handler($this->counter, $this->origin, $this->gitamp); - \Amp\run(function() use ($handler) { + \Amp\run(function () use ($handler) { yield $handler->onStart($this->createMock(Endpoint::class)); \Amp\repeat('\Amp\stop', 25000); @@ -129,14 +121,14 @@ public function testOnCloseDecrementsUserCount() { $this->counter ->expects($this->once()) - ->method('decrement') - ->with('connected_users') - ; + ->method('decrement'); $handler = new Handler($this->counter, $this->origin, $this->gitamp); - \Amp\run(function() use ($handler) { - yield from $handler->onClose(1, 0, 'foo'); + \Amp\run(function () use ($handler) { + $handler->onStart($this->createMock(Endpoint::class)); + + $handler->onClose(1, 0, 'foo'); \Amp\repeat('\Amp\stop', 27000); });