|
8 | 8 | use Typesense\Exceptions\ServerError; |
9 | 9 | use Typesense\Exceptions\RequestMalformed; |
10 | 10 | use Typesense\Exceptions\ObjectNotFound; |
| 11 | +use Typesense\Exceptions\ServiceUnavailable; |
11 | 12 | use Http\Client\Exception\HttpException; |
12 | 13 | use Http\Client\Exception\TransferException; |
13 | 14 | use JsonException; |
@@ -54,7 +55,8 @@ public function testExceptionsCarryTheHttpStatusCode(): void |
54 | 55 | public function testThrownExceptionsCarryTheStatusCodeAndTheServerMessage(): void |
55 | 56 | { |
56 | 57 | $httpClient = $this->createMock(ClientInterface::class); |
57 | | - $httpClient->method('sendRequest') |
| 58 | + $httpClient->expects($this->once()) |
| 59 | + ->method('sendRequest') |
58 | 60 | ->willReturn($this->createJsonResponseMock('{"message": "Not found."}', 404)); |
59 | 61 |
|
60 | 62 | $config = new Configuration([ |
@@ -632,4 +634,93 @@ public function testSearchSurfacesATimeoutWhenEveryAttemptReturns408(): void |
632 | 634 |
|
633 | 635 | $client->collections['books']->documents->search(['q' => '*', 'query_by' => 'title']); |
634 | 636 | } |
| 637 | + |
| 638 | + public function testThrowsTheLastErrorWhenEveryAttemptReturns5xx(): void |
| 639 | + { |
| 640 | + $expectedExceptions = [ |
| 641 | + 500 => ServerError::class, |
| 642 | + 502 => TypesenseClientError::class, |
| 643 | + 503 => ServiceUnavailable::class, |
| 644 | + ]; |
| 645 | + |
| 646 | + foreach ($expectedExceptions as $statusCode => $expectedException) { |
| 647 | + $callCount = 0; |
| 648 | + |
| 649 | + $httpClient = $this->createMock(ClientInterface::class); |
| 650 | + $httpClient->method('sendRequest') |
| 651 | + ->willReturnCallback(function () use (&$callCount, $statusCode) { |
| 652 | + $callCount++; |
| 653 | + |
| 654 | + return $this->createJsonResponseMock( |
| 655 | + '{"message": "Attempt ' . $callCount . ' failed."}', |
| 656 | + $statusCode |
| 657 | + ); |
| 658 | + }); |
| 659 | + |
| 660 | + $config = new Configuration([ |
| 661 | + 'api_key' => 'test-key', |
| 662 | + 'nodes' => [ |
| 663 | + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'], |
| 664 | + ['host' => 'node2', 'port' => 8108, 'protocol' => 'http'] |
| 665 | + ], |
| 666 | + 'num_retries' => 2, |
| 667 | + 'retry_interval_seconds' => 0.001, |
| 668 | + 'client' => $httpClient |
| 669 | + ]); |
| 670 | + |
| 671 | + $apiCall = new ApiCall($config); |
| 672 | + |
| 673 | + try { |
| 674 | + $apiCall->get('/test', []); |
| 675 | + $this->fail("Expected a {$expectedException} for HTTP {$statusCode}."); |
| 676 | + } catch (TypesenseClientError $exception) { |
| 677 | + $this->assertSame($expectedException, get_class($exception)); |
| 678 | + $this->assertSame($statusCode, $exception->getCode()); |
| 679 | + $this->assertSame('Attempt 3 failed.', $exception->getMessage()); |
| 680 | + } |
| 681 | + |
| 682 | + $this->assertSame(3, $callCount); |
| 683 | + } |
| 684 | + } |
| 685 | + |
| 686 | + public function test5xxResponseMarksTheNodeUnhealthy(): void |
| 687 | + { |
| 688 | + $requestedHosts = []; |
| 689 | + |
| 690 | + $httpClient = $this->createMock(ClientInterface::class); |
| 691 | + $httpClient->method('sendRequest') |
| 692 | + ->willReturnCallback(function (RequestInterface $request) use (&$requestedHosts) { |
| 693 | + $requestedHosts[] = $request->getUri()->getHost(); |
| 694 | + |
| 695 | + if ($request->getUri()->getHost() === 'node2') { |
| 696 | + return $this->createJsonResponseMock('{"message": "Not Ready or Lagging"}', 503); |
| 697 | + } |
| 698 | + |
| 699 | + return $this->createJsonResponseMock('{"success": true}'); |
| 700 | + }); |
| 701 | + |
| 702 | + $config = new Configuration([ |
| 703 | + 'api_key' => 'test-key', |
| 704 | + 'nodes' => [ |
| 705 | + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'], |
| 706 | + ['host' => 'node2', 'port' => 8108, 'protocol' => 'http'] |
| 707 | + ], |
| 708 | + 'num_retries' => 2, |
| 709 | + 'retry_interval_seconds' => 0.001, |
| 710 | + 'randomize_nodes' => false, |
| 711 | + 'client' => $httpClient |
| 712 | + ]); |
| 713 | + |
| 714 | + $apiCall = new ApiCall($config); |
| 715 | + |
| 716 | + // Nodes are picked round-robin, so the first request starts on node2 |
| 717 | + $this->assertEquals(['success' => true], $apiCall->get('/test', [])); |
| 718 | + $this->assertTrue($config->getNodes()[0]->isHealthy()); |
| 719 | + $this->assertFalse($config->getNodes()[1]->isHealthy()); |
| 720 | + |
| 721 | + // node2 is next in line, but is skipped until its health check is due |
| 722 | + $this->assertEquals(['success' => true], $apiCall->get('/test', [])); |
| 723 | + |
| 724 | + $this->assertSame(['node2', 'node1', 'node1'], $requestedHosts); |
| 725 | + } |
635 | 726 | } |
0 commit comments