fix unit test

This commit is contained in:
mochizuki875 2024-08-26 08:39:22 +00:00
parent a262c806d2
commit 632f162d71

View File

@ -284,22 +284,22 @@ func TestStartupProbeSuccessThreshold(t *testing.T) {
if w.onHold { if w.onHold {
t.Errorf("Prober should not be on hold") t.Errorf("Prober should not be on hold")
} }
msg := fmt.Sprintf("%d success", i+1)
expectContinue(t, w, w.doProbe(ctx), msg)
expectResult(t, w, results.Success, msg)
} else { } else {
// Probe should be on hold and will not be executed anymore // Probe should be on hold and will not be executed anymore
// when successThreshold is met // when successThreshold is met
if !w.onHold { if !w.onHold {
t.Errorf("Prober should be on hold because successThreshold is exceeded") t.Errorf("Prober should be on hold because successThreshold is exceeded")
} }
}
msg := fmt.Sprintf("%d success", successThreshold)
expectContinue(t, w, w.doProbe(ctx), msg)
expectResult(t, w, results.Success, msg)
// Meeting or exceeding successThreshold should cause resultRun to reset to 0 // Meeting or exceeding successThreshold should cause resultRun to reset to 0
if w.resultRun != 0 { if w.resultRun != 0 {
t.Errorf("Prober resultRun should be 0, but %d", w.resultRun) t.Errorf("Prober resultRun should be 0, but %d", w.resultRun)
} }
} }
} }
}
func TestStartupProbeFailureThreshold(t *testing.T) { func TestStartupProbeFailureThreshold(t *testing.T) {
ctx := context.Background() ctx := context.Background()
@ -311,7 +311,7 @@ func TestStartupProbeFailureThreshold(t *testing.T) {
m.prober.exec = fakeExecProber{probe.Failure, nil} m.prober.exec = fakeExecProber{probe.Failure, nil}
for i := 0; i < failureThreshold+1; i++ { for i := 0; i < failureThreshold+1; i++ {
if i < failureThreshold-1 { if i < failureThreshold {
// Probe should not be on hold and will continue to be excuted // Probe should not be on hold and will continue to be excuted
// until failureThreshold is met // until failureThreshold is met
if w.onHold { if w.onHold {
@ -319,33 +319,32 @@ func TestStartupProbeFailureThreshold(t *testing.T) {
} }
msg := fmt.Sprintf("%d failure", i+1) msg := fmt.Sprintf("%d failure", i+1)
expectContinue(t, w, w.doProbe(ctx), msg) expectContinue(t, w, w.doProbe(ctx), msg)
switch i {
case 0, 1:
// At this point, the expected result is Unknown
// because w.resultsManager.Set() will be called after FailureThreshold is reached
expectResult(t, w, results.Unknown, msg) expectResult(t, w, results.Unknown, msg)
// resultRun should be incremented until failureThreshold is met // resultRun should be incremented until failureThreshold is met
if w.resultRun != i+1 { if w.resultRun != i+1 {
t.Errorf("Prober resultRun should be %d, but %d", i+1, w.resultRun) t.Errorf("Prober resultRun should be %d, but %d", i+1, w.resultRun)
} }
} else if i < failureThreshold { case 2:
// Probe should not be on hold and will continue to be excuted // The expected result is Failure
// until failureThreshold is met // because w.resultsManager.Set() will be called due to resultRun reaching failureThreshold,
if w.onHold { // updating the cached result to Failure.
t.Errorf("Prober should not be on hold") // After that, resultRun will be reset to 0.
}
msg := fmt.Sprintf("%d failure", i+1)
expectContinue(t, w, w.doProbe(ctx), msg)
expectResult(t, w, results.Failure, msg) expectResult(t, w, results.Failure, msg)
// Meeting failureThreshold should cause resultRun to reset to 0 // Meeting failureThreshold should cause resultRun to reset to 0
if w.resultRun != 0 { if w.resultRun != 0 {
t.Errorf("Prober resultRun should be 0, but %d", w.resultRun) t.Errorf("Prober resultRun should be 0, but %d", w.resultRun)
} }
}
} else { } else {
// Probe should be on hold and will not be executed anymore // Probe should be on hold and will not be executed anymore
// when failureThreshold is met // when failureThreshold is met
if !w.onHold { if !w.onHold {
t.Errorf("Prober should be on hold because failureThreshold is exceeded") t.Errorf("Prober should be on hold because failureThreshold is exceeded")
} }
msg := fmt.Sprintf("%d failure", failureThreshold)
expectContinue(t, w, w.doProbe(ctx), msg)
expectResult(t, w, results.Failure, msg)
// Exceeding failureThreshold should cause resultRun to reset to 0 // Exceeding failureThreshold should cause resultRun to reset to 0
if w.resultRun != 0 { if w.resultRun != 0 {
t.Errorf("Prober resultRun should be 0, but %d", w.resultRun) t.Errorf("Prober resultRun should be 0, but %d", w.resultRun)