Skip to content

Commit

Permalink
chore: refactor after code review and fix erroneous test case
Browse files Browse the repository at this point in the history
  • Loading branch information
IronCore864 committed Oct 8, 2024
1 parent b46ca80 commit 1132f73
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions internals/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,14 +1341,17 @@ services:

// Wait for the change to be in doing state so that the service is in starting state.
for i := 0; ; i++ {
if i >= 10 {
if i >= 45 {
c.Fatalf("timed out waiting for change")
}
d.state.Lock()
change := d.state.Change(rsp.Change)
changeStatus := change.Status()
var status state.Status
if change != nil {
status = change.Status()
}
d.state.Unlock()
if change != nil && changeStatus == state.DoingStatus {
if status == state.DoingStatus {
break
}
time.Sleep(20 * time.Millisecond)
Expand Down
15 changes: 10 additions & 5 deletions internals/overlord/servstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,19 @@ services:
func (s *S) TestStopServiceWithinOkayDelay(c *C) {
// A longer okayDelay is used so that the change for starting the service won't
// quickly transition into the running state.
fakeOkayDelay := 5 * shortOkayDelay
fakeOkayDelay := 20 * shortOkayDelay
servstate.FakeOkayWait(fakeOkayDelay)

s.newServiceManager(c)
serviceName := "test-stop-within-okaywait"
// The service sleeps for fakeOkayDelay second then creates a side effect (a file at donePath).
layer := `
services:
%s:
override: replace
command: /bin/sh -c "sleep %g; {{.NotifyDoneCheck}}"
`
serviceName := "test-stop-within-okaywait"
s.planAddLayer(c, fmt.Sprintf(layer, serviceName, fakeOkayDelay))
s.planAddLayer(c, fmt.Sprintf(layer, serviceName, fakeOkayDelay.Seconds()))
s.planChanged(c)

// Start the service without waiting for change ready.
Expand All @@ -324,7 +324,12 @@ services:
s.st.Unlock()
s.runner.Ensure()

// Stop the service immediately within okayDelay
// Wait until the service is in the starting state.
s.waitUntilService(c, serviceName, func(service *servstate.ServiceInfo) bool {
return service.Current == servstate.StatusActive
})

// Stop the service within okayDelay.
chg := s.stopServices(c, [][]string{{serviceName}})
s.st.Lock()
c.Assert(chg.Err(), IsNil)
Expand All @@ -334,7 +339,7 @@ services:

s.st.Lock()
c.Check(chgStart.Status(), Equals, state.ErrorStatus)
c.Check(chgStart.Err(), ErrorMatches, `(?s).*cannot start service: exited quickly with code.*`)
c.Check(chgStart.Err(), ErrorMatches, fmt.Sprintf(`(?s).*stopped before the %s okay delay.*`, fakeOkayDelay))
s.st.Unlock()

donePath := filepath.Join(s.dir, serviceName)
Expand Down

0 comments on commit 1132f73

Please sign in to comment.