shad-go/distbuild/pkg/scheduler/scheduler_test.go

114 lines
2.7 KiB
Go
Raw Normal View History

2020-03-28 21:34:09 +00:00
package scheduler
import (
"testing"
"time"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"go.uber.org/zap/zaptest"
2020-03-29 16:03:07 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/api"
2020-03-28 21:34:09 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/build"
)
const (
2020-03-29 16:03:07 +00:00
workerID0 api.WorkerID = "w0"
2020-03-28 21:34:09 +00:00
)
func TestScheduler(t *testing.T) {
defer goleak.VerifyNone(t)
clock := clockwork.NewFakeClock()
timeAfter = clock.After
defer func() { timeAfter = time.After }()
config := Config{
CacheTimeout: time.Second,
DepsTimeout: time.Minute,
}
t.Run("SingleJob", func(t *testing.T) {
s := NewScheduler(zaptest.NewLogger(t), config)
2020-04-04 21:49:25 +00:00
job0 := &api.JobSpec{Job: build.Job{ID: build.NewID()}}
2020-03-28 21:34:09 +00:00
pendingJob0 := s.ScheduleJob(job0)
s.RegisterWorker(workerID0)
pickerJob := s.PickJob(workerID0, nil)
require.Equal(t, pendingJob0, pickerJob)
2020-03-29 16:03:07 +00:00
result := &api.JobResult{ID: job0.ID, ExitCode: 0}
2020-03-28 21:34:09 +00:00
s.OnJobComplete(workerID0, job0.ID, result)
select {
case <-pendingJob0.Finished:
require.Equal(t, pendingJob0.Result, result)
default:
t.Fatalf("job0 is not finished")
}
})
t.Run("PickJobTimeout", func(t *testing.T) {
s := NewScheduler(zaptest.NewLogger(t), config)
canceled := make(chan struct{})
close(canceled)
s.RegisterWorker(workerID0)
require.Nil(t, s.PickJob(workerID0, canceled))
})
t.Run("CacheLocalScheduling", func(t *testing.T) {
s := NewScheduler(zaptest.NewLogger(t), config)
2020-04-04 21:49:25 +00:00
job0 := &api.JobSpec{Job: build.Job{ID: build.NewID()}}
job1 := &api.JobSpec{Job: build.Job{ID: build.NewID()}}
2020-03-28 21:34:09 +00:00
s.RegisterWorker(workerID0)
2020-03-29 16:03:07 +00:00
s.OnJobComplete(workerID0, job0.ID, &api.JobResult{})
2020-03-28 21:34:09 +00:00
pendingJob1 := s.ScheduleJob(job1)
pendingJob0 := s.ScheduleJob(job0)
// job0 scheduling should be blocked on CacheTimeout
clock.BlockUntil(1)
pickedJob := s.PickJob(workerID0, nil)
require.Equal(t, pendingJob0, pickedJob)
pickedJob = s.PickJob(workerID0, nil)
require.Equal(t, pendingJob1, pickedJob)
clock.Advance(time.Hour)
})
t.Run("DependencyLocalScheduling", func(t *testing.T) {
s := NewScheduler(zaptest.NewLogger(t), config)
2020-04-04 21:49:25 +00:00
job0 := &api.JobSpec{Job: build.Job{ID: build.NewID()}}
job1 := &api.JobSpec{Job: build.Job{ID: build.NewID(), Deps: []build.ID{job0.ID}}}
job2 := &api.JobSpec{Job: build.Job{ID: build.NewID()}}
2020-03-28 21:34:09 +00:00
s.RegisterWorker(workerID0)
2020-03-29 16:03:07 +00:00
s.OnJobComplete(workerID0, job0.ID, &api.JobResult{})
2020-03-28 21:34:09 +00:00
pendingJob2 := s.ScheduleJob(job2)
pendingJob1 := s.ScheduleJob(job1)
// job1 should be blocked on DepsTimeout
clock.BlockUntil(1)
pickedJob := s.PickJob(workerID0, nil)
require.Equal(t, pendingJob1, pickedJob)
pickedJob = s.PickJob(workerID0, nil)
require.Equal(t, pendingJob2, pickedJob)
clock.Advance(time.Hour)
})
}