Skip to content

Commit 61e18ab

Browse files
authored
[FX] Fixing dealing with exceptions wrapped in fulfilled promises (#15)
1 parent 965f4a0 commit 61e18ab

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ php:
1313

1414
env:
1515
global:
16-
- DOCKER_COMPOSE_VERSION=1.13.0
16+
- DOCKER_COMPOSE_VERSION=1.19.0
1717
matrix:
1818
- DEPENDENCIES="low" INTEGRATION_TEST="enabled"
1919
- DEPENDENCIES="low" INTEGRATION_TEST="disabled"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ quick-test:
44

55
integration-test:
66
docker-compose up -d
7-
sleep 10
7+
sleep 20
88
./vendor/bin/phpunit -c phpunit.xml.integration.dist -v --group integration
99
docker-compose down
1010

composer.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
"email": "thomas.ploch@flixbus.com"
1111
}
1212
],
13-
"repositories": [
14-
{
15-
"type": "vcs",
16-
"url": "git@github.com:flix-tech/avro-php.git"
17-
}
18-
],
1913
"require": {
2014
"php": "~7.0",
2115
"guzzlehttp/guzzle": "~6.3",

docker-compose.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ services:
1414
ipv4_address: 172.25.0.101
1515

1616
broker:
17-
image: confluentinc/cp-enterprise-kafka
17+
image: confluentinc/cp-kafka
1818
hostname: broker
1919
depends_on:
2020
- zookeeper
@@ -24,12 +24,6 @@ services:
2424
KAFKA_BROKER_ID: 1
2525
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
2626
KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker:9092'
27-
KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
28-
CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:9092
29-
CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181
30-
CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
31-
CONFLUENT_METRICS_ENABLE: 'true'
32-
CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'
3327
networks:
3428
confluent-net:
3529
ipv4_address: 172.25.0.102

src/Registry/CachedRegistry.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace FlixTech\SchemaRegistryApi\Registry;
66

77
use AvroSchema;
8+
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;
89
use FlixTech\SchemaRegistryApi\Registry;
910
use GuzzleHttp\Promise\PromiseInterface;
1011

@@ -47,7 +48,11 @@ public function __construct(Registry $registry, CacheAdapter $cacheAdapter, call
4748
*/
4849
public function register(string $subject, AvroSchema $schema, callable $requestCallback = null)
4950
{
50-
$closure = function (int $schemaId) use ($schema) {
51+
$closure = function ($schemaId) use ($schema) {
52+
if ($schemaId instanceof SchemaRegistryException) {
53+
return $schemaId;
54+
}
55+
5156
$this->cacheAdapter->cacheSchemaWithId($schema, $schemaId);
5257
$this->cacheAdapter->cacheSchemaIdByHash($schemaId, $this->getSchemaHash($schema));
5358

@@ -68,7 +73,11 @@ function (PromiseInterface $promise) use ($closure) {
6873
*/
6974
public function schemaVersion(string $subject, AvroSchema $schema, callable $requestCallback = null)
7075
{
71-
$closure = function (int $version) use ($schema, $subject) {
76+
$closure = function ($version) use ($schema, $subject) {
77+
if ($version instanceof SchemaRegistryException) {
78+
return $version;
79+
}
80+
7281
$this->cacheAdapter->cacheSchemaWithSubjectAndVersion($schema, $subject, $version);
7382

7483
return $version;
@@ -94,7 +103,11 @@ public function schemaId(string $subject, AvroSchema $schema, callable $requestC
94103
return $this->cacheAdapter->getIdWithHash($schemaHash);
95104
}
96105

97-
$closure = function (int $schemaId) use ($schema, $schemaHash) {
106+
$closure = function ($schemaId) use ($schema, $schemaHash) {
107+
if ($schemaId instanceof SchemaRegistryException) {
108+
return $schemaId;
109+
}
110+
98111
$this->cacheAdapter->cacheSchemaWithId($schema, $schemaId);
99112
$this->cacheAdapter->cacheSchemaIdByHash($schemaId, $schemaHash);
100113

@@ -119,7 +132,11 @@ public function schemaForId(int $schemaId, callable $requestCallback = null)
119132
return $this->cacheAdapter->getWithId($schemaId);
120133
}
121134

122-
$closure = function (AvroSchema $schema) use ($schemaId) {
135+
$closure = function ($schema) use ($schemaId) {
136+
if ($schema instanceof SchemaRegistryException) {
137+
return $schema;
138+
}
139+
123140
$this->cacheAdapter->cacheSchemaWithId($schema, $schemaId);
124141
$this->cacheAdapter->cacheSchemaIdByHash($schemaId, $this->getSchemaHash($schema));
125142

@@ -144,7 +161,11 @@ public function schemaForSubjectAndVersion(string $subject, int $version, callab
144161
return $this->cacheAdapter->getWithSubjectAndVersion($subject, $version);
145162
}
146163

147-
$closure = function (AvroSchema $schema) use ($subject, $version) {
164+
$closure = function ($schema) use ($subject, $version) {
165+
if ($schema instanceof SchemaRegistryException) {
166+
return $schema;
167+
}
168+
148169
$this->cacheAdapter->cacheSchemaWithSubjectAndVersion($schema, $subject, $version);
149170

150171
return $schema;
@@ -184,6 +205,6 @@ private function applyValueHandlers($value, callable $promiseHandler, callable $
184205

185206
private function getSchemaHash(AvroSchema $schema): string
186207
{
187-
return call_user_func($this->hashAlgoFunc, $schema);
208+
return \call_user_func($this->hashAlgoFunc, $schema);
188209
}
189210
}

test/Registry/CachedRegistryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace FlixTech\SchemaRegistryApi\Test\Registry;
66

77
use AvroSchema;
8+
use FlixTech\SchemaRegistryApi\Exception\SubjectNotFoundException;
89
use FlixTech\SchemaRegistryApi\Registry;
910
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter;
1011
use FlixTech\SchemaRegistryApi\Registry\CachedRegistry;
@@ -381,4 +382,25 @@ public function it_should_not_cache_latest_version_calls()
381382

382383
$this->assertEquals($this->schema, $this->cachedRegistry->latestVersion($this->subject));
383384
}
385+
386+
/**
387+
* @test
388+
*/
389+
public function it_should_handle_exceptions_wrapped_in_promises_correctly()
390+
{
391+
$subjectNotFoundException = new SubjectNotFoundException();
392+
393+
$promise = new FulfilledPromise($subjectNotFoundException);
394+
395+
$this->registryMock
396+
->expects($this->once())
397+
->method('register')
398+
->with($this->subject, $this->schema)
399+
->willReturn($promise);
400+
401+
$this->assertEquals(
402+
$this->cachedRegistry->register($this->subject, $this->schema)->wait(),
403+
$subjectNotFoundException
404+
);
405+
}
384406
}

0 commit comments

Comments
 (0)