fix unit test

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

View File

@ -284,19 +284,19 @@ 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")
} }
} // Meeting or exceeding successThreshold should cause resultRun to reset to 0
msg := fmt.Sprintf("%d success", successThreshold) if w.resultRun != 0 {
expectContinue(t, w, w.doProbe(ctx), msg) t.Errorf("Prober resultRun should be 0, but %d", w.resultRun)
expectResult(t, w, results.Success, msg) }
// Meeting or exceeding successThreshold should cause resultRun to reset to 0
if w.resultRun != 0 {
t.Errorf("Prober resultRun should be 0, but %d", w.resultRun)
} }
} }
} }
@ -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,23 +319,25 @@ 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)
expectResult(t, w, results.Unknown, msg) switch i {
// resultRun should be incremented until failureThreshold is met case 0, 1:
if w.resultRun != i+1 { // At this point, the expected result is Unknown
t.Errorf("Prober resultRun should be %d, but %d", i+1, w.resultRun) // because w.resultsManager.Set() will be called after FailureThreshold is reached
} expectResult(t, w, results.Unknown, msg)
} else if i < failureThreshold { // resultRun should be incremented until failureThreshold is met
// Probe should not be on hold and will continue to be excuted if w.resultRun != i+1 {
// until failureThreshold is met t.Errorf("Prober resultRun should be %d, but %d", i+1, w.resultRun)
if w.onHold { }
t.Errorf("Prober should not be on hold") case 2:
} // The expected result is Failure
msg := fmt.Sprintf("%d failure", i+1) // because w.resultsManager.Set() will be called due to resultRun reaching failureThreshold,
expectContinue(t, w, w.doProbe(ctx), msg) // updating the cached result to Failure.
expectResult(t, w, results.Failure, msg) // After that, resultRun will be reset to 0.
// Meeting failureThreshold should cause resultRun to reset to 0 expectResult(t, w, results.Failure, msg)
if w.resultRun != 0 { // Meeting failureThreshold should cause resultRun to reset to 0
t.Errorf("Prober resultRun should be 0, but %d", w.resultRun) if w.resultRun != 0 {
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
@ -343,9 +345,6 @@ func TestStartupProbeFailureThreshold(t *testing.T) {
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)