Problem/Motivation

https://www.drupal.org/pift-ci-job/866534
https://www.drupal.org/pift-ci-job/991286

1) Drupal\Tests\comment\Functional\CommentStatisticsTest::testCommentNodeCommentStatistics
The value of node last_comment_name is the name of the anonymous user.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'hFpV0qnB'
+''

/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Equality/IsEqual.php:96
/var/www/html/core/modules/comment/tests/src/Functional/CommentStatisticsTest.php:121
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728

https://www.drupal.org/pift-ci-job/2692252

Steps to reproduce

The fact that this one didn't occur for a long time immediately points to a timing issue.
We went to faster test instances (and it dissappeared) and after that we're on slower instances now.

I dabbled with this one for quite a while and can't really pin down _where_ the timing issue occurs.
Somewhere else, I dumped the node comment list when the test fails.

The relevant bit:

  #list: array:1 [
    0 => Drupal\comment\Plugin\Field\FieldType\CommentItem {#4072
      #definition: Drupal\Core\Field\TypedData\FieldItemDataDefinition {#4911}
      #name: 0
      #parent: Drupal\comment\CommentFieldItemList {#7857}
      #_serviceIds: []
      #_entityStorages: []
      #stringTranslation: null
      #typedDataManager: Drupal\Core\TypedData\TypedDataManager {#5039}
      #values: array:6 [
        "status" => "2"
        "cid" => "1"
        "last_comment_timestamp" => "1687262036"
        "last_comment_name" => ""
        "last_comment_uid" => "4"
        "comment_count" => "2"
      ]
      #properties: []
    }
  ]

So somehow when it goes wrong, the last comment is tributed to uid 4, which is $this->webUser2 in the test.

Asserting that that user, which logs in to make the first comment and then logs out, is not still logged in always passes, so the timing issue lays elsewhere.

Deleting caches also didn't help.

Proposed resolution

We're testing a rare occurence where a user logs in, makes a comment, logs out, comment settings are changed, logged out user makes a comment, comment settings are changed again and the same anon-user comments again.

These setting changes are done directly using user_role_change_permissions.

I found that actually logging in, and change the permissions using "/admin/people/permissions" doesn't fail randomly.

If this is because of the extra delay that this causes or something else is a mystery to me.

Remaining tasks

Looking at this test, there's nothing in it that actually uses asserts against the actual forms/screens/whatever.
It solely tests (posted) comments and the node they belong to.

Therefore, IMHO this should be a Kernel test.

I would like some confirmation on that, and if it should be, open a follow-up for that.

Comments

tacituseu created an issue. See original summary.

tacituseu’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

krzysztof domański’s picture

StatusFileSize
new38 bytes

Wrong patch. Ignore.

krzysztof domański’s picture

StatusFileSize
new2.23 KB
krzysztof domański’s picture

Status: Active » Closed (cannot reproduce)

IMO out of date.

xjm’s picture

spokje’s picture

Assigned: Unassigned » spokje
Issue summary: View changes
Status: Closed (cannot reproduce) » Active
spokje’s picture

Title: Random failure in CommentStatisticsTest » [random test failure] Random failure in CommentStatisticsTest
spokje’s picture

StatusFileSize
new2 KB

Let's first proof that running \Drupal\Tests\comment\Functional\CommentStatisticsTest and \Drupal\Tests\comment\Functional\CommentStatisticsTest alone produces the random test failure.

Here's a patch that runs only \Drupal\Tests\comment\Functional\CommentStatisticsTest 1500 times.

spokje’s picture

Version: 8.9.x-dev » 11.x-dev

Hmm, this issue _is_ old...

spokje’s picture

StatusFileSize
new2 KB
new4.53 KB

The normal routine to prove a random failure is fixed is to run the failing patch and the patch with the fix at the same time, whilst the latter has to have ~8000 - 10.000 failure free runs to prove it's credibility.

Let's do that here.

spokje’s picture

StatusFileSize
new2.53 KB

That's 6 * 5000 = 30.000 failure free runs.

Good enough for me, here's the actual patch.

spokje’s picture

StatusFileSize
new2.52 KB

Meh! Retry.

spokje’s picture

Issue summary: View changes
StatusFileSize
new126.35 KB
spokje’s picture

spokje’s picture

StatusFileSize
new2.52 KB

Hmm, editing this in 2 tabs appareantly ate the visibility of patch #16 and I don't see it in the file list to restore that visibility.
Re-upping

spokje’s picture

Assigned: spokje » Unassigned
Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

  • catch committed 7667300b on 10.1.x
    Issue #2980750 by Spokje, Krzysztof Domański, tacituseu: [random test...

  • catch committed 61edc1b1 on 11.x
    Issue #2980750 by Spokje, Krzysztof Domański, tacituseu: [random test...
catch’s picture

Version: 11.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Fixed

Would normally push back against changing admin settings in the UI (unless that's what we're testing), but given how long this has been random, let's make it not random again and any refactoring can happen on a firmer footing.

Committed/pushed to 11.x and cherry-picked to 10.1.x, thanks!

Status: Fixed » Closed (fixed)

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