@@ -2,7 +2,6 @@ package main
22
33import (
44 "context"
5- "io"
65 "log/slog"
76 "net/http"
87 "testing"
@@ -18,6 +17,13 @@ func Test_desktopMonitorParentProcess(t *testing.T) { //nolint:paralleltest
1817 runnerServer , err := runnerserver .New (multislogger .NewNopLogger (), nil , nil )
1918 require .NoError (t , err )
2019
20+ // start server
21+ go func () {
22+ if err := runnerServer .Serve (); err != nil {
23+ require .ErrorIs (t , err , http .ErrServerClosed )
24+ }
25+ }()
26+
2127 // register client and get token
2228 token := runnerServer .RegisterClient ("0" )
2329
@@ -29,40 +35,33 @@ func Test_desktopMonitorParentProcess(t *testing.T) { //nolint:paralleltest
2935 Level : slog .LevelDebug ,
3036 }))
3137
32- go func () {
33- monitorParentProcess (slogger , runnerServer .Url (), token , monitorInterval )
34- }()
35-
38+ // Start up monitoring the parent process, waiting just a moment for the server to start
3639 time .Sleep (monitorInterval * 2 )
37-
38- // should retry
39- require .Contains (t , logBytes .String (), "will retry" )
40-
41- // start server
40+ monitorShutdownChan := make (chan struct {}, 1 )
4241 go func () {
43- if err := runnerServer .Serve (); err != nil {
44- require .ErrorIs (t , err , http .ErrServerClosed )
45- }
42+ monitorParentProcess (slogger , runnerServer .Url (), token , monitorInterval )
43+ monitorShutdownChan <- struct {}{}
4644 }()
4745
48- // wait a moment for server to start
49- time .Sleep (monitorInterval * 2 )
50-
51- // clear the log
52- io .Copy (io .Discard , & logBytes )
5346 // let it run for a few intervals and make sure there is no error
5447 time .Sleep (monitorInterval * 4 )
5548
5649 // we should succeed now, nothing should be in the log
5750 require .Empty (t , logBytes .String ())
5851
59- // stop the server, should now start getting errors
52+ // stop the server -- we should now start getting errors
6053 require .NoError (t , runnerServer .Shutdown (context .Background ()))
6154 time .Sleep (monitorInterval * 8 )
6255
63- // should retry
56+ // `monitorParentProcess` should perform some retries during this time
6457 require .Contains (t , logBytes .String (), "will retry" )
6558
66- // should exit
67- require .Contains (t , logBytes .String (), "exiting" )
59+ // `monitorParentProcess` should exit
60+ select {
61+ case <- monitorShutdownChan :
62+ // monitorParentProcess returned
63+ case <- time .After (8 * monitorInterval ):
64+ t .Errorf ("monitorParentProcess did not exit after parent server shutdown: logs:\n %s\n " , logBytes .String ())
65+ t .FailNow ()
66+ }
6867}
0 commit comments