In #2616330: statistics.php can exit early and prevent output errors we added some test to call statistics.php.
@dawehner pointed out that we could simplify the guzzle declaration.
Specifically

-    $this->client = \Drupal::service('http_client_factory')
-      ->fromOptions(['config/curl' => [CURLOPT_TIMEOUT => 10]]);
+    $this->client = new Client(['timeout' => 10]);

And add a use statement for the guzzle client. use GuzzleHttp\Client;

More specifically the 'old' declaration way is used in

  • core/modules/statistics/src/Tests/StatisticsAdminTest.php
  • core/modules/statistics/src/Tests/StatisticsLoggingTest.php
  • core/modules/statistics/src/Tests/StatisticsReportsTest.php
  • core/modules/statistics/src/Tests/StatisticsTokenReplaceTest.php

Comments

mallezie created an issue. See original summary.

dawehner’s picture

As asked on the other issue: Why do we actually have a custom timeout in the first place? Maybe some git blame will give us some idea about it.

chanderbhushan’s picture

Hi,

I have updated listed files:-
core/modules/statistics/src/Tests/StatisticsAdminTest.php
core/modules/statistics/src/Tests/StatisticsLoggingTest.php

Not find these line in two rest files.

chanderbhushan’s picture

StatusFileSize
new8.58 KB
chanderbhushan’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: cleanup-guzzle-2785997-3.patch, failed testing.

chanderbhushan’s picture

Status: Needs work » Needs review
StatusFileSize
new8.58 KB

adding new patch

chanderbhushan’s picture

Status: Needs review » Needs work
chanderbhushan’s picture

Status: Needs work » Needs review
mallezie’s picture

Status: Needs review » Needs work

Thanks here.
However after some testing in #2616330: statistics.php can exit early and prevent output errors
We discovered the declaration directly to guzzle is not gonna work, we need the 'Drupal' guzzle clietn.
So the declaration of the client would need to be.

$this->client = \Drupal::httpClient();

Your patch also contains some unrelated changes.

The last submitted patch, 7: cleanup-guzzle-2785997-7.patch, failed testing.

chanderbhushan’s picture

StatusFileSize
new8.6 KB

added new patch

chanderbhushan’s picture

Status: Needs work » Needs review
mallezie’s picture

Status: Needs review » Needs work

Thanks! I think this one should pass.
But the patch still contains unrelated changes from files changed in views.
Actually only the first two hunks of the patch should be here.

chanderbhushan’s picture

StatusFileSize
new1.73 KB

HI,
@mallezie removed unrelated changes from files. Added new patch

thanks

chanderbhushan’s picture

Status: Needs work » Needs review
mallezie’s picture

Status: Needs review » Needs work

Looks great, only one minor point.
There was suggested to remove the timeout. The default setup gives a timeout of 30, there is no reason to set a specific one here.

So we could simply replace
$this->client = \Drupal::httpClient(['timeout' => 10]);
with
$this->client = \Drupal::httpClient();

chanderbhushan’s picture

StatusFileSize
new1.7 KB

thanks! @mallezie removed and added new patch.

chanderbhushan’s picture

Status: Needs work » Needs review
mallezie’s picture

Status: Needs review » Needs work

I'm sorry, but i missed that since the approach changed during this issue.
Actually the use Guzzle statements are not needed anymore, they actually should be removed. (just saw it need to do the same in #2616330: statistics.php can exit early and prevent output errors).

mr.baileys’s picture

Issue tags: +Novice

Agreed on the removal of the timeout value. It was added in #1209532-109: Count node views via AJAX in the statistics module, but that issue did not contain any reasoning why that value was chosen, so falling back to the default timeout value seems fine.

So the only change left is to remove the Guzzle-use-statements. Tagging as novice.

faline’s picture

Status: Needs work » Needs review
StatusFileSize
new1.38 KB
new851 bytes

Removing use Guzzle statements from patch #18

dawehner’s picture

So wait, why are we not fixing all 4 instances which exists according to the issue summary, but rather just 2 of them?

faline’s picture

@dawehner I think is because comment #3

dawehner’s picture

@faline
Well I checked earlier and they seemed to be there. Do you mind checking again?

faline’s picture

Assigned: Unassigned » faline

@dawehner Sure! I take a look and you are right
in both there are this code (probably this occurs because they are looking for $this->client and in these files there is only $client).

I will update the patch with this two files.

faline’s picture

@dawehner I founded one more in /core/modules/statistics/src/Tests/Views/IntegrationTest.php.
Should I change that too?

faline’s picture

Status: Needs review » Needs work
StatusFileSize
new3.03 KB
new0 bytes

This patch fixing all 4 instances according to the issue summary

faline’s picture

Assigned: faline » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.87 KB
new723 bytes

In addition to the 4 mentioned, this patch also correcting the file /core/modules/statistics/src/Tests/Views/IntegrationTest.php.

mallezie’s picture

Status: Needs review » Reviewed & tested by the community

@faline. Thanks for bringing this home. This looks great! And nice find on the 5th one.

dawehner’s picture

Thank you @faciline
Great findings!

cilefen’s picture

Title: Cleanup guzzle declaration in statistics tests » Clean up guzzle declaration in statistics tests
xjm’s picture

Title: Clean up guzzle declaration in statistics tests » Clean up guzzle declaration in core tests
Component: statistics.module » request processing system
Priority: Minor » Normal

Updating the scope based on the inclusion of the Views test. Nice work finding the additional usage!

  • xjm committed 11c92ce on 8.3.x
    Issue #2785997 by chanderbhushan, faline, mallezie, dawehner, mr.baileys...

  • xjm committed 4faed7f on 8.2.x
    Issue #2785997 by chanderbhushan, faline, mallezie, dawehner, mr.baileys...
xjm’s picture

Version: 8.3.x-dev » 8.2.x-dev
Status: Reviewed & tested by the community » Fixed

Really great work on this issue, everyone. It's definitely helpful to see that we checked why this was introduced in the first place and also looked for other similar issues in core. Those are two of the things I check almost every time I review an RTBC patch!

I agree with removing the unneeded overrride for the timeout and letting it fall back to the default; 10 vs. 30 is not worth hardcoding a magic number.

There are two other places in core that the http_client_factory service is used. One is in locale.batch.inc:

    $response = \Drupal::service('http_client_factory')->fromOptions(['allow_redirects' => [
      'on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $request_uri) use (&$actual_uri) {
        $actual_uri = (string) $request_uri;
      }
    ]])->head($uri);

And then in Aggregator, the service is actually injected in core/modules/aggregator/src/Plugin/aggregator/fetcher/DefaultFetcher.php:

      $response = $this->httpClientFactory->fromOptions(['allow_redirects' => [
        'on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $uri) use (&$actual_uri) {
          $actual_uri = (string) $uri;
        }
      ]])->send($request);

Those are not the same usecase, so I agree that the scope for this issue makes sense as is.

Committed and pushed to both 8.3.x and 8.2.x. (Since it is only changing tests and there is no disruption, this makes sense to backport to keep the branches more in sync). Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.