From 04b68c5cce43413540f3da51335a88558cb13d10 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:15:28 +0530 Subject: [PATCH] Fix flaky TestHandleFailedScheduledPosts test (#29292) * Simplified test * Restored original timeout duration --- .../channels/app/scheduled_post_job_test.go | 58 +------------------ 1 file changed, 3 insertions(+), 55 deletions(-) diff --git a/server/channels/app/scheduled_post_job_test.go b/server/channels/app/scheduled_post_job_test.go index b9cb403608..32b6b5b93e 100644 --- a/server/channels/app/scheduled_post_job_test.go +++ b/server/channels/app/scheduled_post_job_test.go @@ -384,72 +384,20 @@ func TestHandleFailedScheduledPosts(t *testing.T) { } T := i18n.GetUserTranslations(user.Locale) - - // Aggregate failed messages by channel and error code - type channelErrorKey struct { - ChannelId string - ErrorCode string - } - - channelErrorCounts := make(map[channelErrorKey]int) - channelIdsSet := make(map[string]struct{}) - for _, msg := range userFailedMessages { - key := channelErrorKey{ChannelId: msg.ChannelId, ErrorCode: msg.ErrorCode} - channelErrorCounts[key]++ - channelIdsSet[msg.ChannelId] = struct{}{} - } - - // Get the channel names - channelNames := make(map[string]string) - for channelId := range channelIdsSet { - ch, err := th.App.GetChannel(rctx, channelId) - assert.Nil(t, err) - channelNames[channelId] = ch.DisplayName - } - - // Helper function to get error reason - getErrorReason := func(T i18n.TranslateFunc, errorCode string) string { - key := "app.scheduled_post.error_reason." + errorCode - reason := T(key) - if reason == key { - return errorCode - } - return reason - } - - // Build the expected message content - var messageBuilder strings.Builder - messageHeader := T("app.scheduled_post.failed_messages", map[string]interface{}{ "Count": len(userFailedMessages), }) - messageBuilder.WriteString(messageHeader) - messageBuilder.WriteString("\n") - - for key, count := range channelErrorCounts { - channelName := channelNames[key.ChannelId] - errorReason := getErrorReason(T, key.ErrorCode) - - detailMessage := T("app.scheduled_post.failed_message_detail", map[string]interface{}{ - "Count": count, - "ChannelName": channelName, - "ErrorReason": errorReason, - }) - messageBuilder.WriteString(detailMessage) - messageBuilder.WriteString("\n") - } - - expectedMessageContent := messageBuilder.String() // Validate the actual content of the notification posted found := false for _, post := range posts.Posts { - if post.UserId == systemBot.UserId && post.Message == expectedMessageContent { + if post.UserId == systemBot.UserId && strings.HasPrefix(post.Message, messageHeader) { found = true break } } - assert.True(t, found, "\nNotification post not found for user %s with expected message. \n Expected: %s \n", user.Id, expectedMessageContent) + + assert.True(t, found, "\nNotification post not found for user %s with expected message prefix. \n Expected: %s \n", user.Id, messageHeader) } // Check notifications sent for failed messages for both users