2018-02-20 09:52:12 +00:00
|
|
|
package diode_test
|
|
|
|
|
|
|
|
import (
|
2018-03-28 18:49:41 +00:00
|
|
|
"bytes"
|
2018-02-20 09:52:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
diodes "code.cloudfoundry.org/go-diodes"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/diode"
|
2018-03-28 18:49:41 +00:00
|
|
|
"github.com/rs/zerolog/internal/cbor"
|
2018-02-20 09:52:12 +00:00
|
|
|
)
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
func TestNewWriter(t *testing.T) {
|
2018-02-20 09:52:12 +00:00
|
|
|
d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
|
|
|
|
fmt.Printf("Dropped %d messages\n", missed)
|
|
|
|
}))
|
2018-03-28 18:49:41 +00:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
w := diode.NewWriter(&buf, d, 10*time.Millisecond)
|
2018-02-20 09:52:12 +00:00
|
|
|
log := zerolog.New(w)
|
|
|
|
log.Print("test")
|
|
|
|
|
|
|
|
w.Close()
|
2018-03-28 18:49:41 +00:00
|
|
|
want := "{\"level\":\"debug\",\"message\":\"test\"}\n"
|
|
|
|
got := cbor.DecodeIfBinaryToString(buf.Bytes())
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Diode New Writer Test failed. got:%s, want:%s!", got, want)
|
|
|
|
}
|
2018-02-20 09:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark(b *testing.B) {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
defer log.SetOutput(os.Stderr)
|
|
|
|
d := diodes.NewManyToOne(100000, nil)
|
|
|
|
w := diode.NewWriter(ioutil.Discard, d, 10*time.Millisecond)
|
|
|
|
log := zerolog.New(w)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
b.SetParallelism(1000)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
log.Print("test")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|