Skip to content

Commit

Permalink
#55 test max_lines_in_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed May 1, 2019
1 parent 6f7d651 commit f6cf484
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
65 changes: 65 additions & 0 deletions exporter/bufferLoadMetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,71 @@ func expectValues(t *testing.T, m *bufferLoadMetric, min15s, min30s, min45s, min
}
}

func TestResetBufferLoadMetrics(t *testing.T) {
m := NewBufferLoadMetric(logrus.New(), false)
c := make(chan time.Time)
tick := &time.Ticker{
C: c,
}
f := make(chan struct{})
m.start(tick, f)
defer m.Stop()
defer close(f)

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 0 | 0 | 0 | 0
// max | 0 | 0 | 0 | 0

m.Inc() // cur = 1
m.Inc() // cur = 2
m.Inc() // cur = 3
m.Inc() // cur = 4
m.Inc() // cur = 5

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 0 | 0 | 0 | 0
// max | 5 | 5 | 5 | 5

synchronousTick(c, f)
synchronousTick(c, f)
synchronousTick(c, f)
synchronousTick(c, f)

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 5 | 5 | 5 | 5
// max | 5 | 5 | 5 | 5

m.Set(7)

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 5 | 5 | 5 | 5
// max | 7 | 7 | 7 | 7

expectValues(t, m, 5, 5, 5, 5, 7, 7, 7, 7)

m.Set(1)

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 1 | 1 | 1 | 1
// max | 7 | 7 | 7 | 7

expectValues(t, m, 1, 1, 1, 1, 7, 7, 7, 7)

synchronousTick(c, f)

// | 15s | 30s | 45s | 60s
// ----------------------------
// min | 1 | 1 | 1 | 1
// max | 1 | 7 | 7 | 7

expectValues(t, m, 1, 1, 1, 1, 1, 7, 7, 7)
}

func synchronousTick(c chan time.Time, f chan struct{}) {
c <- time.Now()
<-f // wait until tick is processed
Expand Down
20 changes: 11 additions & 9 deletions tailer/lineBuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ func TestLineBufferParallel(t *testing.T) {
func TestLineBufferClear(t *testing.T) {
buf := NewLineBuffer()
defer buf.Close()
for i := 1; i <= 7; i++ {
buf.Push(&fswatcher.Line{Line: fmt.Sprintf("This is line number %v.", i)})
}
if buf.Len() != 7 {
t.Fatalf("Expected %v lines in buffer, but got %v", 7, buf.Len())
}
buf.Clear()
if buf.Len() != 0 {
t.Fatalf("Expected %v lines in buffer, but got %v", 0, buf.Len())
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())
}
}
}

Expand Down

0 comments on commit f6cf484

Please sign in to comment.