diff --git a/src/ClientOptions/PollingOptions.php b/src/ClientOptions/PollingOptions.php index a7ded11a..ac2f25bc 100644 --- a/src/ClientOptions/PollingOptions.php +++ b/src/ClientOptions/PollingOptions.php @@ -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( @@ -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( @@ -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; } diff --git a/tests/ClientOptions/PollingOptionsTest.php b/tests/ClientOptions/PollingOptionsTest.php new file mode 100644 index 00000000..53fa5122 --- /dev/null +++ b/tests/ClientOptions/PollingOptionsTest.php @@ -0,0 +1,36 @@ +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); + } +} diff --git a/tests/V2/ClientV2TestFunctional.php b/tests/V2/ClientV2TestFunctional.php index ea23eb7d..b3fee34a 100644 --- a/tests/V2/ClientV2TestFunctional.php +++ b/tests/V2/ClientV2TestFunctional.php @@ -4,6 +4,7 @@ namespace V2; +use Mindee\ClientOptions\PollingOptions; use Mindee\Input\PathInput; use Mindee\Input\UrlInputSource; use Mindee\V2\Client; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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, @@ -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); @@ -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); @@ -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); @@ -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); } }