app: tests for handling service events
@@ -30,6 +30,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
+ "golang.org/x/sys/windows/svc"
)
// TestDoStartHappyPath tests the doStart method for windows. This method should
@@ -181,3 +182,66 @@ func TestHandler_RunAgent_ForceSaveWithTerminationHandler(t *testing.T) {
cancel()
wg.Wait()
}
+
+func TestHandler_HandleWindowsRequests_StopService(t *testing.T) {
+ requests := make(chan svc.ChangeRequest)
+ responses := make(chan svc.Status)
+
+ handler := &handler{}
+ wg := sync.WaitGroup{}
+ wg.Add(2)
+
+ go func() {
+ handler.handleWindowsRequests(context.TODO(), requests, responses)
+ wg.Done()
+ }()
+
+ go func() {
+ resp := <-responses
+ assert.Equal(t, svc.StartPending, resp.State, "Send StartPending immediately")
+ resp = <-responses
+ assert.Equal(t, svc.Running, resp.State, "Send Running after StartPending")
+ assert.Equal(t, svc.AcceptStop|svc.AcceptShutdown, resp.Accepts, "Accept stop & shutdown")
+ requests <- svc.ChangeRequest{Cmd: svc.Interrogate, CurrentStatus: svc.Status{State: svc.Running}}
+ resp = <-responses
+ assert.Equal(t, svc.Running, resp.State, "Send Running after Interrogate")
+ requests <- svc.ChangeRequest{Cmd: svc.Stop}
+ resp = <-responses
+ assert.Equal(t, svc.StopPending, resp.State, "Send StopPending after Stop")
+ wg.Done()
+ }()
+
+ wg.Wait()
+}
+
+func TestHandler_HandleWindowsRequests_Cancel(t *testing.T) {
+ requests := make(chan svc.ChangeRequest)
+ responses := make(chan svc.Status)
+
+ handler := &handler{}
+ ctx, cancel := context.WithCancel(context.TODO())
+ wg := sync.WaitGroup{}
+ wg.Add(2)
+
+ go func() {
+ handler.handleWindowsRequests(ctx, requests, responses)
+ wg.Done()
+ }()
+
+ go func() {
+ resp := <-responses
+ assert.Equal(t, svc.StartPending, resp.State, "Send StartPending immediately")
+ resp = <-responses
+ assert.Equal(t, svc.Running, resp.State, "Send Running after StartPending")
+ assert.Equal(t, svc.AcceptStop|svc.AcceptShutdown, resp.Accepts, "Accept stop & shutdown")
+ requests <- svc.ChangeRequest{Cmd: svc.Interrogate, CurrentStatus: svc.Status{State: svc.Running}}
+ resp = <-responses
+ assert.Equal(t, svc.Running, resp.State, "Send Running after Interrogate")
+ cancel()
+ resp = <-responses
+ assert.Equal(t, svc.StopPending, resp.State, "Send StopPending after Cancel")
+ wg.Done()
+ }()
+
+ wg.Wait()
+}
