Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions cmd/lk/agent_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ func writeMsg(t *testing.T, conn net.Conn, m *agent.AgentDevMessage) {
require.NoError(t, ipc.WriteProto(conn, m))
}

// startSession runs s until the test ends, then closes its connection and
// waits for run to return, so a session never outlives its test: run prints
// through the shared out, which later tests replace.
func startSession(t *testing.T, s *devSession) {
t.Helper()
done := make(chan struct{})
go func() {
s.run()
close(done)
}()
t.Cleanup(func() {
_ = s.conn.Close()
<-done
})
}

func jobsRequest() *agent.AgentDevMessage {
return &agent.AgentDevMessage{
Message: &agent.AgentDevMessage_GetRunningJobsRequest{
Expand All @@ -57,7 +73,7 @@ func TestDevSession_Restore_ServerInfo(t *testing.T) {
defer agentConn.Close()

s := rs.newSession(cliConn)
go s.run()
startSession(t, s)

// Agent sends ServerInfo, then the jobs request.
writeMsg(t, agentConn, &agent.AgentDevMessage{
Expand Down Expand Up @@ -93,7 +109,7 @@ func TestDevSession_Restore_NoServerInfo(t *testing.T) {
defer agentConn.Close()

s := rs.newSession(cliConn)
go s.run()
startSession(t, s)

writeMsg(t, agentConn, jobsRequest())

Expand All @@ -118,7 +134,7 @@ func TestDevSession_TakeSavedJobs_SingleUse(t *testing.T) {
defer agentConn.Close()

s := rs.newSession(cliConn)
go s.run()
startSession(t, s)

agentConn.SetDeadline(time.Now().Add(2 * time.Second))

Expand All @@ -144,7 +160,7 @@ func TestDevSession_CaptureJobs(t *testing.T) {
defer agentConn.Close()

s := newDevSession(cliConn)
go s.run()
startSession(t, s)

// Stand-in agent: answer the inbound jobs request with two jobs.
go func() {
Expand Down Expand Up @@ -180,7 +196,7 @@ func TestDevSession_CaptureJobs_Timeout(t *testing.T) {
}()

s := newDevSession(cliConn)
go s.run()
startSession(t, s)

_, err := s.getRunningJobs(100 * time.Millisecond)
assert.ErrorIs(t, err, errDevSessionTimeout)
Expand All @@ -193,7 +209,7 @@ func TestDevSession_CaptureJobs_Closed(t *testing.T) {
defer agentConn.Close()

s := newDevSession(cliConn)
go s.run()
startSession(t, s)

go func() {
req := &agent.AgentDevMessage{}
Expand Down
Loading
Loading