-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineBuffer_test.go
145 lines (138 loc) · 3.74 KB
/
lineBuffer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019-2020 The grok_exporter Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package go_tailer
import (
"fmt"
"github.com/jdrews/go-tailer/fswatcher"
"math/rand"
"sync"
"testing"
"time"
)
// First produce 10,000 lines, then consume 10,000 lines.
func TestLineBufferSequential(t *testing.T) {
buf := NewLineBuffer()
defer buf.Close()
start := time.Now()
for i := 1; i <= 10000; i++ {
buf.Push(&fswatcher.Line{Line: fmt.Sprintf("This is line number %v.", i)})
}
fmt.Printf("Producer took %v.\n", time.Since(start))
start = time.Now()
for i := 1; i <= 10000; i++ {
line := buf.BlockingPop()
if line.Line != fmt.Sprintf("This is line number %v.", i) {
t.Errorf("Expected 'This is line number %v', but got '%v'.", i, line)
}
}
fmt.Printf("Consumer took %v.\n", time.Since(start))
}
// Produce and consume in parallel.
func TestLineBufferParallel(t *testing.T) {
var (
wg sync.WaitGroup
buf = NewLineBuffer()
)
defer buf.Close()
// producer
go func() {
start := time.Now()
for i := 1; i <= 10000; i++ {
buf.Push(&fswatcher.Line{Line: fmt.Sprintf("This is line number %v.", i)})
if rand.Int()%64 == 0 { // Sleep from time to time
time.Sleep(10 * time.Millisecond)
}
}
fmt.Printf("Producer took %v.\n", time.Since(start))
wg.Done()
}()
// consumer
go func() {
start := time.Now()
for i := 1; i <= 10000; i++ {
line := buf.BlockingPop()
if line.Line != fmt.Sprintf("This is line number %v.", i) {
t.Errorf("Expected 'This is line number %v', but got '%v'.", i, line)
}
if rand.Int()%64 == 0 { // Sleep from time to time
time.Sleep(10 * time.Millisecond)
}
}
fmt.Printf("Consumer took %v.\n", time.Since(start))
wg.Done()
}()
wg.Add(2)
wg.Wait()
}
func TestLineBufferClear(t *testing.T) {
buf := NewLineBuffer()
defer buf.Close()
for linesInBuffer := 0; linesInBuffer < 10; linesInBuffer++ {
for i := 1; i <= linesInBuffer; i++ {
buf.Push(&fswatcher.Line{Line: fmt.Sprintf("This is line number %v of %v.", i, linesInBuffer)})
}
if buf.Len() != linesInBuffer {
t.Fatalf("Expected %v lines in buffer, but got %v", linesInBuffer, buf.Len())
}
buf.Clear()
if buf.Len() != 0 {
t.Fatalf("Expected %v lines in buffer, but got %v", 0, buf.Len())
}
}
}
func TestLineBufferBlockingPop(t *testing.T) {
buf := NewLineBuffer()
done := make(chan struct{})
go func() {
l := buf.BlockingPop()
if l.Line != "hello" {
t.Fatalf("expected to read \"hello\" but got %q.", l.Line)
}
close(done)
}()
select {
case <-done:
t.Fatal("BlockingPop() returned unexpectedly")
case <-time.After(200 * time.Millisecond):
// ok
}
buf.Push(&fswatcher.Line{Line: "hello", File: "/tmp/hello.log"})
select {
case <-done:
// ok
case <-time.After(200 * time.Millisecond):
t.Fatal("BlockingPop() not interrupted by Close()")
}
}
func TestLineBufferClose(t *testing.T) {
buf := NewLineBuffer()
done := make(chan struct{})
go func() {
buf.BlockingPop()
close(done)
}()
select {
case <-done:
t.Fatal("BlockingPop() returned unexpectedly")
case <-time.After(200 * time.Millisecond):
// ok
}
buf.Close() // should interrupt BlockingPop()
select {
case <-done:
// ok
case <-time.After(200 * time.Millisecond):
t.Fatal("BlockingPop() not interrupted by Close()")
}
}