Fix flaky TestSharedChannelPostMetadataSync (#36862)

* Fix flaky TestSharedChannelPostMetadataSync

STEP 6 incorrectly required Cluster A to receive sync traffic after a
resync trigger. When echo prevention suppresses unchanged acknowledgement
payloads, no message arrives and the Eventually timeout fails. Wait for
pending sync tasks, assert the DB still has exactly one acknowledgement,
and only validate sync payload duplicates when traffic is received.

Tests-only change. Verified with `go test -run '^TestSharedChannelPostMetadataSync$' -race -count=50` locally.

Co-authored-by: mattermost-code <matty-code@mattermost.com>

* Assert sync payload ack count outside muA lock

Copy the matching post under muA before calling require.Len so a
failed assertion cannot leave the mutex locked and hang teardown.

Co-authored-by: mattermost-code <matty-code@mattermost.com>

* Retrigger CI after Playwright infra flake

Co-authored-by: mattermost-code <matty-code@mattermost.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: mattermost-code <matty-code@mattermost.com>
This commit is contained in:
cursor[bot]
2026-06-03 12:15:45 -04:00
committed by GitHub
parent 50952dec3f
commit fb8cfbaef7
@@ -683,17 +683,28 @@ func TestSharedChannelPostMetadataSync(t *testing.T) {
// Trigger another sync to ensure no duplicates are created
service.NotifyChannelChanged(testChannel.Id)
// Verify acknowledgement count remains 1 (no duplicates)
// Echo prevention may suppress resending unchanged acknowledgements, so wait
// for sync work to finish instead of requiring Cluster A to receive traffic.
require.Eventually(t, func() bool {
muA.Lock()
defer muA.Unlock()
for _, post := range syncedPostsServerA {
if post.Id == postIdToTrack && post.Metadata != nil && post.Metadata.Acknowledgements != nil {
return len(post.Metadata.Acknowledgements) == 1
}
return !service.HasPendingTasksForTesting()
}, 10*time.Second, 100*time.Millisecond, "Sync tasks should complete after resync trigger")
finalAcks, appErr := th.App.GetAcknowledgementsForPost(postIdToTrack)
require.Nil(t, appErr)
require.Len(t, finalAcks, 1, "Should maintain single acknowledgement after resync")
muA.Lock()
var serverAResyncPost *model.Post
for _, post := range syncedPostsServerA {
if post.Id == postIdToTrack && post.Metadata != nil && post.Metadata.Acknowledgements != nil {
serverAResyncPost = post
break
}
return len(syncedPostsServerA) > 0
}, 3*time.Second, 100*time.Millisecond, "Should maintain single acknowledgement after resync")
}
muA.Unlock()
if serverAResyncPost != nil {
require.Len(t, serverAResyncPost.Metadata.Acknowledgements, 1, "Sync payload should not contain duplicate acknowledgements")
}
t.Logf("✅ Cross-cluster acknowledgement flow completed successfully:")
t.Logf(" 1. Server A created post with ack request: %s", postIdToTrack)