Repository navigation
Expand file tree
/
Copy pathloopback_close_test.go
More file actions
143 lines (135 loc) · 4.54 KB
/
Copy pathloopback_close_test.go
File metadata and controls
143 lines (135 loc) · 4.54 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
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
package remote
import (
"testing"
"testing/synctest"
"github.com/Agent-Field/codeaf/internal/session"
)
// closeGatedAgent is a conversation whose close does not finish until the test
// says so. It is how the engine's way out is held open on purpose, rather than
// by hoping a slow disk or a busy scheduler holds it open for us — which is the
// only way #1647 ever showed itself.
type closeGatedAgent struct {
*fakeAgent
entered chan struct{}
release chan struct{}
}
func newCloseGatedAgent() *closeGatedAgent {
return &closeGatedAgent{fakeAgent: &fakeAgent{}, entered: make(chan struct{}), release: make(chan struct{})}
}
func (a *closeGatedAgent) Close() error {
close(a.entered)
<-a.release
return a.fakeAgent.Close()
}
// A CLOSED LOOP HAS NO ENGINE LEFT RUNNING. Close is asked while the
// conversation's own close is held open, and it must still be waiting once
// every goroutine in the bubble has gone as far as it can go.
//
// THE BUBBLE IS WHAT MAKES "STILL WAITING" A FACT RATHER THAN A GUESS.
// [synctest.Wait] returns only when every goroutine the test started is durably
// blocked, so a Close that does not join has certainly returned by then and a
// Close that does has certainly not — there is no window to size and no clock.
// On the Close that did not join, this test failed every time (#1647).
func TestLoopCloseWaitsForTheEngineToFinishLeaving(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
agent := newCloseGatedAgent()
loop, err := Loopback(Hello{Version: Version}, Options{Boot: func(Hello) (*Engine, error) {
return &Engine{Agent: agent}, nil
}})
if err != nil {
t.Fatal(err)
}
returned := make(chan error, 1)
go func() { returned <- loop.Close() }()
<-agent.entered
synctest.Wait()
select {
case err := <-returned:
close(agent.release)
<-loop.Served
t.Fatalf("Close returned (%v) while the conversation behind it was still closing", err)
default:
}
close(agent.release)
if err := <-returned; err != nil {
t.Fatalf("Close: %v", err)
}
// THE ANSWER IS ALREADY THERE. Close waited for the engine goroutine,
// which puts Serve's answer on Served before it lets Close go, so a
// receive that has to wait here is a Close that let go too early.
select {
case err := <-loop.Served:
if err != nil {
t.Fatalf("Serve after Close: %v", err)
}
default:
t.Fatal("Close returned before Serve had answered")
}
})
}
// CUT IS STILL A LOST LINK, AND A CLOSE AFTER IT STILL JOINS. Cut shuts the
// pipe without a goodbye and does not wait, because the surface it stages is
// one that is about to redial; the Close a test's cleanup calls afterwards must
// wait all the same, and must not turn the retirement Cut caused into a person
// leaving.
func TestLoopCloseAfterCutStillJoinsAndKeepsTheLostLink(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
agent := newCloseGatedAgent()
loop, err := Loopback(Hello{Version: Version}, Options{Boot: func(Hello) (*Engine, error) {
return &Engine{Agent: agent}, nil
}})
if err != nil {
t.Fatal(err)
}
if err := loop.Cut(); err != nil {
t.Fatal(err)
}
<-agent.entered
returned := make(chan error, 1)
go func() { returned <- loop.Close() }()
synctest.Wait()
select {
case err := <-returned:
close(agent.release)
<-loop.Served
t.Fatalf("Close after Cut returned (%v) while the conversation was still closing", err)
default:
}
close(agent.release)
if err := <-returned; err != nil {
t.Fatalf("Close after Cut: %v", err)
}
if err := <-loop.Served; err != nil {
t.Fatalf("Serve after Cut and Close: %v", err)
}
if agent.stopDoor != session.StopByRetired {
t.Fatalf("the cut link ended through door %q, want the retired door a lost link takes", agent.stopDoor)
}
if err := loop.Close(); err != nil {
t.Fatalf("a second Close: %v", err)
}
})
}
// SERVED IS THE CALLER'S, EVEN AFTER CLOSE HAS JOINED. Close waits on its own
// signal and never receives from Served, so the error an engine died of is
// still there for the test that wants to read it.
func TestLoopCloseLeavesServedErrorReadable(t *testing.T) {
loop, err := Loopback(Hello{Version: Version}, Options{Boot: func(Hello) (*Engine, error) {
return &Engine{Agent: &fakeAgent{}}, nil
}})
if err != nil {
t.Fatal(err)
}
if _, err := loop.surface.Write([]byte("{bad frame}\n")); err != nil {
t.Fatal(err)
}
if err := loop.Close(); err != nil {
t.Fatal(err)
}
if err := <-loop.Served; err == nil {
t.Fatal("Serve's bad-frame error was lost")
}
if err := loop.Close(); err != nil {
t.Fatalf("a second Close: %v", err)
}
}