-
Notifications
You must be signed in to change notification settings - Fork 899
fix: agent disconnects from coordinator #7430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Spike Curtis <spike@coder.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package tailnet_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"nhooyr.io/websocket" | ||
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
|
||
|
@@ -74,7 +80,10 @@ func TestCoordinator(t *testing.T) { | |
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug) | ||
coordinator := tailnet.NewCoordinator(logger) | ||
|
||
agentWS, agentServerWS := net.Pipe() | ||
// in this test we use real websockets to test use of deadlines | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong) | ||
defer cancel() | ||
agentWS, agentServerWS := websocketConn(ctx, t) | ||
defer agentWS.Close() | ||
agentNodeChan := make(chan []*tailnet.Node) | ||
sendAgentNode, agentErrChan := tailnet.ServeCoordinator(agentWS, func(nodes []*tailnet.Node) error { | ||
|
@@ -93,7 +102,7 @@ func TestCoordinator(t *testing.T) { | |
return coordinator.Node(agentID) != nil | ||
}, testutil.WaitShort, testutil.IntervalFast) | ||
|
||
clientWS, clientServerWS := net.Pipe() | ||
clientWS, clientServerWS := websocketConn(ctx, t) | ||
defer clientWS.Close() | ||
defer clientServerWS.Close() | ||
clientNodeChan := make(chan []*tailnet.Node) | ||
|
@@ -108,16 +117,28 @@ func TestCoordinator(t *testing.T) { | |
assert.NoError(t, err) | ||
close(closeClientChan) | ||
}() | ||
agentNodes := <-clientNodeChan | ||
require.Len(t, agentNodes, 1) | ||
select { | ||
case agentNodes := <-clientNodeChan: | ||
require.Len(t, agentNodes, 1) | ||
case <-ctx.Done(): | ||
t.Fatal("timed out") | ||
} | ||
sendClientNode(&tailnet.Node{}) | ||
clientNodes := <-agentNodeChan | ||
require.Len(t, clientNodes, 1) | ||
|
||
// wait longer than the internal wait timeout. | ||
// this tests for regression of https://github.com/coder/coder/issues/7428 | ||
time.Sleep(tailnet.WriteTimeout * 3 / 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should shorten the write timeout for tests so that we don't add a long delay? Even though we're running tests concurrently, if we have a lot of these it'll end up impacting test times. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, I could. I could make the write timeout an option, and plumb it through. However, when we run these tests in parallel, it can slow them down quite a bit, so I wouldn't want to set the timer too short, lest we get a flaky test under load. Maybe 1 second? 500 ms? The current sleep is 7.5s, and I'd be able to shave it down to maybe 750ms, which doesn't really feel worth the trouble to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid plumbing, how about something like: var WriteTimeout = func() time.Duration {
if inTest() {
return 1 * time.Second
}
return 5 * time.Second
}() ? Maybe that'd be Perhaps not ideal since this would affect all tests. I'll leave this up to you. |
||
|
||
// Ensure an update to the agent node reaches the client! | ||
sendAgentNode(&tailnet.Node{}) | ||
agentNodes = <-clientNodeChan | ||
require.Len(t, agentNodes, 1) | ||
select { | ||
case agentNodes := <-clientNodeChan: | ||
require.Len(t, agentNodes, 1) | ||
case <-ctx.Done(): | ||
t.Fatal("timed out") | ||
} | ||
|
||
// Close the agent WebSocket so a new one can connect. | ||
err := agentWS.Close() | ||
|
@@ -334,3 +355,25 @@ func TestCoordinator_AgentUpdateWhileClientConnects(t *testing.T) { | |
require.Len(t, cNodes, 1) | ||
require.Equal(t, 1, cNodes[0].PreferredDERP) | ||
} | ||
|
||
func websocketConn(ctx context.Context, t *testing.T) (client net.Conn, server net.Conn) { | ||
t.Helper() | ||
accepted := atomic.Bool{} | ||
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
// we should only call this handler once | ||
require.False(t, accepted.Load()) | ||
accepted.Store(true) | ||
wss, err := websocket.Accept(rw, r, nil) | ||
require.NoError(t, err) | ||
server = websocket.NetConn(r.Context(), wss, websocket.MessageBinary) | ||
|
||
// hold open until request context canceled | ||
<-r.Context().Done() | ||
})) | ||
t.Cleanup(s.Close) | ||
// nolint: bodyclose | ||
wsc, _, err := websocket.Dial(ctx, s.URL, nil) | ||
require.NoError(t, err) | ||
client = websocket.NetConn(ctx, wsc, websocket.MessageBinary) | ||
return client, server | ||
} |
Uh oh!
There was an error while loading. Please reload this page.