Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d122aa

Browse files
authored
chore(cli): avoid use of testutil.RandomPort() in prometheus test (#17297)
Should hopefullyfixcoder/internal#282Instead of picking a random port for the prometheus server, listen on`:0` and read the port from the CLI stdout.
1 parent0e65821 commit8d122aa

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

‎cli/agent.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"net"
78
"net/http"
89
"net/http/pprof"
910
"net/url"
@@ -491,8 +492,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
491492
}
492493

493494
funcServeHandler(ctx context.Context,logger slog.Logger,handler http.Handler,addr,namestring) (closeFuncfunc()) {
494-
logger.Debug(ctx,"http server listening",slog.F("addr",addr),slog.F("name",name))
495-
496495
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
497496
// websockets over the dev tunnel.
498497
// See: https://github.com/coder/coder/pull/3730
@@ -502,9 +501,15 @@ func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler,
502501
Handler:handler,
503502
}
504503
gofunc() {
505-
err:=srv.ListenAndServe()
506-
iferr!=nil&&!xerrors.Is(err,http.ErrServerClosed) {
507-
logger.Error(ctx,"http server listen",slog.F("name",name),slog.Error(err))
504+
ln,err:=net.Listen("tcp",addr)
505+
iferr!=nil {
506+
logger.Error(ctx,"http server listen",slog.F("name",name),slog.F("addr",addr),slog.Error(err))
507+
return
508+
}
509+
deferln.Close()
510+
logger.Info(ctx,"http server listening",slog.F("addr",ln.Addr()),slog.F("name",name))
511+
iferr:=srv.Serve(ln);err!=nil&&!xerrors.Is(err,http.ErrServerClosed) {
512+
logger.Error(ctx,"http server serve",slog.F("addr",ln.Addr()),slog.F("name",name),slog.Error(err))
508513
}
509514
}()
510515

‎cli/server_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"reflect"
25+
"regexp"
2526
"runtime"
2627
"strconv"
2728
"strings"
@@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) {
12171218
t.Parallel()
12181219

12191220
ctx:=testutil.Context(t,testutil.WaitLong)
1220-
randPort:=testutil.RandomPort(t)
1221-
inv,cfg:=clitest.New(t,
1221+
inv,_:=clitest.New(t,
12221222
"server",
12231223
"--in-memory",
12241224
"--http-address",":0",
12251225
"--access-url","http://example.com",
12261226
"--provisioner-daemons","1",
12271227
"--prometheus-enable",
1228-
"--prometheus-address",":"+strconv.Itoa(randPort),
1228+
"--prometheus-address",":0",
12291229
// "--prometheus-collect-db-metrics", // disabled by default
12301230
"--cache-dir",t.TempDir(),
12311231
)
12321232

1233+
pty:=ptytest.New(t)
1234+
inv.Stdout=pty.Output()
1235+
inv.Stderr=pty.Output()
1236+
12331237
clitest.Start(t,inv)
1234-
_=waitAccessURL(t,cfg)
1238+
1239+
// Wait until we see the prometheus address in the logs.
1240+
addrMatchExpr:=`http server listening\s+addr=(\S+)\s+name=prometheus`
1241+
lineMatch:=pty.ExpectRegexMatchContext(ctx,addrMatchExpr)
1242+
promAddr:=regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]
12351243

12361244
testutil.Eventually(ctx,t,func(ctx context.Context)bool {
1237-
req,err:=http.NewRequestWithContext(ctx,"GET",fmt.Sprintf("http://127.0.0.1:%d/metrics",randPort),nil)
1245+
req,err:=http.NewRequestWithContext(ctx,"GET",fmt.Sprintf("http://%s/metrics",promAddr),nil)
12381246
iferr!=nil {
12391247
t.Logf("error creating request: %s",err.Error())
12401248
returnfalse
@@ -1272,24 +1280,31 @@ func TestServer(t *testing.T) {
12721280
t.Parallel()
12731281

12741282
ctx:=testutil.Context(t,testutil.WaitLong)
1275-
randPort:=testutil.RandomPort(t)
1276-
inv,cfg:=clitest.New(t,
1283+
inv,_:=clitest.New(t,
12771284
"server",
12781285
"--in-memory",
12791286
"--http-address",":0",
12801287
"--access-url","http://example.com",
12811288
"--provisioner-daemons","1",
12821289
"--prometheus-enable",
1283-
"--prometheus-address",":"+strconv.Itoa(randPort),
1290+
"--prometheus-address",":0",
12841291
"--prometheus-collect-db-metrics",
12851292
"--cache-dir",t.TempDir(),
12861293
)
12871294

1295+
pty:=ptytest.New(t)
1296+
inv.Stdout=pty.Output()
1297+
inv.Stderr=pty.Output()
1298+
12881299
clitest.Start(t,inv)
1289-
_=waitAccessURL(t,cfg)
1300+
1301+
// Wait until we see the prometheus address in the logs.
1302+
addrMatchExpr:=`http server listening\s+addr=(\S+)\s+name=prometheus`
1303+
lineMatch:=pty.ExpectRegexMatchContext(ctx,addrMatchExpr)
1304+
promAddr:=regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]
12901305

12911306
testutil.Eventually(ctx,t,func(ctx context.Context)bool {
1292-
req,err:=http.NewRequestWithContext(ctx,"GET",fmt.Sprintf("http://127.0.0.1:%d/metrics",randPort),nil)
1307+
req,err:=http.NewRequestWithContext(ctx,"GET",fmt.Sprintf("http://%s/metrics",promAddr),nil)
12931308
iferr!=nil {
12941309
t.Logf("error creating request: %s",err.Error())
12951310
returnfalse

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp