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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// Tideland Go Library - Scroller - Unit Tests
//
// Copyright (C) 2014-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package scroller_test
//--------------------
// IMPORTS
//--------------------
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"sync"
"testing"
"time"
"github.com/tideland/golib/audit"
"github.com/tideland/golib/loop"
"github.com/tideland/golib/scroller"
)
//--------------------
// TESTS
//--------------------
// tests contains the data descibing the tests.
var tests = []struct {
description string
initialLeg int
options func() []scroller.Option
initialExpected []int
appendedExpected []int
injector func(*scroller.Scroller, *readSeeker) func([]string)
err string
}{{
description: "no lines existing; initially no lines scrolled",
options: func() []scroller.Option {
return []scroller.Option{
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: []int{},
appendedExpected: intRange(0, 99),
}, {
description: "no lines existing; initially five lines scrolled",
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(5),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: []int{},
appendedExpected: intRange(0, 99),
}, {
description: "ten lines existing; initially no lines scrolled",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: []int{},
appendedExpected: intRange(10, 99),
}, {
description: "ten lines existing; initially five lines scrolled",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(5),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: intRange(5, 9),
appendedExpected: intRange(10, 99),
}, {
description: "ten lines existing; initially twenty lines scrolled",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(20),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: intRange(0, 9),
appendedExpected: intRange(10, 99),
}, {
description: "ten lines existing; initially twenty lines scrolled; buffer smaller than lines",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(20),
scroller.PollTime(2 * time.Millisecond),
scroller.BufferSize(10),
}
},
initialExpected: intRange(0, 9),
appendedExpected: intRange(10, 99),
}, {
description: "ten lines existing; initially three lines scrolled; filter lines with special prefix",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(3),
scroller.Filter(func(line []byte) bool { return bytes.HasPrefix(line, specialPrefix) }),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: []int{3, 5, 8},
appendedExpected: []int{13, 21, 44, 65},
}, {
description: "ten lines existing; initially five lines scrolled; error after further 25 lines",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(5),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: intRange(5, 9),
appendedExpected: intRange(10, 99),
injector: func(s *scroller.Scroller, rs *readSeeker) func([]string) {
return func(lines []string) {
if len(lines) == 25 {
rs.setError("ouch")
}
}
},
err: "ouch",
}, {
description: "ten lines existing; initially five lines scrolled; simply stop after 25 lines",
initialLeg: 10,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(5),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: intRange(5, 9),
appendedExpected: intRange(10, 99),
injector: func(s *scroller.Scroller, rs *readSeeker) func([]string) {
return func(lines []string) {
if len(lines) == 25 {
s.Stop()
}
}
},
}, {
description: "unterminated last line is not scrolled",
initialLeg: 103,
options: func() []scroller.Option {
return []scroller.Option{
scroller.Lines(5),
scroller.PollTime(2 * time.Millisecond),
}
},
initialExpected: intRange(95, 97),
appendedExpected: intRange(98, 99),
}}
// TestScroller runs the different scroller test.
func TestScroller(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
input, output := generateTestData()
for i, test := range tests {
assert.Logf("test #%d/%d: %s", i+1, len(tests), test.description)
rs, sigc := newReadSeeker(input, test.initialLeg)
receiver := newReceiver(assert, output)
// Set options.
options := []scroller.Option{}
if test.options != nil {
options = test.options()
}
// Start scroller.
s, err := scroller.NewScroller(rs, receiver.writer, options...)
assert.Nil(err)
receiver.autoClose(s)
// Prepare injection.
var injection func([]string)
if test.injector != nil {
injection = test.injector(s, rs)
}
// Make assertions.
receiver.assertCollected(test.initialExpected, nil)
sigc <- struct{}{}
receiver.assertCollected(test.appendedExpected, injection)
// Test success or error.
if test.err == "" {
assert.Nil(s.Stop())
} else {
st, err := s.Error()
assert.Equal(st, loop.Stopped)
assert.ErrorMatch(err, test.err)
}
}
}
//--------------------
// TEST HELPERS
//--------------------
// intRange creates a set of ints.
func intRange(lo, hi int) []int {
is := []int{}
for i := lo; i <= hi; i++ {
is = append(is, i)
}
return is
}
var (
regularPrefix = []byte("[REGULAR]")
specialPrefix = []byte("[SPECIAL]")
)
// generateTestData returns slices with input and output data for tests.
func generateTestData() (input, output []string) {
tagged := []int{1, 2, 3, 5, 8, 13, 21, 44, 65}
rand := audit.FixedRand()
gen := audit.NewGenerator(rand)
line := ""
// Generate 98 standard lines.
for i := 0; i < 98; i++ {
switch {
case i%10 == 0:
// Spread some empty lines.
line = "\n"
case len(tagged) > 0 && i == tagged[0]:
// Special prefixed lines.
line = fmt.Sprintf("%s #%d ", specialPrefix, i) + gen.Sentence() + "\n"
tagged = tagged[1:]
default:
// Regular prefixed lines.
line = fmt.Sprintf("%s #%d ", regularPrefix, i) + gen.Sentence() + "\n"
}
input = append(input, line)
output = append(output, line)
}
// Add two longer lines, each time the first half not terminated.
tmp := ""
for i := 0; i < 4; i++ {
if i%2 == 0 {
line = fmt.Sprintf("%s #%d ", regularPrefix, i) +
gen.Sentence() + " " +
gen.Sentence() + " "
tmp = line
} else {
line = gen.Sentence() + " " + gen.Sentence() + "\n"
tmp += line
}
input = append(input, line)
if i%2 != 0 {
output = append(output, tmp)
tmp = ""
}
}
// Add an unterminated line.
line = fmt.Sprintf("%s #%d ", specialPrefix, 100) + gen.Sentence()
input = append(input, line)
return input, output
}
// readSeeker simulates the ReadSeeker in the tests.
type readSeeker struct {
mux sync.Mutex
buffer []byte
pos int
err error
}
// newReadSeeker creates the ReadSeeker with the passed input. The data
// is written with an initial number of lines and then waits for a signal
// to continue.
func newReadSeeker(input []string, initialLeg int) (*readSeeker, chan struct{}) {
sigc := make(chan struct{})
rs := &readSeeker{}
i := 0
for ; i < initialLeg; i++ {
rs.write(input[i])
}
go func() {
<-sigc
for ; i < len(input); i++ {
time.Sleep(5 * time.Millisecond)
rs.write(input[i])
}
}()
return rs, sigc
}
func (rs *readSeeker) write(s string) {
rs.mux.Lock()
defer rs.mux.Unlock()
rs.buffer = append(rs.buffer, []byte(s)...)
}
func (rs *readSeeker) setError(msg string) {
rs.mux.Lock()
defer rs.mux.Unlock()
rs.err = errors.New(msg)
}
func (rs *readSeeker) Read(p []byte) (n int, err error) {
rs.mux.Lock()
defer rs.mux.Unlock()
if rs.err != nil {
return 0, rs.err
}
if rs.pos >= len(rs.buffer) {
return 0, io.EOF
}
n = copy(p, rs.buffer[rs.pos:])
rs.pos += n
return n, nil
}
func (rs *readSeeker) Seek(offset int64, whence int) (ret int64, err error) {
rs.mux.Lock()
defer rs.mux.Unlock()
var newPos int64
switch whence {
case 0:
newPos = offset
case 1:
newPos = int64(rs.pos) + offset
case 2:
newPos = int64(len(rs.buffer)) + offset
default:
return 0, fmt.Errorf("invalid whence: %d", whence)
}
if newPos < 0 {
return 0, fmt.Errorf("negative position: %d", newPos)
}
if newPos >= 1<<31 {
return 0, fmt.Errorf("position out of range: %d", newPos)
}
rs.pos = int(newPos)
return newPos, nil
}
// receiver is responsible for receiving the scrolled lines and
// performing the assertions
type receiver struct {
assert audit.Assertion
data []string
reader *io.PipeReader
writer *io.PipeWriter
linec chan string
}
// newReceiver creates a new receiver.
func newReceiver(assert audit.Assertion, data []string) *receiver {
r := &receiver{
assert: assert,
data: data,
linec: make(chan string),
}
r.reader, r.writer = io.Pipe()
go r.loop()
return r
}
func (r *receiver) autoClose(scroller *scroller.Scroller) {
go func() {
scroller.Wait()
r.writer.Close()
}()
}
func (r *receiver) assertCollected(expected []int, injection func([]string)) {
expectedLines := []string{}
for _, lineNo := range expected {
expectedLines = append(expectedLines, r.data[lineNo])
}
timeout := time.After(2 * time.Second)
lines := []string{}
for {
select {
case line, ok := <-r.linec:
if ok {
lines = append(lines, line)
if injection != nil {
injection(lines)
}
if len(lines) == len(expectedLines) {
// All data received.
r.assert.Equal(lines, expectedLines)
return
}
} else {
// linec closed after stopping or error.
r.assert.Equal(lines, expectedLines[:len(lines)])
return
}
case <-timeout:
if len(expected) == 0 || injection != nil {
return
}
r.assert.Fail("timeout during tailer collection")
return
}
}
}
func (r *receiver) loop() {
defer close(r.linec)
reader := bufio.NewReader(r.reader)
for {
line, err := reader.ReadString('\n')
switch err {
case nil:
r.linec <- line
case io.EOF:
return
default:
r.assert.Fail()
}
}
}
// EOF
|