Files
threshold/internal/scheduler/testing.go
T
Jim 591ec476e7 refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785)
* chore: Add new dbw dependency

* refactor: Convert to use dbw.DB

* refactor (db): Change GetTicket(...) signature

* feature (db tests): Added TestDeleteWhere(...)
Adding this allows us to eliminate a leak of the gorm library

* refactor: Add ctx to ScanRows(...) signature
This is required to refactor all the deprecated errors pkg calls.
This refactor required adding a context to quite a few call chains
and in particular the job scheduler bits

* feature (db/tests): Add support for test opt: WithTestLogLevel(...)
This test only option allows callers to specify a log level for the
underlying db package (where supported/applicable) when calling
TestSetup(...)

* refactor (db): Simplify Db type
Only store the underlying *DB and create the *RW on demand when
needed.

* refactor (oplog): Port to using dbw pkg for db operations

* refactor (db): Remove gormDB(...) func

* tests (db): Add unit tests for TestDeleteWhere(...)

* chore: make fmt changes to formatting
2022-01-05 14:38:38 -05:00

107 lines
2.6 KiB
Go

package scheduler
import (
"context"
"sync"
"testing"
"time"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/scheduler/job"
"github.com/hashicorp/boundary/internal/servers"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
)
// 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.
func TestScheduler(t *testing.T, conn *db.DB, wrapper wrapping.Wrapper, opt ...Option) *Scheduler {
t.Helper()
rw := db.New(conn)
kmsCache := kms.TestKms(t, conn, wrapper)
serversRepo, err := servers.NewRepository(rw, rw, kmsCache)
require.NoError(t, err)
iam.TestRepo(t, conn, wrapper)
id, err := uuid.GenerateUUID()
require.NoError(t, err)
controller := &servers.Server{
PrivateId: "test-job-server-" + id,
Type: servers.ServerTypeController.String(),
Description: "Test Job Controller",
Address: "127.0.0.1",
}
_, _, err = serversRepo.UpsertServer(context.Background(), controller)
require.NoError(t, err)
jobRepoFn := func() (*job.Repository, error) {
return job.NewRepository(rw, rw, kmsCache)
}
s, err := New(controller.PrivateId, jobRepoFn, opt...)
require.NoError(t, err)
return s
}
func testJobFn() (func(ctx context.Context) error, chan struct{}, chan struct{}) {
jobReady := make(chan struct{})
jobDone := make(chan struct{})
fn := func(ctx context.Context) error {
jobReady <- struct{}{}
// Block until context is canceled
<-ctx.Done()
jobDone <- struct{}{}
return nil
}
return fn, jobReady, jobDone
}
type testJob struct {
nextRunIn time.Duration
name, description string
fn func(context.Context) error
statusFn func() JobStatus
}
func (j testJob) Status() JobStatus {
if j.statusFn == nil {
return JobStatus{}
}
return j.statusFn()
}
func (j testJob) Run(ctx context.Context) error {
return j.fn(ctx)
}
func (j testJob) NextRunIn(_ context.Context) (time.Duration, error) {
return j.nextRunIn, nil
}
func (j testJob) Name() string {
return j.name
}
func (j testJob) Description() string {
return j.description
}
func mapLen(sm *sync.Map) int {
count := 0
sm.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}