0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

fix(coverage): scheduler coverage (#893)

Signed-off-by: Catalin Hofnar <catalin.hofnar@gmail.com>
This commit is contained in:
Catalin-George Hofnar 2022-10-21 18:33:22 +03:00 committed by GitHub
parent 763287873e
commit 00e65bd32b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 6 deletions

View file

@ -6003,7 +6003,7 @@ func TestPeriodicGC(t *testing.T) {
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
time.Sleep(500 * time.Millisecond)
time.Sleep(5000 * time.Millisecond)
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)

View file

@ -112,11 +112,24 @@ func (scheduler *Scheduler) RunScheduler(ctx context.Context) {
func (scheduler *Scheduler) pushReadyGenerators() {
// iterate through waiting generators list and resubmit those which become ready to run
for i, gen := range scheduler.waitingGenerators {
if gen.getState() == ready {
gen.done = false
heap.Push(&scheduler.generators, gen)
scheduler.waitingGenerators = append(scheduler.waitingGenerators[:i], scheduler.waitingGenerators[i+1:]...)
for {
modified := false
for i, gen := range scheduler.waitingGenerators {
if gen.getState() == ready {
gen.done = false
heap.Push(&scheduler.generators, gen)
scheduler.waitingGenerators = append(scheduler.waitingGenerators[:i], scheduler.waitingGenerators[i+1:]...)
modified = true
scheduler.log.Debug().Msg("waiting generator is ready, pushing to ready generators")
break
}
}
if !modified {
break
}
}
}

View file

@ -59,7 +59,54 @@ func (g *generator) Reset() {
g.step = 0
}
type shortGenerator struct {
log log.Logger
priority string
done bool
index int
step int
}
func (g *shortGenerator) GenerateTask() (scheduler.Task, error) {
g.done = true
return &task{log: g.log, msg: fmt.Sprintf("executing %s task; index: %d", g.priority, g.index), err: false}, nil
}
func (g *shortGenerator) IsDone() bool {
return g.done
}
func (g *shortGenerator) Reset() {
g.done = true
g.step = 0
}
func TestScheduler(t *testing.T) {
Convey("Test active to waiting periodic generator", t, func() {
logFile, err := os.CreateTemp("", "zot-log*.txt")
So(err, ShouldBeNil)
defer os.Remove(logFile.Name()) // clean up
logger := log.NewLogger("debug", logFile.Name())
sch := scheduler.NewScheduler(logger)
genH := &shortGenerator{log: logger, priority: "high priority"}
// interval has to be higher than throttle value to simulate
sch.SubmitGenerator(genH, 12000*time.Millisecond, scheduler.HighPriority)
ctx, cancel := context.WithCancel(context.Background())
sch.RunScheduler(ctx)
time.Sleep(16 * time.Second)
cancel()
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)
So(string(data), ShouldContainSubstring, "waiting generator is ready, pushing to ready generators")
})
Convey("Test order of generators in queue", t, func() {
logFile, err := os.CreateTemp("", "zot-log*.txt")
So(err, ShouldBeNil)