Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(coderd/healthcheck/derphealth): do not override parent context de…
…adline
  • Loading branch information
johnstcn committed Jun 6, 2024
commit 7c9d51a0afcb19ec6fb666c040cbc917216ef8fd
8 changes: 6 additions & 2 deletions coderd/healthcheck/derphealth/derp.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ func (r *NodeReport) derpURL() *url.URL {
}

func (r *NodeReport) Run(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
// If there already is a deadline set on the context, do not override it.
if _, ok := ctx.Deadline(); !ok {
dCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
ctx = dCtx
}

r.Severity = health.SeverityOK
r.ClientLogs = [][]string{}
Expand Down
42 changes: 42 additions & 0 deletions coderd/healthcheck/derphealth/derp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -84,6 +85,47 @@ func TestDERP(t *testing.T) {
}
})

t.Run("TimeoutCtx", func(t *testing.T) {
t.Parallel()

derpSrv := derp.NewServer(key.NewNode(), func(format string, args ...any) { t.Logf(format, args...) })
defer derpSrv.Close()
srv := httptest.NewServer(derphttp.Handler(derpSrv))
defer srv.Close()

var (
// nolint:gocritic // testing a deadline exceeded
ctx, cancel = context.WithTimeout(context.Background(), time.Nanosecond)
report = derphealth.Report{}
derpURL, _ = url.Parse(srv.URL)
opts = &derphealth.ReportOptions{
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
1: {
EmbeddedRelay: true,
RegionID: 999,
Nodes: []*tailcfg.DERPNode{{
Name: "1a",
RegionID: 999,
HostName: derpURL.Host,
IPv4: derpURL.Host,
STUNPort: -1,
InsecureForTests: true,
ForceHTTP: true,
}},
},
}},
}
)
cancel()

report.Run(ctx, opts)

assert.False(t, report.Healthy)
if assert.NotNil(t, report.Error) {
assert.Contains(t, context.DeadlineExceeded, *report.Error)
}
})

t.Run("HealthyWithNodeDegraded", func(t *testing.T) {
t.Parallel()

Expand Down