mirror of
https://github.com/project-zot/zot.git
synced 2024-12-30 22:34:13 -05:00
fix(coverage): scheduler coverage (#893)
Signed-off-by: Catalin Hofnar <catalin.hofnar@gmail.com>
This commit is contained in:
parent
763287873e
commit
00e65bd32b
3 changed files with 66 additions and 6 deletions
|
@ -6003,7 +6003,7 @@ func TestPeriodicGC(t *testing.T) {
|
||||||
defer stopServer(ctlr)
|
defer stopServer(ctlr)
|
||||||
test.WaitTillServerReady(baseURL)
|
test.WaitTillServerReady(baseURL)
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(5000 * time.Millisecond)
|
||||||
|
|
||||||
data, err := os.ReadFile(logFile.Name())
|
data, err := os.ReadFile(logFile.Name())
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
|
@ -112,11 +112,24 @@ func (scheduler *Scheduler) RunScheduler(ctx context.Context) {
|
||||||
|
|
||||||
func (scheduler *Scheduler) pushReadyGenerators() {
|
func (scheduler *Scheduler) pushReadyGenerators() {
|
||||||
// iterate through waiting generators list and resubmit those which become ready to run
|
// iterate through waiting generators list and resubmit those which become ready to run
|
||||||
|
for {
|
||||||
|
modified := false
|
||||||
|
|
||||||
for i, gen := range scheduler.waitingGenerators {
|
for i, gen := range scheduler.waitingGenerators {
|
||||||
if gen.getState() == ready {
|
if gen.getState() == ready {
|
||||||
gen.done = false
|
gen.done = false
|
||||||
heap.Push(&scheduler.generators, gen)
|
heap.Push(&scheduler.generators, gen)
|
||||||
scheduler.waitingGenerators = append(scheduler.waitingGenerators[:i], scheduler.waitingGenerators[i+1:]...)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,54 @@ func (g *generator) Reset() {
|
||||||
g.step = 0
|
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) {
|
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() {
|
Convey("Test order of generators in queue", t, func() {
|
||||||
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
Loading…
Reference in a new issue