blob: c38d7a62b64ea9a5b9265fa7a061494c78fa9a99 [file] [log] [blame]
Lorenz Brun25b82a82020-03-23 20:27:51 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package logbuffer
18
19import (
20 "testing"
21
22 "github.com/stretchr/testify/require"
23)
24
25func TestSingleLine(t *testing.T) {
26 buf := New(1, 16000)
27 buf.Write([]byte("Hello World\n"))
28 out := buf.ReadLines(1)
29 require.Len(t, out, 1, "Invalid number of lines read")
30 require.Equal(t, "Hello World", out[0].Data, "Read bad log line")
31 require.Equal(t, 11, out[0].OriginalLength, "Invalid line length")
32}
33
34func TestPartialWritesAndReads(t *testing.T) {
35 buf := New(2, 16000)
36 buf.Write([]byte("Hello "))
37 buf.Write([]byte("World\nTest "))
38 buf.Write([]byte("2\n"))
39
40 out := buf.ReadLines(1)
41 require.Len(t, out, 1, "Invalid number of lines for partial read")
42 require.Equal(t, "Test 2", out[0].Data, "Read bad log line")
43
44 out2 := buf.ReadLines(2)
45 require.Len(t, out2, 2, "Invalid number of lines read")
46 require.Equal(t, "Hello World", out2[0].Data, "Read bad log line")
47 require.Equal(t, "Test 2", out2[1].Data, "Read bad log line")
48}
49
50func TestBufferOverwrite(t *testing.T) {
51 buf := New(3, 16000)
52 buf.Write([]byte("Test1\nTest2\nTest3\nTest4\n"))
53
54 out := buf.ReadLines(3)
55 require.Equal(t, out[0].Data, "Test2", "Read bad log line")
56 require.Equal(t, out[1].Data, "Test3", "Read bad log line")
57 require.Equal(t, out[2].Data, "Test4", "Overwritten data is invalid")
58}
59
60func TestTooLargeRequests(t *testing.T) {
61 buf := New(1, 16000)
62 outEmpty := buf.ReadLines(1)
63 require.Len(t, outEmpty, 0, "Returned more data than there is")
64
65 buf.Write([]byte("1\n2\n"))
66 out := buf.ReadLines(2)
67 require.Len(t, out, 1, "Returned more data than the ring buffer can hold")
68}
69
70func TestSpecialCases(t *testing.T) {
71 buf := New(2, 16000)
72 buf.Write([]byte("Test1"))
73 buf.Write([]byte("\nTest2\n"))
74 out := buf.ReadLines(2)
75 require.Len(t, out, 2, "Too many lines written")
76 require.Equal(t, out[0].Data, "Test1", "Read bad log line")
77 require.Equal(t, out[1].Data, "Test2", "Read bad log line")
78}
79
80func TestLineLengthLimit(t *testing.T) {
81 buf := New(2, 6)
82
83 testStr := "Just Testing"
84
85 buf.Write([]byte(testStr + "\nShort\n"))
86
87 out := buf.ReadLines(2)
88 require.Equal(t, len(testStr), out[0].OriginalLength, "Line is over length limit")
89 require.Equal(t, "Just T", out[0].Data, "Log line not properly truncated")
90
91 out2 := buf.ReadLinesTruncated(2, "...")
92 require.Equal(t, out2[0], "Just T...", "Line is over length limit")
93 require.Equal(t, out2[1], "Short", "Truncated small enough line")
94}