1717
1818namespace Google \Cloud \Samples \ErrorReporting ;
1919
20- use Google \Cloud \ TestUtils \ TestTrait ;
21- use Google \Cloud \TestUtils \ EventuallyConsistentTestTrait ;
20+ use Google \ApiCore \ ApiException ;
21+ use Google \Cloud \Core \ ExponentialBackoff ;
2222use Google \Cloud \ErrorReporting \V1beta1 \ErrorStatsServiceClient ;
2323use Google \Cloud \ErrorReporting \V1beta1 \QueryTimeRange ;
2424use Google \Cloud \ErrorReporting \V1beta1 \QueryTimeRange \Period ;
25+ use Google \Cloud \TestUtils \TestTrait ;
26+ use Google \Rpc \Code ;
27+ use PHPUnit \Framework \ExpectationFailedException ;
2528
2629trait VerifyReportedErrorTrait
2730{
28- use EventuallyConsistentTestTrait;
2931 use TestTrait;
3032
3133 private function verifyReportedError ($ projectId , $ message )
3234 {
33- $ retryCount = 7 ;
35+ $ retries = 20 ; // Retry for 20 minutes
36+ $ backoff = new ExponentialBackoff ($ retries , function ($ exception ) {
37+ // retry if the exception is resource exhausted from Google APIs
38+ if ($ exception instanceof ApiException
39+ && $ exception ->getCode () == Code::RESOURCE_EXHAUSTED ) {
40+ return true ;
41+ }
42+
43+ // retry if the exxception is PHPUnit failed assertion
44+ if ($ exception instanceof ExpectationFailedException
45+ || $ exception instanceof \PHPUnit_Framework_ExpectationFailedException) {
46+ return true ;
47+ }
48+ });
49+
3450 $ errorStats = new ErrorStatsServiceClient ();
3551 $ projectName = $ errorStats ->projectName ($ projectId );
3652
3753 $ timeRange = (new QueryTimeRange ())
3854 ->setPeriod (Period::PERIOD_1_HOUR );
3955
4056 // Iterate through all elements
41- $ this ->runEventuallyConsistentTest (function () use (
42- $ errorStats ,
43- $ projectName ,
44- $ timeRange ,
45- $ message
46- ) {
57+ $ testFunc = function () use ($ errorStats , $ projectName , $ timeRange , $ message ) {
4758 $ messages = [];
4859 $ response = $ errorStats ->listGroupStats ($ projectName , $ timeRange , [
4960 'pageSize ' => 100 ,
@@ -59,6 +70,8 @@ private function verifyReportedError($projectId, $message)
5970 }
6071
6172 $ this ->assertContains ($ message , implode ("\n" , $ messages ));
62- }, $ retryCount , true );
73+ };
74+
75+ $ backoff ->execute ($ testFunc );
6376 }
6477}
0 commit comments