package forgekey import ( "context" "testing" "time" ) func lowered(t *testing.T) { was := CallTimeout CallTimeout = 50 * time.Millisecond t.Cleanup(func() { CallTimeout = was }) } // 1497/AC1: ExecRunnerFor(0), no deadline: a command longer than the lowered CallTimeout is not cut. func TestExecRunnerForZeroIsNotCutByCallTimeout(t *testing.T) { lowered(t) r := ExecRunnerFor(0)(context.Background(), nil, "sleep", "0.2") if r.Status != 0 || r.Err != "" { t.Errorf("ran = %+v, want status 0", r) } } // 1497/AC2: ExecRunner is unchanged: cut at CallTimeout with status 124. func TestExecRunnerIsStillCutAtCallTimeout(t *testing.T) { lowered(t) r := ExecRunner(context.Background(), nil, "sleep", "0.2") if r.Status != 124 { t.Errorf("ran = %+v, want status 124", r) } } // 1497/AC3: ExecRunnerFor(0) is cut when its context is cancelled. func TestExecRunnerForZeroIsCutByItsContext(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() began := time.Now() r := ExecRunnerFor(0)(ctx, nil, "sleep", "2") if r.Status == 0 || time.Since(began) > time.Second { t.Errorf("ran = %+v after %v, want it cut", r, time.Since(began)) } }