storj/cmd/storj-sim/prefix_test.go
paul cannon 5b9fd4e50a cmd/storj-sim: fix prefix writer infinite loop bug
Fix case where line is larger than maxline and writer ended up in an
infinite loop trying to buffer more data.

Change-Id: I243da738b331279d6bf27255778b5798e7f37f95
2020-09-11 12:33:04 +00:00

103 lines
3.0 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func TestPrefixWriter(t *testing.T) {
root := NewPrefixWriter("", storjSimMaxLineLen, ioutil.Discard)
alpha := root.Prefixed("alpha")
beta := root.Prefixed("beta")
var group errgroup.Group
defer func() {
require.NoError(t, group.Wait())
}()
group.Go(func() error {
_, err := alpha.Write([]byte{1, 2, 3})
return err
})
group.Go(func() error {
_, err := alpha.Write([]byte{3, 2, 1})
return err
})
group.Go(func() error {
_, err := beta.Write([]byte{1, 2, 3})
return err
})
}
func TestPrefixWriterWithLongLine(t *testing.T) {
const (
maxLineLen = 46
inputString = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity,"
prefix = "dickens"
nodeID = "TWO-CITIES"
)
dst := &bytes.Buffer{}
w := NewPrefixWriter(prefix, maxLineLen, dst)
w.nowFunc = func() time.Time { return time.Unix(1599750982, 123456789).UTC() }
_, err := w.Write([]byte("Hi. Node " + nodeID + " started\n"))
require.NoError(t, err)
n, err := w.Write([]byte(inputString))
require.NoError(t, err)
require.Equal(t, len(inputString), n)
// then write the newline separately
n, err = w.Write([]byte("\n"))
require.NoError(t, err)
require.Equal(t, 1, n)
expected := `
dickens TWO-CITIES 15:16:22.123 | Hi. Node TWO-CITIES started
dickens TWO-CITIES 15:16:22.123 | It was the best of times, it was the worst of
| times, it was the age of wisdom, it was the
| age of foolishness, it was the epoch of
dickens TWO-CITIES 15:16:22.123 | belief, it was the epoch of incredulity,
`
require.Equal(t, strings.TrimLeft(expected, "\n"), dst.String())
}
func TestPrefixWriterWithLongLineAndNewline(t *testing.T) {
const (
maxLineLen = 46
inputString = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity,\n"
prefix = "dickens"
nodeID = "TWO-CITIES"
)
dst := &bytes.Buffer{}
w := NewPrefixWriter(prefix, maxLineLen, dst)
w.nowFunc = func() time.Time { return time.Unix(1599750982, 123456789).UTC() }
_, err := w.Write([]byte("Hi. Node " + nodeID + " started\n"))
require.NoError(t, err)
n, err := w.Write([]byte(inputString))
require.NoError(t, err)
require.Equal(t, len(inputString), n)
expected := `
dickens TWO-CITIES 15:16:22.123 | Hi. Node TWO-CITIES started
dickens TWO-CITIES 15:16:22.123 | It was the best of times, it was the worst of
| times, it was the age of wisdom, it was the
| age of foolishness, it was the epoch of
| belief, it was the epoch of incredulity,
`
require.Equal(t, strings.TrimLeft(expected, "\n"), dst.String())
}