Skip to content

Commit 515d1ef

Browse files
authored
🐛 polling options constructor accepts values (#202)
1 parent 57ec68c commit 515d1ef

3 files changed

Lines changed: 76 additions & 28 deletions

File tree

src/ClientOptions/PollingOptions.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ class PollingOptions
3030

3131
/**
3232
* Polling Options.
33+
*
34+
* @param float $initialDelaySec Initial delay (in seconds) before attempting to poll a queue.
35+
* @param float $delaySec Delay (in seconds) between successive attempts to poll a queue.
36+
* @param integer $maxRetries Maximum number of retries for a queue.
37+
* @throws MindeeApiException Throws if any delay value is below the allowed minimum.
3338
*/
34-
public function __construct()
39+
public function __construct(float $initialDelaySec = 2.0, float $delaySec = 1.5, int $maxRetries = 80)
3540
{
36-
$this->initialDelaySec = 2.0;
37-
$this->delaySec = 1.5;
38-
$this->maxRetries = 80;
41+
$this->setInitialDelaySec($initialDelaySec);
42+
$this->setDelaySec($delaySec);
43+
$this->setMaxRetries($maxRetries);
3944
}
4045

4146
/**
42-
* @param integer $initialDelay Delay between polls.
47+
* @param float $initialDelay Delay between polls.
4348
* @return $this
44-
* @throws MindeeApiException Throws if the initial parsing delay is less than 4 seconds.
49+
* @throws MindeeApiException Throws if the initial parsing delay is less than the minimum.
4550
*/
46-
public function setInitialDelaySec(int $initialDelay): self
51+
public function setInitialDelaySec(float $initialDelay): self
4752
{
4853
if ($initialDelay < MINIMUM_INITIAL_DELAY_SECONDS) {
4954
throw new MindeeApiException(
@@ -56,11 +61,11 @@ public function setInitialDelaySec(int $initialDelay): self
5661
}
5762

5863
/**
59-
* @param integer $delay Delay between successive attempts to poll a queue.
64+
* @param float $delay Delay between successive attempts to poll a queue.
6065
* @return $this
6166
* @throws MindeeApiException Throws if the delay is too low.
6267
*/
63-
public function setDelaySec(int $delay): self
68+
public function setDelaySec(float $delay): self
6469
{
6570
if ($delay < MINIMUM_DELAY_SECONDS) {
6671
throw new MindeeApiException(
@@ -78,11 +83,11 @@ public function setDelaySec(int $delay): self
7883
*/
7984
public function setMaxRetries(int $maxRetries): self
8085
{
81-
if (!$maxRetries || $maxRetries < 0) {
86+
if ($maxRetries <= 0) {
8287
$this->maxRetries = 80;
8388
error_log("Notice: setting the amount of retries for auto-parsing to 80.");
8489
} else {
85-
$this->delaySec = $maxRetries;
90+
$this->maxRetries = $maxRetries;
8691
}
8792
return $this;
8893
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClientOptions;
6+
7+
use Mindee\ClientOptions\PollingOptions;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PollingOptionsTest extends TestCase
11+
{
12+
public function testConstructorNoArguments(): void
13+
{
14+
$pollingOptions = new PollingOptions();
15+
self::assertEquals(80, $pollingOptions->maxRetries);
16+
self::assertEquals(1.5, $pollingOptions->delaySec);
17+
self::assertEquals(2, $pollingOptions->initialDelaySec);
18+
}
19+
20+
public function testConstructorSomeArguments(): void
21+
{
22+
$pollingOptions = new PollingOptions(maxRetries: 100);
23+
self::assertEquals(100, $pollingOptions->maxRetries);
24+
self::assertEquals(1.5, $pollingOptions->delaySec);
25+
self::assertEquals(2, $pollingOptions->initialDelaySec);
26+
}
27+
28+
public function testConstructorAllArguments(): void
29+
{
30+
// voluntarily passing arguments in a different order than the constructor
31+
$pollingOptions = new PollingOptions(delaySec: 3.0, maxRetries: 100, initialDelaySec: 10);
32+
self::assertEquals(100, $pollingOptions->maxRetries);
33+
self::assertEquals(3, $pollingOptions->delaySec);
34+
self::assertEquals(10.0, $pollingOptions->initialDelaySec);
35+
}
36+
}

tests/V2/ClientV2TestFunctional.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace V2;
66

7+
use Mindee\ClientOptions\PollingOptions;
78
use Mindee\Input\PathInput;
89
use Mindee\Input\UrlInputSource;
910
use Mindee\V2\Client;
@@ -29,9 +30,15 @@ protected function setUp(): void
2930
public function testParseFileEmptyMultiPageMustSucceed(): void
3031
{
3132
$source = new PathInput(TestingUtilities::getFileTypesDir() . '/pdf/multipage_cut-2.pdf');
32-
$inferenceParams = new ExtractionParameters($this->modelId, rag: false, rawText: true);
33-
34-
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
33+
$modelParams = new ExtractionParameters($this->modelId, rag: false, rawText: true);
34+
$pollingOptions = new PollingOptions(maxRetries: 100);
35+
36+
$response = $this->mindeeClient->enqueueAndGetResult(
37+
ExtractionResponse::class,
38+
$source,
39+
$modelParams,
40+
$pollingOptions
41+
);
3542
self::assertNotNull($response);
3643
$inference = $response->inference;
3744
self::assertNotNull($inference);
@@ -67,9 +74,9 @@ public function testParseFileFilledSinglePageMustSucceed(): void
6774
TestingUtilities::getV1DataDir() . '/products/financial_document/default_sample.jpg'
6875
);
6976

70-
$inferenceParams = new ExtractionParameters($this->modelId, rag: false, textContext: 'this is an invoice');
77+
$modelParams = new ExtractionParameters($this->modelId, rag: false, textContext: 'this is an invoice');
7178

72-
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
79+
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $modelParams);
7380
self::assertNotNull($response);
7481
$inference = $response->inference;
7582
self::assertNotNull($inference);
@@ -100,10 +107,10 @@ public function testInvalidUUIDMustThrowError(): void
100107

101108
$source = new PathInput(TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf');
102109

103-
$inferenceParams = new ExtractionParameters('INVALID MODEL ID');
110+
$modelParams = new ExtractionParameters('INVALID MODEL ID');
104111

105112
try {
106-
$this->mindeeClient->enqueue($source, $inferenceParams);
113+
$this->mindeeClient->enqueue($source, $modelParams);
107114
} catch (MindeeV2HttpException $e) {
108115
self::assertStringStartsWith('422-', $e->errorCode);
109116
self::assertNotEmpty($e->title);
@@ -115,10 +122,10 @@ public function testUnknownModelMustThrowError(): void
115122
{
116123
$source = new PathInput(TestingUtilities::getFileTypesDir() . '/pdf/multipage_cut-2.pdf');
117124

118-
$inferenceParams = new ExtractionParameters('fc405e37-4ba4-4d03-aeba-533a8d1f0f21', textContext: 'this is invalid');
125+
$modelParams = new ExtractionParameters('fc405e37-4ba4-4d03-aeba-533a8d1f0f21', textContext: 'this is invalid');
119126

120127
try {
121-
$this->mindeeClient->enqueue($source, $inferenceParams);
128+
$this->mindeeClient->enqueue($source, $modelParams);
122129
} catch (MindeeV2HttpException $e) {
123130
self::assertStringStartsWith('404-', $e->errorCode);
124131
self::assertNotEmpty($e->title);
@@ -142,7 +149,7 @@ public function testInvalidWebhookIDsMustThrowError(): void
142149
{
143150
$source = new PathInput(TestingUtilities::getFileTypesDir() . '/pdf/multipage_cut-2.pdf');
144151

145-
$inferenceParams = new ExtractionParameters(
152+
$modelParams = new ExtractionParameters(
146153
$this->modelId,
147154
null,
148155
null,
@@ -154,7 +161,7 @@ public function testInvalidWebhookIDsMustThrowError(): void
154161
);
155162

156163
try {
157-
$this->mindeeClient->enqueue($source, $inferenceParams);
164+
$this->mindeeClient->enqueue($source, $modelParams);
158165
} catch (MindeeV2HttpException $e) {
159166
self::assertStringStartsWith('422-', $e->errorCode);
160167
self::assertNotEmpty($e->title);
@@ -166,9 +173,9 @@ public function testUrlInputSourceMustNotRaiseErrors(): void
166173
{
167174
$urlSource = new UrlInputSource(getenv('MINDEE_V2_SE_TESTS_BLANK_PDF_URL'));
168175

169-
$inferenceParams = new ExtractionParameters($this->modelId);
176+
$modelParams = new ExtractionParameters($this->modelId);
170177

171-
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $urlSource, $inferenceParams);
178+
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $urlSource, $modelParams);
172179
self::assertNotNull($response);
173180
$inference = $response->inference;
174181
self::assertNotNull($inference);
@@ -190,9 +197,9 @@ public function testDataSchemaMustSucceed(): void
190197
TestingUtilities::getV2DataDir() . '/products/extraction/data_schema_replace_param.json'
191198
);
192199

193-
$inferenceParams = new ExtractionParameters($this->modelId, dataSchema: $dataSchemaReplace);
200+
$modelParams = new ExtractionParameters($this->modelId, dataSchema: $dataSchemaReplace);
194201

195-
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
202+
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $modelParams);
196203
self::assertNotNull($response);
197204
$inference = $response->inference;
198205
self::assertNotNull($inference);
@@ -225,13 +232,13 @@ public function testMultipleWebhooksMustSucceed(): void
225232
TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf'
226233
);
227234

228-
$inferenceParams = new ExtractionParameters(
235+
$modelParams = new ExtractionParameters(
229236
$this->modelId,
230237
webhookIds: [
231238
getenv('MINDEE_V2_FAILURE_WEBHOOK_ID'),
232239
getenv('MINDEE_V2_SE_TESTS_FAILURE_WEBHOOK_ID')]
233240
);
234-
$response = $this->mindeeClient->enqueue($source, $inferenceParams);
241+
$response = $this->mindeeClient->enqueue($source, $modelParams);
235242
self::assertCount(2, $response->job->webhooks);
236243
}
237244
}

0 commit comments

Comments
 (0)