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
27 changes: 16 additions & 11 deletions src/ClientOptions/PollingOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ class PollingOptions

/**
* Polling Options.
*
* @param float $initialDelaySec Initial delay (in seconds) before attempting to poll a queue.
* @param float $delaySec Delay (in seconds) between successive attempts to poll a queue.
* @param integer $maxRetries Maximum number of retries for a queue.
* @throws MindeeApiException Throws if any delay value is below the allowed minimum.
*/
public function __construct()
public function __construct(float $initialDelaySec = 2.0, float $delaySec = 1.5, int $maxRetries = 80)
{
$this->initialDelaySec = 2.0;
$this->delaySec = 1.5;
$this->maxRetries = 80;
$this->setInitialDelaySec($initialDelaySec);
$this->setDelaySec($delaySec);
$this->setMaxRetries($maxRetries);
}

/**
* @param integer $initialDelay Delay between polls.
* @param float $initialDelay Delay between polls.
* @return $this
* @throws MindeeApiException Throws if the initial parsing delay is less than 4 seconds.
* @throws MindeeApiException Throws if the initial parsing delay is less than the minimum.
*/
public function setInitialDelaySec(int $initialDelay): self
public function setInitialDelaySec(float $initialDelay): self
{
if ($initialDelay < MINIMUM_INITIAL_DELAY_SECONDS) {
throw new MindeeApiException(
Expand All @@ -56,11 +61,11 @@ public function setInitialDelaySec(int $initialDelay): self
}

/**
* @param integer $delay Delay between successive attempts to poll a queue.
* @param float $delay Delay between successive attempts to poll a queue.
* @return $this
* @throws MindeeApiException Throws if the delay is too low.
*/
public function setDelaySec(int $delay): self
public function setDelaySec(float $delay): self
{
if ($delay < MINIMUM_DELAY_SECONDS) {
throw new MindeeApiException(
Expand All @@ -78,11 +83,11 @@ public function setDelaySec(int $delay): self
*/
public function setMaxRetries(int $maxRetries): self
{
if (!$maxRetries || $maxRetries < 0) {
if ($maxRetries <= 0) {
$this->maxRetries = 80;
error_log("Notice: setting the amount of retries for auto-parsing to 80.");
} else {
$this->delaySec = $maxRetries;
$this->maxRetries = $maxRetries;
}
return $this;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/ClientOptions/PollingOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace ClientOptions;

use Mindee\ClientOptions\PollingOptions;
use PHPUnit\Framework\TestCase;

class PollingOptionsTest extends TestCase
{
public function testConstructorNoArguments(): void
{
$pollingOptions = new PollingOptions();
self::assertEquals(80, $pollingOptions->maxRetries);
self::assertEquals(1.5, $pollingOptions->delaySec);
self::assertEquals(2, $pollingOptions->initialDelaySec);
}

public function testConstructorSomeArguments(): void
{
$pollingOptions = new PollingOptions(maxRetries: 100);
self::assertEquals(100, $pollingOptions->maxRetries);
self::assertEquals(1.5, $pollingOptions->delaySec);
self::assertEquals(2, $pollingOptions->initialDelaySec);
}

public function testConstructorAllArguments(): void
{
// voluntarily passing arguments in a different order than the constructor
$pollingOptions = new PollingOptions(delaySec: 3.0, maxRetries: 100, initialDelaySec: 10);
self::assertEquals(100, $pollingOptions->maxRetries);
self::assertEquals(3, $pollingOptions->delaySec);
self::assertEquals(10.0, $pollingOptions->initialDelaySec);
}
}
41 changes: 24 additions & 17 deletions tests/V2/ClientV2TestFunctional.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace V2;

use Mindee\ClientOptions\PollingOptions;
use Mindee\Input\PathInput;
use Mindee\Input\UrlInputSource;
use Mindee\V2\Client;
Expand All @@ -29,9 +30,15 @@ protected function setUp(): void
public function testParseFileEmptyMultiPageMustSucceed(): void
{
$source = new PathInput(TestingUtilities::getFileTypesDir() . '/pdf/multipage_cut-2.pdf');
$inferenceParams = new ExtractionParameters($this->modelId, rag: false, rawText: true);

$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
$modelParams = new ExtractionParameters($this->modelId, rag: false, rawText: true);
$pollingOptions = new PollingOptions(maxRetries: 100);

$response = $this->mindeeClient->enqueueAndGetResult(
ExtractionResponse::class,
$source,
$modelParams,
$pollingOptions
);
self::assertNotNull($response);
$inference = $response->inference;
self::assertNotNull($inference);
Expand Down Expand Up @@ -67,9 +74,9 @@ public function testParseFileFilledSinglePageMustSucceed(): void
TestingUtilities::getV1DataDir() . '/products/financial_document/default_sample.jpg'
);

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

$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $modelParams);
self::assertNotNull($response);
$inference = $response->inference;
self::assertNotNull($inference);
Expand Down Expand Up @@ -100,10 +107,10 @@ public function testInvalidUUIDMustThrowError(): void

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

$inferenceParams = new ExtractionParameters('INVALID MODEL ID');
$modelParams = new ExtractionParameters('INVALID MODEL ID');

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

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

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

$inferenceParams = new ExtractionParameters(
$modelParams = new ExtractionParameters(
$this->modelId,
null,
null,
Expand All @@ -154,7 +161,7 @@ public function testInvalidWebhookIDsMustThrowError(): void
);

try {
$this->mindeeClient->enqueue($source, $inferenceParams);
$this->mindeeClient->enqueue($source, $modelParams);
} catch (MindeeV2HttpException $e) {
self::assertStringStartsWith('422-', $e->errorCode);
self::assertNotEmpty($e->title);
Expand All @@ -166,9 +173,9 @@ public function testUrlInputSourceMustNotRaiseErrors(): void
{
$urlSource = new UrlInputSource(getenv('MINDEE_V2_SE_TESTS_BLANK_PDF_URL'));

$inferenceParams = new ExtractionParameters($this->modelId);
$modelParams = new ExtractionParameters($this->modelId);

$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $urlSource, $inferenceParams);
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $urlSource, $modelParams);
self::assertNotNull($response);
$inference = $response->inference;
self::assertNotNull($inference);
Expand All @@ -190,9 +197,9 @@ public function testDataSchemaMustSucceed(): void
TestingUtilities::getV2DataDir() . '/products/extraction/data_schema_replace_param.json'
);

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

$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $inferenceParams);
$response = $this->mindeeClient->enqueueAndGetResult(ExtractionResponse::class, $source, $modelParams);
self::assertNotNull($response);
$inference = $response->inference;
self::assertNotNull($inference);
Expand Down Expand Up @@ -225,13 +232,13 @@ public function testMultipleWebhooksMustSucceed(): void
TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf'
);

$inferenceParams = new ExtractionParameters(
$modelParams = new ExtractionParameters(
$this->modelId,
webhookIds: [
getenv('MINDEE_V2_FAILURE_WEBHOOK_ID'),
getenv('MINDEE_V2_SE_TESTS_FAILURE_WEBHOOK_ID')]
);
$response = $this->mindeeClient->enqueue($source, $inferenceParams);
$response = $this->mindeeClient->enqueue($source, $modelParams);
self::assertCount(2, $response->job->webhooks);
}
}
Loading