-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
194 lines (167 loc) · 4.09 KB
/
program.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package testexec
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"runtime"
"syscall"
"testing"
)
type Nil chan struct{ private struct{} }
var (
calledFromMain = false
programs = make(map[string]program)
nilChanType = typeOf[Nil]()
)
const envKey = "TESTEXEC_KEY"
type ProgramHandle[In any, Out any] string
type program struct {
fn func(*T, io.Reader, io.WriteCloser)
options options
}
func NewProgram[In any, Out any](f func(*T, In, Out), opts ...ProgramOption) ProgramHandle[In, Out] {
if calledFromMain {
panic("should not call NewProgram after RunTestMain")
}
var prg program
for _, o := range opts {
o(&prg.options)
}
var key string
if _, file, line, ok := runtime.Caller(1); ok {
key = fmt.Sprintf("%d|%s", line, file)
}
if _, ok := programs[key]; key == "" || ok {
panic(fmt.Sprintf("invalid exec_key = `%s'", key))
}
inType := typeOf[In]()
if inType.Kind() == reflect.Chan {
if inType == nilChanType {
// noop
} else if inType.ChanDir() != reflect.RecvDir {
panic("request parameter must be recv channel")
}
}
outType := typeOf[Out]()
if outType.Kind() == reflect.Chan {
if outType == nilChanType {
// noop
} else if outType.ChanDir() != reflect.SendDir {
panic("response parameter must be send channel")
}
} else if outType.Kind() != reflect.Ptr {
panic("response parameter must be channel or pointer")
}
prg.fn = func(t *T, in io.Reader, out io.WriteCloser) {
var (
inArg In
outArg Out
)
switch inType.Kind() {
case reflect.Chan:
ch := makeChan[In](0)
setValue(&inArg, ch)
go func() {
if err := decodeValues(ch, in); err != nil {
panic(fmt.Sprintf("fail to decode request: %s", err))
}
}()
default:
if err := decodeSingle(&inArg, in); err != nil {
panic(fmt.Sprintf("fail to decode request: %s", err))
}
}
switch outType.Kind() {
case reflect.Chan:
done := make(chan struct{})
ch := makeChan[Out](0)
setValue(&outArg, ch)
go func() {
if err := encodeValues(ch, out); err != nil {
panic(fmt.Sprintf("fail to encode response: %s", err))
}
close(done)
}()
f(t, inArg, outArg)
ch.Close()
<-done
case reflect.Ptr:
setValue(&outArg, reflect.New(outType.Elem()))
f(t, inArg, outArg)
if err := encodeSingle(outArg, out); err != nil {
panic(fmt.Sprintf("fail to encode response: %s", err))
}
}
}
programs[key] = prg
return ProgramHandle[In, Out](key)
}
func (c ProgramHandle[In, Out]) exec(ctx context.Context, request In, response Out, args []string) *Cmd {
cmd := &Cmd{ctx: ctx}
if ctx != nil {
cmd.Cmd = exec.CommandContext(ctx, os.Args[0], args...)
} else {
cmd.Cmd = exec.Command(os.Args[0], args...)
}
cmd.Cmd.Env = append(os.Environ(), envKey+"="+string(c))
if wd, err := os.Getwd(); err == nil {
cmd.Cmd.Dir = wd
}
cmd.Cmd.Stderr = os.Stderr
cmd.Cmd.Stdout = os.Stdout
cmd.Cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM,
}
inR, inW, _ := os.Pipe()
outR, outW, _ := os.Pipe()
cmd.Cmd.ExtraFiles = []*os.File{inR, outW}
cmd.closers = append(cmd.closers, inR, inW, outR, outW)
cmd.wg.Go(func() error {
var err error
switch typeOf[Out]().Kind() {
case reflect.Chan:
err = encodeValues(reflect.ValueOf(request), inW)
default:
err = encodeSingle(request, inW)
}
if err != nil {
return fmt.Errorf("fail to encode request: %w", err)
}
return nil
})
cmd.wg.Go(func() error {
var err error
switch typeOf[Out]().Kind() {
case reflect.Chan:
err = decodeValues(reflect.ValueOf(response), outR)
default:
err = decodeSingle(&response, outR)
}
if err != nil {
return fmt.Errorf("fail to decode response: %w", err)
}
return nil
})
return cmd
}
func Main(m *testing.M) {
calledFromMain = true
if key := os.Getenv(envKey); key != "" {
inPipe := os.NewFile(uintptr(3), "")
outPipe := os.NewFile(uintptr(4), "")
defer inPipe.Close()
defer outPipe.Close()
prg, ok := programs[key]
if !ok {
panic(fmt.Sprintf("program not found, exec_key = %s", key))
}
prg.fn(&T{
name: prg.options.Name,
}, inPipe, outPipe)
return
}
os.Exit(m.Run())
}