Skip to content

Commit a028941

Browse files
committed
Add tests for bindLoggingPipe
Brings in a test from #107, further covering additional scenarios. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 1c6daa7 commit a028941

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

executor/logging.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ func bindLoggingPipe(name string, pipe io.Reader, output io.Writer, logPrefix bo
1616

1717
scanner := bufio.NewScanner(pipe)
1818

19-
size := bufio.MaxScanTokenSize
20-
21-
buffer := make([]byte, size)
22-
scanner.Buffer(buffer, size)
19+
buffer := make([]byte, maxBufferSize)
20+
scanner.Buffer(buffer, maxBufferSize)
2321

2422
logFlags := log.Flags()
2523
prefix := log.Prefix()

executor/logging_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package executor
5+
6+
import (
7+
"bytes"
8+
"log"
9+
"math"
10+
"os"
11+
"strings"
12+
"testing"
13+
"time"
14+
)
15+
16+
func TestBindLoggingPipe_ErrorsWithLargeToken(t *testing.T) {
17+
input := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`
18+
19+
reader := strings.NewReader(input)
20+
21+
logs := bytes.Buffer{}
22+
23+
log.SetOutput(&logs)
24+
defer func() {
25+
log.SetOutput(os.Stderr)
26+
}()
27+
28+
out := bytes.Buffer{}
29+
30+
maxBufferBytes := 32
31+
addPrefix := false
32+
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)
33+
34+
// give the pipe time to actually parse the logs
35+
time.Sleep(500 * time.Millisecond)
36+
37+
got := out.String()
38+
want := ""
39+
if want != got {
40+
t.Fatalf("expected empty string due to error, but got %q", got)
41+
}
42+
43+
wantSt := `bufio.Scanner: token too long`
44+
if !strings.Contains(logs.String(), wantSt) {
45+
t.Fatalf("want text: %q, but not found in: %q", wantSt, logs.String())
46+
}
47+
}
48+
49+
func TestBindLoggingPipe_ReadsValidSize(t *testing.T) {
50+
input := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
51+
`
52+
validSize := len(input)
53+
reader := strings.NewReader(input)
54+
55+
logs := bytes.Buffer{}
56+
57+
log.SetOutput(&logs)
58+
defer func() {
59+
log.SetOutput(os.Stderr)
60+
}()
61+
62+
out := bytes.Buffer{}
63+
64+
maxBufferBytes := validSize
65+
addPrefix := false
66+
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)
67+
68+
// give the pipe time to actually parse the logs
69+
time.Sleep(500 * time.Millisecond)
70+
71+
got := out.String()
72+
want := input
73+
if want != got {
74+
t.Fatalf("want output %q, but got %q", want, got)
75+
}
76+
77+
wantSt := `bufio.Scanner: token too long`
78+
if strings.Contains(logs.String(), wantSt) {
79+
t.Fatalf("Found error %s in output: %q", wantSt, logs.String())
80+
}
81+
}
82+
83+
func TestBindLoggingPipe_ReadsValidSizedLines(t *testing.T) {
84+
input1 := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
85+
`
86+
input2 := `Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
87+
`
88+
validSize := int(math.Max(float64(len(input1)), float64(len(input2))))
89+
90+
reader := strings.NewReader(input1 + input2)
91+
92+
logs := bytes.Buffer{}
93+
94+
log.SetOutput(&logs)
95+
defer func() {
96+
log.SetOutput(os.Stderr)
97+
}()
98+
99+
out := bytes.Buffer{}
100+
101+
maxBufferBytes := validSize
102+
addPrefix := false
103+
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)
104+
105+
// give the pipe time to actually parse the logs
106+
time.Sleep(500 * time.Millisecond)
107+
108+
got := out.String()
109+
want := input1 + input2
110+
if want != got {
111+
t.Fatalf("want output %q, but got %q", want, got)
112+
}
113+
114+
t.Logf(out.String())
115+
wantSt := `bufio.Scanner: token too long`
116+
if strings.Contains(logs.String(), wantSt) {
117+
t.Fatalf("Found error %s in output: %q", wantSt, logs.String())
118+
}
119+
}

0 commit comments

Comments
 (0)