-
-
Notifications
You must be signed in to change notification settings - Fork 779
Expand file tree
/
Copy pathruntime_test.go
More file actions
106 lines (95 loc) · 2.61 KB
/
Copy pathruntime_test.go
File metadata and controls
106 lines (95 loc) · 2.61 KB
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
package main
import (
"context"
"errors"
"testing"
"time"
)
type fakeManagedApplication struct {
start func() error
startErr error
stop func(context.Context) error
serveErrors chan error
stopCalls chan context.Context
}
func (application *fakeManagedApplication) Start() error {
if application.start != nil {
return application.start()
}
return application.startErr
}
func (application *fakeManagedApplication) Stop(ctx context.Context) error {
if application.stopCalls != nil {
application.stopCalls <- ctx
}
if application.stop != nil {
return application.stop(ctx)
}
return nil
}
func (application *fakeManagedApplication) ServeErrors() <-chan error {
return application.serveErrors
}
func TestManagedRuntimeCleansUpAfterStartFailure(t *testing.T) {
startErr := errors.New("startup failed")
stopCalls := make(chan context.Context, 1)
runtime := &managedRuntime{
application: &fakeManagedApplication{
startErr: startErr,
stopCalls: stopCalls,
},
shutdownTimeout: 10 * time.Second,
}
err := runtime.start()
if !errors.Is(err, startErr) {
t.Fatalf("start() error = %v, want %v", err, startErr)
}
select {
case cleanupCtx := <-stopCalls:
deadline, ok := cleanupCtx.Deadline()
if !ok || time.Until(deadline) > time.Second {
t.Fatalf("startup cleanup deadline = %v, want at most one second", deadline)
}
default:
t.Fatal("start() did not clean up after failure")
}
}
func TestManagedRuntimeUsesConfiguredShutdownDeadline(t *testing.T) {
const timeout = 250 * time.Millisecond
stopCalls := make(chan context.Context, 1)
runtime := &managedRuntime{
application: &fakeManagedApplication{stopCalls: stopCalls},
shutdownTimeout: timeout,
}
if err := runtime.stop(nil); err != nil {
t.Fatalf("stop() error = %v", err)
}
ctx := <-stopCalls
deadline, ok := ctx.Deadline()
if !ok {
t.Fatal("shutdown context has no deadline")
}
remaining := time.Until(deadline)
if remaining <= 0 || remaining > timeout {
t.Fatalf("shutdown deadline remaining = %v, want within %v", remaining, timeout)
}
}
func TestManagedRuntimeCancelsShutdownWhenForced(t *testing.T) {
stopStarted := make(chan struct{})
runtime := &managedRuntime{
application: &fakeManagedApplication{stop: func(ctx context.Context) error {
close(stopStarted)
<-ctx.Done()
return context.Cause(ctx)
}},
shutdownTimeout: time.Minute,
}
force := make(chan struct{})
result := make(chan error, 1)
go func() { result <- runtime.stop(force) }()
<-stopStarted
close(force)
if err := <-result; !errors.Is(err, context.Canceled) {
t.Fatalf("forced stop error = %v, want context canceled", err)
}
}