Files
team/server/channels/jobs/batch_worker_test.go
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

170 lines
4.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package jobs_test
import (
"strconv"
"testing"
"time"
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/shared/request"
"github.com/hanzoteam/server/server/v8/channels/jobs"
"github.com/stretchr/testify/require"
)
// TestBatchWorkerRace tests race conditions during the start/stop
// cases of the batch worker. Use the -race flag while testing this.
func TestBatchWorkerRace(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t)
worker := jobs.MakeBatchWorker(th.Server.Jobs, th.Server.Store(), 1*time.Second, func(rctx request.CTX, job *model.Job) bool {
return false
})
go worker.Run()
worker.Stop()
}
func TestBatchWorker(t *testing.T) {
mainHelper.Parallel(t)
createBatchWorker := func(t *testing.T, th *TestHelper, doBatch func(rctx request.CTX, job *model.Job) bool) (*jobs.BatchWorker, *model.Job) {
t.Helper()
worker := jobs.MakeBatchWorker(th.Server.Jobs, th.Server.Store(), 1*time.Second, doBatch)
job := th.SetupBatchWorker(t, worker)
return worker, job
}
getBatchNumberFromData := func(t *testing.T, data model.StringMap) int {
t.Helper()
batchNumber, err := strconv.Atoi(data["batch_number"])
require.NoError(t, err)
return batchNumber
}
incrementBatchNumber := func(t *testing.T, th *TestHelper, job *model.Job) {
t.Helper()
batchNumber, err := strconv.Atoi(job.Data["batch_number"])
require.NoError(t, err)
batchNumber++
job.Data["batch_number"] = strconv.Itoa(batchNumber)
appErr := th.Server.Jobs.SetJobProgress(job, 0)
require.Nil(t, appErr)
}
t.Run("stop after first batch", func(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
var worker *jobs.BatchWorker
worker, job := createBatchWorker(t, th, func(rctx request.CTX, job *model.Job) bool {
batchNumber := getBatchNumberFromData(t, job.Data)
require.Equal(t, 1, batchNumber, "only batch 1 should have run")
// Shut down the worker after the first batch to prevent subsequent ones.
if batchNumber >= 1 {
go worker.Stop()
} else {
incrementBatchNumber(t, th, job)
}
return false
})
// Queue the work to be done
worker.JobChannel() <- *job
th.WaitForJobStatus(t, job, model.JobStatusPending)
th.WaitForBatchNumber(t, job, 1)
})
t.Run("stop after second batch", func(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
var worker *jobs.BatchWorker
worker, job := createBatchWorker(t, th, func(rctx request.CTX, job *model.Job) bool {
batchNumber := getBatchNumberFromData(t, job.Data)
require.LessOrEqual(t, batchNumber, 2, "only batches 1 and 2 should have run")
// Shut down the worker after the second batch to prevent subsequent ones.
if batchNumber >= 2 {
go worker.Stop()
} else {
incrementBatchNumber(t, th, job)
}
return false
})
// Queue the work to be done
worker.JobChannel() <- *job
th.WaitForJobStatus(t, job, model.JobStatusPending)
th.WaitForBatchNumber(t, job, 2)
})
t.Run("done after first batch", func(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
var worker *jobs.BatchWorker
worker, job := createBatchWorker(t, th, func(rctx request.CTX, job *model.Job) bool {
batchNumber := getBatchNumberFromData(t, job.Data)
require.Equal(t, 1, batchNumber, "only batch 1 should have run")
if batchNumber >= 1 {
go worker.Stop() // Shut down the worker when the job is done
return true
}
incrementBatchNumber(t, th, job)
return false
})
// Queue the work to be done
worker.JobChannel() <- *job
th.WaitForBatchNumber(t, job, 1)
})
t.Run("done after three batches", func(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
var worker *jobs.BatchWorker
worker, job := createBatchWorker(t, th, func(rctx request.CTX, job *model.Job) bool {
batchNumber := getBatchNumberFromData(t, job.Data)
require.LessOrEqual(t, batchNumber, 3, "only 3 batches should have run")
if batchNumber >= 3 {
go worker.Stop() // Shut down the worker when the job is done
return true
}
incrementBatchNumber(t, th, job)
return false
})
// Queue the work to be done
worker.JobChannel() <- *job
th.WaitForBatchNumber(t, job, 3)
})
}