-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
testing.go
243 lines (210 loc) Β· 6.88 KB
/
testing.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package terminal
import (
"context"
"time"
"github.com/safing/portbase/container"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/spn/cabin"
"github.com/safing/portmaster/spn/hub"
)
const (
defaultTestQueueSize = 16
defaultTestPadding = 8
logTestCraneMsgs = false
)
// TestTerminal is a terminal for running tests.
type TestTerminal struct {
*TerminalBase
}
// NewLocalTestTerminal returns a new local test terminal.
func NewLocalTestTerminal(
ctx context.Context,
id uint32,
parentID string,
remoteHub *hub.Hub,
initMsg *TerminalOpts,
upstream Upstream,
) (*TestTerminal, *container.Container, *Error) {
// Create Terminal Base.
t, initData, err := NewLocalBaseTerminal(ctx, id, parentID, remoteHub, initMsg, upstream)
if err != nil {
return nil, nil, err
}
t.StartWorkers(module, "test terminal")
return &TestTerminal{t}, initData, nil
}
// NewRemoteTestTerminal returns a new remote test terminal.
func NewRemoteTestTerminal(
ctx context.Context,
id uint32,
parentID string,
identity *cabin.Identity,
initData *container.Container,
upstream Upstream,
) (*TestTerminal, *TerminalOpts, *Error) {
// Create Terminal Base.
t, initMsg, err := NewRemoteBaseTerminal(ctx, id, parentID, identity, initData, upstream)
if err != nil {
return nil, nil, err
}
t.StartWorkers(module, "test terminal")
return &TestTerminal{t}, initMsg, nil
}
type delayedMsg struct {
msg *Msg
timeout time.Duration
delayUntil time.Time
}
func createDelayingTestForwardingFunc(
srcName,
dstName string,
delay time.Duration,
delayQueueSize int,
deliverFunc func(msg *Msg, timeout time.Duration) *Error,
) func(msg *Msg, timeout time.Duration) *Error {
// Return simple forward func if no delay is given.
if delay == 0 {
return func(msg *Msg, timeout time.Duration) *Error {
// Deliver to other terminal.
dErr := deliverFunc(msg, timeout)
if dErr != nil {
log.Errorf("spn/testing: %s>%s: failed to deliver to terminal: %s", srcName, dstName, dErr)
return dErr
}
return nil
}
}
// If there is delay, create a delaying channel and handler.
delayedMsgs := make(chan *delayedMsg, delayQueueSize)
go func() {
for {
// Read from chan
msg := <-delayedMsgs
if msg == nil {
return
}
// Check if we need to wait.
waitFor := time.Until(msg.delayUntil)
if waitFor > 0 {
time.Sleep(waitFor)
}
// Deliver to other terminal.
dErr := deliverFunc(msg.msg, msg.timeout)
if dErr != nil {
log.Errorf("spn/testing: %s>%s: failed to deliver to terminal: %s", srcName, dstName, dErr)
}
}
}()
return func(msg *Msg, timeout time.Duration) *Error {
// Add msg to delaying msg channel.
delayedMsgs <- &delayedMsg{
msg: msg,
timeout: timeout,
delayUntil: time.Now().Add(delay),
}
return nil
}
}
// HandleAbandon gives the terminal the ability to cleanly shut down.
// The returned error is the error to send to the other side.
// Should never be called directly. Call Abandon() instead.
func (t *TestTerminal) HandleAbandon(err *Error) (errorToSend *Error) {
switch err {
case nil:
// nil means that the Terminal is being shutdown by the owner.
log.Tracef("spn/terminal: %s is closing", fmtTerminalID(t.parentID, t.id))
default:
// All other errors are faults.
log.Warningf("spn/terminal: %s: %s", fmtTerminalID(t.parentID, t.id), err)
}
return
}
// NewSimpleTestTerminalPair provides a simple conntected terminal pair for tests.
func NewSimpleTestTerminalPair(delay time.Duration, delayQueueSize int, opts *TerminalOpts) (a, b *TestTerminal, err error) {
if opts == nil {
opts = &TerminalOpts{
Padding: defaultTestPadding,
FlowControl: FlowControlDFQ,
FlowControlSize: defaultTestQueueSize,
}
}
var initData *container.Container
var tErr *Error
a, initData, tErr = NewLocalTestTerminal(
module.Ctx, 127, "a", nil, opts, UpstreamSendFunc(createDelayingTestForwardingFunc(
"a", "b", delay, delayQueueSize, func(msg *Msg, timeout time.Duration) *Error {
return b.Deliver(msg)
},
)),
)
if tErr != nil {
return nil, nil, tErr.Wrap("failed to create local test terminal")
}
b, _, tErr = NewRemoteTestTerminal(
module.Ctx, 127, "b", nil, initData, UpstreamSendFunc(createDelayingTestForwardingFunc(
"b", "a", delay, delayQueueSize, func(msg *Msg, timeout time.Duration) *Error {
return a.Deliver(msg)
},
)),
)
if tErr != nil {
return nil, nil, tErr.Wrap("failed to create remote test terminal")
}
return a, b, nil
}
// BareTerminal is a bare terminal that just returns errors for testing.
type BareTerminal struct{}
var (
_ Terminal = &BareTerminal{}
errNotImplementedByBareTerminal = ErrInternalError.With("not implemented by bare terminal")
)
// ID returns the terminal ID.
func (t *BareTerminal) ID() uint32 {
return 0
}
// Ctx returns the terminal context.
func (t *BareTerminal) Ctx() context.Context {
return context.Background()
}
// Deliver delivers a message to the terminal.
// Should not be overridden by implementations.
func (t *BareTerminal) Deliver(msg *Msg) *Error {
return errNotImplementedByBareTerminal
}
// Send is used by others to send a message through the terminal.
// Should not be overridden by implementations.
func (t *BareTerminal) Send(msg *Msg, timeout time.Duration) *Error {
return errNotImplementedByBareTerminal
}
// Flush sends all messages waiting in the terminal.
// Should not be overridden by implementations.
func (t *BareTerminal) Flush(timeout time.Duration) {}
// StartOperation starts the given operation by assigning it an ID and sending the given operation initialization data.
// Should not be overridden by implementations.
func (t *BareTerminal) StartOperation(op Operation, initData *container.Container, timeout time.Duration) *Error {
return errNotImplementedByBareTerminal
}
// StopOperation stops the given operation.
// Should not be overridden by implementations.
func (t *BareTerminal) StopOperation(op Operation, err *Error) {}
// Abandon shuts down the terminal unregistering it from upstream and calling HandleAbandon().
// Should not be overridden by implementations.
func (t *BareTerminal) Abandon(err *Error) {}
// HandleAbandon gives the terminal the ability to cleanly shut down.
// The terminal is still fully functional at this point.
// The returned error is the error to send to the other side.
// Should never be called directly. Call Abandon() instead.
// Meant to be overridden by implementations.
func (t *BareTerminal) HandleAbandon(err *Error) (errorToSend *Error) {
return err
}
// HandleDestruction gives the terminal the ability to clean up.
// The terminal has already fully shut down at this point.
// Should never be called directly. Call Abandon() instead.
// Meant to be overridden by implementations.
func (t *BareTerminal) HandleDestruction(err *Error) {}
// FmtID formats the terminal ID (including parent IDs).
// May be overridden by implementations.
func (t *BareTerminal) FmtID() string {
return "bare"
}