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

Commit845a4c5

Browse files
committed
add a log volume test
1 parenta960b8e commit845a4c5

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

‎cli/server_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"runtime"
2626
"strconv"
2727
"strings"
28+
"sync"
2829
"sync/atomic"
2930
"testing"
3031
"time"
@@ -240,6 +241,70 @@ func TestServer(t *testing.T) {
240241
t.Fatalf("expected postgres URL to start with\"postgres://\", got %q",got)
241242
}
242243
})
244+
t.Run("SpammyLogs",func(t*testing.T) {
245+
// The purpose of this test is to ensure we don't show excessive logs when the server starts.
246+
t.Parallel()
247+
inv,cfg:=clitest.New(t,
248+
"server",
249+
"--in-memory",
250+
"--http-address",":0",
251+
"--access-url","http://localhost:3000/",
252+
"--cache-dir",t.TempDir(),
253+
)
254+
stdoutRW:=syncReaderWriter{}
255+
stderrRW:=syncReaderWriter{}
256+
inv.Stdout=io.MultiWriter(os.Stdout,&stdoutRW)
257+
inv.Stderr=io.MultiWriter(os.Stderr,&stderrRW)
258+
clitest.Start(t,inv)
259+
260+
// Wait for startup
261+
_=waitAccessURL(t,cfg)
262+
263+
// Wait a bit for more logs to be printed.
264+
time.Sleep(testutil.WaitShort)
265+
266+
// Lines containing these strings are printed because we're
267+
// running the server with a test config. They wouldn't be
268+
// normally shown to the user, so we'll ignore them.
269+
ignoreLines:= []string{
270+
"isn't externally reachable",
271+
"install.sh will be unavailable",
272+
"telemetry disabled, unable to notify of security issues",
273+
}
274+
275+
countLines:=func(fullOutputstring)int {
276+
terminalWidth:=80
277+
linesByNewline:=strings.Split(fullOutput,"\n")
278+
countByWidth:=0
279+
lineLoop:
280+
for_,line:=rangelinesByNewline {
281+
for_,ignoreLine:=rangeignoreLines {
282+
ifstrings.Contains(line,ignoreLine) {
283+
continue lineLoop
284+
}
285+
}
286+
ifline=="" {
287+
// Empty lines take up one line.
288+
countByWidth++
289+
}else {
290+
countByWidth+= (len(line)+terminalWidth-1)/terminalWidth
291+
}
292+
}
293+
returncountByWidth
294+
}
295+
296+
stdout,err:=io.ReadAll(&stdoutRW)
297+
iferr!=nil {
298+
t.Fatalf("failed to read stdout: %v",err)
299+
}
300+
stderr,err:=io.ReadAll(&stderrRW)
301+
iferr!=nil {
302+
t.Fatalf("failed to read stderr: %v",err)
303+
}
304+
305+
numLines:=countLines(string(stdout))+countLines(string(stderr))
306+
require.Less(t,numLines,20)
307+
})
243308

244309
// Validate that a warning is printed that it may not be externally
245310
// reachable.
@@ -2140,3 +2205,26 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
21402205

21412206
returnserverURL,deployment,snapshot
21422207
}
2208+
2209+
typesafeBufferstruct {
2210+
buf bytes.Buffer
2211+
mu sync.Mutex
2212+
}
2213+
2214+
// syncWriter provides a thread-safe io.ReadWriter implementation
2215+
typesyncReaderWriterstruct {
2216+
bufsafeBuffer
2217+
}
2218+
2219+
func (w*syncReaderWriter)Write(p []byte) (nint,errerror) {
2220+
w.buf.mu.Lock()
2221+
deferw.buf.mu.Unlock()
2222+
returnw.buf.buf.Write(p)
2223+
}
2224+
2225+
func (w*syncReaderWriter)Read(p []byte) (nint,errerror) {
2226+
w.buf.mu.Lock()
2227+
deferw.buf.mu.Unlock()
2228+
2229+
returnw.buf.buf.Read(p)
2230+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp