-
Notifications
You must be signed in to change notification settings - Fork 292
/
app_windows_test.go
122 lines (106 loc) · 3.7 KB
/
app_windows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2020-2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:build windows
// +build windows
package fx_test
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
"golang.org/x/sys/windows"
)
// Regression test for https://github.com/uber-go/fx/issues/781.
func TestWindowsCtrlCHandler(t *testing.T) {
// This test operates by launching a separate process,
// which we'll send a SIGINT to,
// and verifying the output of the application.
// Launch a separate process we will send SIGINT to.
testExe, err := os.Executable()
require.NoError(t, err, "determine test executable")
cmd := exec.Command(testExe, "-test.run", "TestWindowsMinimalApp")
cmd.Env = append(os.Environ(), "FX_TEST_FAKE=1")
// On Windows, we need to use GenerateConsoleCtrlEvent
// to SIGINT the child process.
//
// That API operates on Group ID granularity,
// so we need to make sure our new child process
// gets a new group ID rather than using the same ID
// as the test we're running.
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
}
stdout, err := cmd.StdoutPipe()
require.NoError(t, err, "create stdout")
stderr, err := cmd.StderrPipe()
require.NoError(t, err, "create stderr")
require.NoError(t, cmd.Start())
// Block until the child is ready by waiting for the "ready" text
// printed to stderr.
ready := make(chan struct{})
go func() {
defer close(ready)
stderr.Read(make([]byte, 1024))
}()
<-ready
require.NoError(t,
windows.GenerateConsoleCtrlEvent(1, uint32(cmd.Process.Pid)),
"SIGINT child process")
// Drain stdout and stderr, and wait for the process to exit.
output, err := io.ReadAll(stdout)
require.NoError(t, err)
_, err = io.Copy(io.Discard, stderr)
require.NoError(t, err)
require.NoError(t, cmd.Wait())
assert.Contains(t, string(output), "ONSTOP",
"stdout should include ONSTOP")
}
func TestWindowsMinimalApp(t *testing.T) {
// This is not a real test.
// It defines the behavior of the fake application
// that we spawn from TestWindowsCtrlCHandler.
if os.Getenv("FX_TEST_FAKE") != "1" {
return
}
// An Fx application that prints "ready" to stderr
// once its start hooks have been invoked,
// and "ONSTOP" to stdout when its stop hooks have been invoked.
fx.New(
fx.NopLogger,
fx.Invoke(func(lifecycle fx.Lifecycle) {
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
fmt.Fprintln(os.Stderr, "ready")
return nil
},
OnStop: func(ctx context.Context) error {
fmt.Fprintln(os.Stdout, "ONSTOP")
return nil
},
})
}),
).Run()
}