Files
upstream-mattermost/server/channels/app/import_bulk_insert_test.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

120 lines
3.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
)
// TestImportBulkInsertChunking reads a pre-generated JSONL fixture through
// BulkImport that exceeds the default INSERT parameter threshold (50,000
// params — 76% of PostgreSQL's 65,535 hard limit). A single worker is used
// so that the import batches are large enough to trigger multi-chunk INSERTs.
//
// The fixture contains placeholder tokens that are replaced with unique
// names here so parallel test runs do not collide.
//
// Overflow thresholds exercised:
//
// Posts (replies): 18 cols → chunk at 2,777 rows (2,778 generated)
// Thread memberships: 6 cols → chunk at 8,333 rows (9,000 generated)
func TestImportBulkInsertChunking(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t)
// server/tests/bulk_insert_chunk_test.jsonl — 1,015-line, 535 KB file containing:
// - 1 team, 2 channels, 10 users
// - 1,000 root posts each with 9 thread followers (9,000 memberships; threshold is 8,333)
// - 1 root post with 2,778 replies (threshold is 2,777)
testsDir, _ := fileutils.FindDir("tests")
raw, err := os.ReadFile(testsDir + "/bulk_insert_chunk_test.jsonl")
require.NoError(t, err)
// Replace placeholders with unique names for this test run.
teamName := model.NewRandomTeamName()
chanThreads := NewTestId()
chanReplies := NewTestId()
usernames := make([]string, 10)
oldNew := []string{
"__TEAM__", teamName,
"__CHAN_THREADS__", chanThreads,
"__CHAN_REPLIES__", chanReplies,
}
for i := range 10 {
usernames[i] = model.NewUsername()
oldNew = append(oldNew,
fmt.Sprintf("__USER%d__", i), usernames[i],
)
}
jsonl := strings.NewReplacer(oldNew...).Replace(string(raw))
line, appErr := th.App.BulkImport(th.Context, strings.NewReader(jsonl), nil, false, 1)
require.Nil(t, appErr, "BulkImport failed at line %d", line)
require.Equal(t, 0, line)
team, gErr := th.App.GetTeamByName(teamName)
require.Nil(t, gErr)
t.Run("thread memberships were chunked across parameter limit", func(t *testing.T) {
channel, cErr := th.App.GetChannelByName(th.Context, chanThreads, team.Id, false)
require.Nil(t, cErr)
followerIDs := make([]string, 9)
for i := range 9 {
u, uErr := th.App.GetUserByUsername(usernames[i+1])
require.Nil(t, uErr)
followerIDs[i] = u.Id
}
// Spot-check thread followers on the first and last posts.
const (
baseTime = 1700000000000
numRootPosts = 1000
)
firstPosts, nErr := th.App.Srv().Store().Post().GetPostsCreatedAt(channel.Id, baseTime)
require.NoError(t, nErr)
require.Len(t, firstPosts, 1)
firstFollowers, nErr := th.App.Srv().Store().Thread().GetThreadFollowers(firstPosts[0].Id, true)
require.NoError(t, nErr)
assert.ElementsMatch(t, followerIDs, firstFollowers)
lastPosts, nErr := th.App.Srv().Store().Post().GetPostsCreatedAt(channel.Id, baseTime+numRootPosts-1)
require.NoError(t, nErr)
require.Len(t, lastPosts, 1)
lastFollowers, nErr := th.App.Srv().Store().Thread().GetThreadFollowers(lastPosts[0].Id, true)
require.NoError(t, nErr)
assert.ElementsMatch(t, followerIDs, lastFollowers)
})
t.Run("replies were chunked across parameter limit", func(t *testing.T) {
channel, cErr := th.App.GetChannelByName(th.Context, chanReplies, team.Id, false)
require.Nil(t, cErr)
const (
replyBaseTime = 1700001000000
numReplies = 2778
)
rootPosts, nErr := th.App.Srv().Store().Post().GetPostsCreatedAt(channel.Id, replyBaseTime)
require.NoError(t, nErr)
require.Len(t, rootPosts, 1)
thread, nErr := th.App.Srv().Store().Thread().Get(rootPosts[0].Id)
require.NoError(t, nErr)
require.NotNil(t, thread)
assert.Equal(t, int64(numReplies), thread.ReplyCount)
})
}