2019-06-19 20:30:48 +01:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
2020-09-10 14:37:16 +01:00
"bytes"
2022-10-11 12:39:08 +01:00
"io"
2020-09-10 14:37:16 +01:00
"strings"
2019-06-19 20:30:48 +01:00
"testing"
2020-09-10 14:37:16 +01:00
"time"
2019-06-19 20:30:48 +01:00
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func TestPrefixWriter ( t * testing . T ) {
2022-10-11 12:39:08 +01:00
root := NewPrefixWriter ( "" , storjSimMaxLineLen , io . Discard )
2019-06-19 20:30:48 +01:00
alpha := root . Prefixed ( "alpha" )
2022-08-02 12:52:24 +01:00
defer func ( ) { _ = alpha . Flush ( ) } ( )
2019-06-19 20:30:48 +01:00
beta := root . Prefixed ( "beta" )
2022-08-02 12:52:24 +01:00
defer func ( ) { _ = beta . Flush ( ) } ( )
2019-06-19 20:30:48 +01:00
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
} )
}
2020-09-10 14:37:16 +01:00
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 ( ) )
}