| 1 |
// Copyright 2012-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build windows |
| 15 |
|
| 16 |
package server |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"golang.org/x/sys/windows/svc" |
| 25 |
) |
| 26 |
|
| 27 |
// TestWinServiceWrapper reproduces the tests for service, |
| 28 |
// with extra complication for windows API |
| 29 |
func TestWinServiceWrapper(t *testing.T) { |
| 30 |
/* |
| 31 |
Since the windows API can't be tested through just the Run func, |
| 32 |
a basic mock checks the intractions of the server, as happens in svc.Run. |
| 33 |
This test checks : |
| 34 |
- that the service fails to start within an unreasonable timeframe (serverC) |
| 35 |
- that the service signals its state correctly to windows with svc.StartPending (mockC) |
| 36 |
- that no other signal is sent to the windows service API(mockC) |
| 37 |
*/ |
| 38 |
var ( |
| 39 |
wsw = &winServiceWrapper{New(DefaultOptions())} |
| 40 |
args = make([]string, 0) |
| 41 |
// Size of 1 should be enough but don't want to block if the test fails. |
| 42 |
serverC = make(chan error, 10) |
| 43 |
mockC = make(chan error, 10) |
| 44 |
changes = make(chan svc.ChangeRequest) |
| 45 |
status = make(chan svc.Status) |
| 46 |
) |
| 47 |
t.Setenv("NATS_STARTUP_DELAY", "1ns") // purposely small |
| 48 |
// prepare mock expectations |
| 49 |
wsm := &winSvcMock{status: status} |
| 50 |
wsm.Expect(svc.StartPending) |
| 51 |
|
| 52 |
go func() { |
| 53 |
mockC <- wsm.Listen(250 * time.Millisecond) |
| 54 |
}() |
| 55 |
|
| 56 |
go func() { |
| 57 |
var err error |
| 58 |
_, exitCode := wsw.Execute(args, changes, status) |
| 59 |
// We expect an error... |
| 60 |
if exitCode == 0 { |
| 61 |
err = errors.New("Should have exitCode != 0") |
| 62 |
} |
| 63 |
serverC <- err |
| 64 |
}() |
| 65 |
|
| 66 |
checkErr := func(c chan error, txt string) { |
| 67 |
select { |
| 68 |
case err := <-c: |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("%s: %v", txt, err) |
| 71 |
} |
| 72 |
case <-time.After(2 * time.Second): |
| 73 |
t.Fatal("Test timed out") |
| 74 |
} |
| 75 |
} |
| 76 |
checkErr(mockC, "windows.svc mock") |
| 77 |
checkErr(serverC, "server behavior") |
| 78 |
|
| 79 |
wsw.server.Shutdown() |
| 80 |
} |
| 81 |
|
| 82 |
func TestWinServiceLDMExit(t *testing.T) { |
| 83 |
var ( |
| 84 |
wsw = &winServiceWrapper{New(DefaultOptions())} |
| 85 |
args = make([]string, 0) |
| 86 |
// Size of 1 should be enough but don't want to block if the test fails. |
| 87 |
serverC = make(chan error, 10) |
| 88 |
mockC = make(chan error, 10) |
| 89 |
changes = make(chan svc.ChangeRequest, 1) |
| 90 |
status = make(chan svc.Status) |
| 91 |
) |
| 92 |
// prepare mock expectations |
| 93 |
wsm := &winSvcMock{status: status} |
| 94 |
wsm.Expect(svc.StartPending) |
| 95 |
// Expect that we will be running |
| 96 |
wsm.Expect(svc.Running) |
| 97 |
// Sending the LDM signal |
| 98 |
changes <- svc.ChangeRequest{Cmd: svc.Cmd(ldmCode)} |
| 99 |
// Expect to be stopping |
| 100 |
wsm.Expect(svc.StopPending) |
| 101 |
|
| 102 |
go func() { |
| 103 |
// Duration long enough for the test to complete, but not too long |
| 104 |
// to wait for nothing. |
| 105 |
mockC <- wsm.Listen(time.Second) |
| 106 |
}() |
| 107 |
|
| 108 |
go func() { |
| 109 |
var err error |
| 110 |
if _, exitCode := wsw.Execute(args, changes, status); exitCode != 0 { |
| 111 |
err = fmt.Errorf("exited with %v", exitCode) |
| 112 |
} |
| 113 |
serverC <- err |
| 114 |
}() |
| 115 |
|
| 116 |
checkErr := func(c chan error, txt string) { |
| 117 |
select { |
| 118 |
case err := <-c: |
| 119 |
if err != nil { |
| 120 |
t.Fatalf("%s: %v", txt, err) |
| 121 |
} |
| 122 |
case <-time.After(2 * time.Second): |
| 123 |
t.Fatal("Test timed out") |
| 124 |
} |
| 125 |
} |
| 126 |
checkErr(mockC, "windows.svc mock") |
| 127 |
checkErr(serverC, "server behavior") |
| 128 |
|
| 129 |
// At this point, we have proven already that the LDM signal did |
| 130 |
// end the NATS server's Execute() loop because we received the |
| 131 |
// svc.StopPending status. Still, we will verify that we get |
| 132 |
// notified that the server has shutdown. |
| 133 |
ch := make(chan struct{}, 1) |
| 134 |
go func() { |
| 135 |
wsw.server.WaitForShutdown() |
| 136 |
ch <- struct{}{} |
| 137 |
}() |
| 138 |
select { |
| 139 |
case <-ch: |
| 140 |
// OK |
| 141 |
case <-time.After(time.Second): |
| 142 |
t.Fatal("Did not finish shutdown") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// winSvcMock mocks part of the golang.org/x/sys/windows/svc |
| 147 |
// execution stack, listening to svc.Status on its chan. |
| 148 |
type winSvcMock struct { |
| 149 |
status chan svc.Status |
| 150 |
expectedSt []svc.State |
| 151 |
} |
| 152 |
|
| 153 |
// Expect allows to prepare a winSvcMock to receive a specific type of StatusMessage |
| 154 |
func (w *winSvcMock) Expect(st svc.State) { |
| 155 |
w.expectedSt = append(w.expectedSt, st) |
| 156 |
} |
| 157 |
|
| 158 |
// Listen is the mock's mainloop, expects messages to comply with previous Expect(). |
| 159 |
func (w *winSvcMock) Listen(dur time.Duration) error { |
| 160 |
timeout := time.NewTimer(dur) |
| 161 |
defer timeout.Stop() |
| 162 |
for _, state := range w.expectedSt { |
| 163 |
select { |
| 164 |
case status := <-w.status: |
| 165 |
if status.State != state { |
| 166 |
return fmt.Errorf("message to winsock: expected %v, got %v", state, status.State) |
| 167 |
} |
| 168 |
case <-timeout.C: |
| 169 |
return errors.New("Mock timed out") |
| 170 |
} |
| 171 |
} |
| 172 |
select { |
| 173 |
case <-timeout.C: |
| 174 |
return nil |
| 175 |
case st := <-w.status: |
| 176 |
return fmt.Errorf("extra message to winsock: got %v", st) |
| 177 |
|
| 178 |
} |
| 179 |
} |