2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-05-06 14:54:10 -07:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-18 12:30:38 -05:00
|
|
|
"fmt"
|
2021-05-06 14:54:10 -07:00
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-10-12 10:33:41 -07:00
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/iam"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/kms"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/scheduler/job"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/server"
|
2022-03-16 17:04:07 -04:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2021-05-06 14:54:10 -07:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2021-06-11 11:36:14 -07:00
|
|
|
// TestScheduler creates a mock controller and a new Scheduler attached to that controller id.
|
|
|
|
|
// The Scheduler returned should only be used for tests. The mock controller is not run.
|
|
|
|
|
//
|
|
|
|
|
// WithRunJobsLimit, WithRunJobsInterval, WithMonitorInterval and WithInterruptThreshold are
|
|
|
|
|
// the only valid options.
|
2022-04-25 15:52:44 -04:00
|
|
|
func TestScheduler(t testing.TB, conn *db.DB, wrapper wrapping.Wrapper, opt ...Option) *Scheduler {
|
2021-05-06 14:54:10 -07:00
|
|
|
t.Helper()
|
|
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
ctx := context.Background()
|
2021-05-06 14:54:10 -07:00
|
|
|
rw := db.New(conn)
|
|
|
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
2023-06-06 17:09:08 -04:00
|
|
|
serversRepo, err := server.NewRepository(ctx, rw, rw, kmsCache)
|
2021-05-06 14:54:10 -07:00
|
|
|
require.NoError(t, err)
|
2021-06-11 11:36:14 -07:00
|
|
|
iam.TestRepo(t, conn, wrapper)
|
2021-05-06 14:54:10 -07:00
|
|
|
|
|
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
|
require.NoError(t, err)
|
2025-03-18 12:30:38 -05:00
|
|
|
|
|
|
|
|
controller := server.NewController(fmt.Sprintf("test-server-job%s", id), server.WithAddress("127.0.0.1"))
|
2023-06-06 17:09:08 -04:00
|
|
|
_, err = serversRepo.UpsertController(ctx, controller)
|
2021-05-06 14:54:10 -07:00
|
|
|
require.NoError(t, err)
|
2021-06-11 11:36:14 -07:00
|
|
|
|
|
|
|
|
jobRepoFn := func() (*job.Repository, error) {
|
2023-06-06 17:09:08 -04:00
|
|
|
return job.NewRepository(ctx, rw, rw, kmsCache)
|
2021-06-11 11:36:14 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
s, err := New(ctx, controller.PrivateId, jobRepoFn, opt...)
|
2021-06-11 11:36:14 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
return s
|
2021-05-06 14:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 12:14:29 -04:00
|
|
|
func testJobFn() (func(ctx context.Context, _ time.Duration) error, chan struct{}, chan struct{}) {
|
2021-05-06 14:54:10 -07:00
|
|
|
jobReady := make(chan struct{})
|
|
|
|
|
jobDone := make(chan struct{})
|
2024-09-20 12:14:29 -04:00
|
|
|
fn := func(ctx context.Context, _ time.Duration) error {
|
2021-05-06 14:54:10 -07:00
|
|
|
jobReady <- struct{}{}
|
|
|
|
|
|
2021-05-14 16:26:02 -04:00
|
|
|
// Block until context is canceled
|
2021-05-06 14:54:10 -07:00
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
|
|
jobDone <- struct{}{}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return fn, jobReady, jobDone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type testJob struct {
|
|
|
|
|
nextRunIn time.Duration
|
|
|
|
|
name, description string
|
2024-09-20 12:14:29 -04:00
|
|
|
fn func(context.Context, time.Duration) error
|
2021-05-06 14:54:10 -07:00
|
|
|
statusFn func() JobStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j testJob) Status() JobStatus {
|
|
|
|
|
if j.statusFn == nil {
|
|
|
|
|
return JobStatus{}
|
|
|
|
|
}
|
|
|
|
|
return j.statusFn()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 12:14:29 -04:00
|
|
|
func (j testJob) Run(ctx context.Context, statusThreshold time.Duration) error {
|
|
|
|
|
return j.fn(ctx, statusThreshold)
|
2021-05-06 14:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 14:38:38 -05:00
|
|
|
func (j testJob) NextRunIn(_ context.Context) (time.Duration, error) {
|
2021-06-11 11:36:14 -07:00
|
|
|
return j.nextRunIn, nil
|
2021-05-06 14:54:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j testJob) Name() string {
|
|
|
|
|
return j.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j testJob) Description() string {
|
|
|
|
|
return j.description
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapLen(sm *sync.Map) int {
|
|
|
|
|
count := 0
|
2022-10-25 18:29:38 -05:00
|
|
|
sm.Range(func(key, value any) bool {
|
2021-05-06 14:54:10 -07:00
|
|
|
count++
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return count
|
|
|
|
|
}
|