@@ -32,49 +32,60 @@ func TestMonitoredCmd(t *testing.T) {
3232 }
3333 defer os .Remove ("./echosleep" )
3434
35- cmd := mkTestCmd (2 )
36- err = cmd .run (context .Background ())
37- if err != nil {
38- t .Errorf ("Expected command not to fail: %s" , err )
35+ tests := []struct {
36+ name string
37+ iterations int
38+ output string
39+ err bool
40+ timeout bool
41+ }{
42+ {"success" , 2 , "foo\n foo\n " , false , false },
43+ {"timeout" , 5 , "foo\n foo\n foo\n foo\n " , true , true },
3944 }
4045
41- expectedOutput := "foo\n foo\n "
42- if cmd .stdout .buf .String () != expectedOutput {
43- t .Errorf ("Unexpected output:\n \t (GOT): %s\n \t (WNT): %s" , cmd .stdout .buf .String (), expectedOutput )
44- }
46+ for _ , want := range tests {
47+ t .Run (want .name , func (t * testing.T ) {
48+ cmd := mkTestCmd (want .iterations )
4549
46- cmd2 := mkTestCmd (10 )
47- err = cmd2 .run (context .Background ())
48- if err == nil {
49- t .Error ("Expected command to fail" )
50- }
50+ err := cmd .run (context .Background ())
51+ if ! want .err && err != nil {
52+ t .Errorf ("Eexpected command not to fail, got error: %s" , err )
53+ } else if want .err && err == nil {
54+ t .Error ("expected command to fail" )
55+ }
5156
52- _ , ok := err .( * noProgressError )
53- if ! ok {
54- t .Errorf ("Expected a timeout error, but got: %s " , err )
55- }
57+ got := cmd . stdout . String ( )
58+ if want . output != got {
59+ t .Errorf ("unexpected output: \n \t (GOT): \n %s \n \t (WNT): \n %s " , got , want . output )
60+ }
5661
57- expectedOutput = "foo\n foo\n foo\n foo\n "
58- if cmd2 .stdout .buf .String () != expectedOutput {
59- t .Errorf ("Unexpected output:\n \t (GOT): %s\n \t (WNT): %s" , cmd2 .stdout .buf .String (), expectedOutput )
62+ if want .timeout {
63+ _ , ok := err .(* noProgressError )
64+ if ! ok {
65+ t .Errorf ("Expected a timeout error, but got: %s" , err )
66+ }
67+ }
68+ })
6069 }
6170
62- ctx , cancel := context .WithCancel (context .Background ())
63- sync1 , errchan := make (chan struct {}), make (chan error )
64- cmd3 := mkTestCmd (2 )
65- go func () {
66- close (sync1 )
67- errchan <- cmd3 .run (ctx )
68- }()
71+ t .Run ("cancel" , func (t * testing.T ) {
72+ ctx , cancel := context .WithCancel (context .Background ())
73+ sync , errchan := make (chan struct {}), make (chan error )
74+ cmd := mkTestCmd (2 )
75+ go func () {
76+ close (sync )
77+ errchan <- cmd .run (ctx )
78+ }()
6979
70- // Make sure goroutine is at least started before we cancel the context.
71- <- sync1
72- // Give it a bit to get the process started.
73- <- time .After (5 * time .Millisecond )
74- cancel ()
80+ // Make sure goroutine is at least started before we cancel the context.
81+ <- sync
82+ // Give it a bit to get the process started.
83+ <- time .After (5 * time .Millisecond )
84+ cancel ()
7585
76- err = <- errchan
77- if err != context .Canceled {
78- t .Errorf ("should have gotten canceled error, got %s" , err )
79- }
86+ err := <- errchan
87+ if err != context .Canceled {
88+ t .Errorf ("expected a canceled error, got %s" , err )
89+ }
90+ })
8091}
0 commit comments