Skip to content

Commit 1d15914

Browse files
committed
assume we receive logs by lines and don't ignore those without EOL
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e21a8d6 commit 1d15914

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

pkg/compose/logs_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func TestComposeService_Logs_Demux(t *testing.T) {
7171
c1Stdout := stdcopy.NewStdWriter(c1Writer, stdcopy.Stdout)
7272
c1Stderr := stdcopy.NewStdWriter(c1Writer, stdcopy.Stderr)
7373
go func() {
74-
_, err := c1Stdout.Write([]byte("hello stdout\n"))
74+
_, err := c1Stdout.Write([]byte("hello\n stdout"))
7575
assert.NoError(t, err, "Writing to fake stdout")
76-
_, err = c1Stderr.Write([]byte("hello stderr\n"))
76+
_, err = c1Stderr.Write([]byte("hello\n stderr"))
7777
assert.NoError(t, err, "Writing to fake stderr")
7878
_ = c1Writer.Close()
7979
}()
@@ -94,7 +94,7 @@ func TestComposeService_Logs_Demux(t *testing.T) {
9494

9595
require.Equal(
9696
t,
97-
[]string{"hello stdout", "hello stderr"},
97+
[]string{"hello", " stdout", "hello", " stderr"},
9898
consumer.LogsForContainer("c"),
9999
)
100100
}

pkg/utils/writer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ func (s *splitWriter) Write(b []byte) (int, error) {
4343
for {
4444
b = s.buffer.Bytes()
4545
index := bytes.Index(b, []byte{'\n'})
46-
if index < 0 {
46+
if index > 0 {
47+
line := s.buffer.Next(index + 1)
48+
s.consumer(string(line[:len(line)-1]))
49+
} else {
50+
line := s.buffer.String()
51+
s.buffer.Reset()
52+
if len(line) > 0 {
53+
s.consumer(line)
54+
}
4755
break
4856
}
49-
line := s.buffer.Next(index + 1)
50-
s.consumer(string(line[:len(line)-1]))
5157
}
5258
return n, nil
5359
}

pkg/utils/writer_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ func TestSplitWriter(t *testing.T) {
2828
w := GetWriter(func(line string) {
2929
lines = append(lines, line)
3030
})
31-
w.Write([]byte("h"))
32-
w.Write([]byte("e"))
33-
w.Write([]byte("l"))
34-
w.Write([]byte("l"))
35-
w.Write([]byte("o"))
36-
w.Write([]byte("\n"))
37-
w.Write([]byte("world!\n"))
31+
w.Write([]byte("hello\n"))
32+
w.Write([]byte("world\n"))
33+
w.Write([]byte("!"))
34+
assert.DeepEqual(t, lines, []string{"hello", "world", "!"})
35+
36+
}
37+
38+
//nolint:errcheck
39+
func TestSplitWriterNoEOL(t *testing.T) {
40+
var lines []string
41+
w := GetWriter(func(line string) {
42+
lines = append(lines, line)
43+
})
44+
w.Write([]byte("hello\n"))
45+
w.Write([]byte("world!"))
3846
assert.DeepEqual(t, lines, []string{"hello", "world!"})
3947

4048
}

0 commit comments

Comments
 (0)