Skip to content

Commit

Permalink
Vortex: Set timeout options to jpush batch dispacher
Browse files Browse the repository at this point in the history
Issue HKGO-2082

Reviewed at https://reviews.lunr.nl/r/1068/
  • Loading branch information
David Mendes committed Nov 9, 2023
1 parent 309c79d commit f7200a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/Lunr/Vortex/JPush/JPushDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public function __construct(Requests_Session $http, LoggerInterface $logger)
$this->http = $http;
$this->logger = $logger;
$this->auth_token = NULL;

$this->http->options = [
'timeout' => 15, // timeout in seconds
'connect_timeout' => 15 // timeout in seconds
];
}

/**
Expand Down Expand Up @@ -148,10 +143,14 @@ protected function push_batch(JPushPayload $payload, array &$endpoints): JPushBa
$tmp_payload['audience']['registration_id'] = $endpoints;

$json_payload = json_encode($tmp_payload, JSON_UNESCAPED_UNICODE);
$options = [
'timeout' => 15, // timeout in seconds
'connect_timeout' => 15 // timeout in seconds
];

try
{
$http_response = $this->http->post(static::JPUSH_SEND_URL, [], $json_payload, []);
$http_response = $this->http->post(static::JPUSH_SEND_URL, [], $json_payload, $options);

Check failure on line 153 in src/Lunr/Vortex/JPush/JPushDispatcher.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unsafe access to private constant Lunr\Vortex\JPush\JPushDispatcher::JPUSH_SEND_URL through static::.

Check failure on line 153 in src/Lunr/Vortex/JPush/JPushDispatcher.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unsafe access to private constant Lunr\Vortex\JPush\JPushDispatcher::JPUSH_SEND_URL through static::.
}
catch (Requests_Exception $e)
{
Expand Down
3 changes: 0 additions & 3 deletions src/Lunr/Vortex/JPush/Tests/JPushDispatcherBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class JPushDispatcherBaseTest extends JPushDispatcherTest
public function testRequestsSessionIsSetCorrectly(): void
{
$this->assertPropertySame('http', $this->http);

$this->assertSame(15, $this->http->options['timeout']);
$this->assertSame(15, $this->http->options['connect_timeout']);
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ public function testPushWithFailedRequest(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->throwException(new Requests_Exception('cURL error 10: Request error', 'curlerror', NULL)));

$message = 'Dispatching JPush notification(s) failed: {message}';
Expand Down Expand Up @@ -150,14 +151,15 @@ public function testPushWithTimeoutRequest()
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->throwException(new Requests_Exception('cURL error 28: Request timed out', 'curlerror', NULL)));

$message = 'Dispatching JPush notification(s) failed: {message}';
Expand Down Expand Up @@ -187,6 +189,7 @@ public function testPushWithFailureResponse()
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -198,7 +201,7 @@ public function testPushWithFailureResponse()

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$message = 'Dispatching JPush notification failed: {error}';
Expand Down Expand Up @@ -226,6 +229,7 @@ public function testPushRequestWithDefaultValues(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -235,7 +239,7 @@ public function testPushRequestWithDefaultValues(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand All @@ -252,6 +256,7 @@ public function testPushRequestWithSingleEndpoint(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -263,7 +268,7 @@ public function testPushRequestWithSingleEndpoint(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand All @@ -280,6 +285,7 @@ public function testPushRequestWithMultibyteCharacters(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"collapse_key":"abcde-12345","alert":"hello","message":{"msg_content":"凄い"},"audience":{"registration_id":["endpoint"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello', 'message' => [ 'msg_content' => '凄い' ] ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -291,7 +297,7 @@ public function testPushRequestWithMultibyteCharacters(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->willReturn($response);

$this->class->push($this->payload, $endpoints);
Expand Down Expand Up @@ -329,7 +335,7 @@ public function testPushRequestWithMultipleEndpointsOneBatch(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand Down Expand Up @@ -357,14 +363,15 @@ public function testPushRequestWithMultipleEndpointsMultipleBatches(): void
$post2 = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint3","endpoint4"]}}';
$post3 = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint5"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(3))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->exactly(3))
->method('post')
->withConsecutive([ $url, [], $post1, []], [ $url, [], $post2, []], [ $url, [], $post3, []])
->withConsecutive([ $url, [], $post1, $options ], [ $url, [], $post2, $options ], [ $url, [], $post3, $options ])
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand Down

0 comments on commit f7200a8

Please sign in to comment.