Files
PrivateCaptcha/pkg/api/jobs_test.go
T

51 lines
1.1 KiB
Go

package api
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/PrivateCaptcha/PrivateCaptcha/pkg/common"
"github.com/PrivateCaptcha/PrivateCaptcha/pkg/maintenance"
)
type TestJob struct {
count int32
}
func (j *TestJob) RunOnce(ctx context.Context, params any) error {
atomic.AddInt32(&j.count, 1)
return nil
}
func (j *TestJob) Interval() time.Duration { return 200 * time.Millisecond }
func (j *TestJob) Jitter() time.Duration { return 1 }
func (j *TestJob) Name() string { return "test_job" }
func (j *TestJob) NewParams() any { return struct{}{} }
func TestUniqueJob(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
t.Parallel()
job := &TestJob{}
uniqueJob := &maintenance.UniquePeriodicJob{
Job: job,
Store: store,
LockDuration: 1 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
if err := common.RunPeriodicJobOnce(ctx, uniqueJob, uniqueJob.NewParams()); err != nil {
t.Fatal(err)
}
cancel()
if job.count == 0 || job.count > 3 {
t.Fatalf("Unexpected count of job executions: %v", job.count)
}
}