broadcast/lines_ring_test.go

66 lines
1.2 KiB
Go
Raw Normal View History

2023-02-13 23:49:45 +01:00
package broadcast
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.sr.ht/~blallo/logz/interface"
"git.sr.ht/~blallo/logz/testlogger"
)
func TestLineRing(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
ctx := context.TODO()
lines, err := NewLineRing(3)
require.NoError(err)
logLevel := logz.LogInfo
if os.Getenv("DEBUG") != "" {
logLevel = logz.LogDebug
}
logger := testlogger.New(t).SetLevel(logLevel)
lines.WithLogger(logger)
go lines.Run(ctx)
defer lines.Close()
testLines := []string{
"test line 1",
"test line 2",
"test line 3",
"test line 4",
"test line 5",
}
pIn := lines.File()
for _, line := range testLines[:3] {
pIn.Write([]byte(line))
pIn.Write([]byte("\n"))
}
assert.Eventually(
func() bool {
return assert.Equal(testLines[:3], lines.Lines())
}, 100*time.Millisecond, time.Millisecond,
)
for _, line := range testLines[3:] {
pIn.Write([]byte(line))
pIn.Write([]byte("\n"))
}
assert.Eventually(
func() bool {
return assert.Equal(append(testLines[2:3], testLines[3:]...), lines.Lines())
}, 100*time.Millisecond, time.Millisecond,
)
}