@ -1,16 +1,21 @@
package event
package event
import (
import (
"bufio"
"bytes"
"encoding/json"
"encoding/json"
"testing"
"testing"
"time"
"time"
"lol.mleku.dev/chk"
"lol.mleku.dev/chk"
"lol.mleku.dev/log"
"lukechampine.com/frand"
"lukechampine.com/frand"
"next.orly.dev/pkg/encoders/event/examples"
"next.orly.dev/pkg/encoders/hex"
"next.orly.dev/pkg/encoders/hex"
"next.orly.dev/pkg/encoders/tag"
"next.orly.dev/pkg/encoders/tag"
"next.orly.dev/pkg/utils"
"next.orly.dev/pkg/utils"
"next.orly.dev/pkg/utils/bufpool"
"next.orly.dev/pkg/utils/bufpool"
"next.orly.dev/pkg/utils/units"
)
)
func TestMarshalJSONUnmarshalJSON ( t * testing . T ) {
func TestMarshalJSONUnmarshalJSON ( t * testing . T ) {
@ -35,13 +40,12 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) {
` )
` )
ev . Sig = frand . Bytes ( 64 )
ev . Sig = frand . Bytes ( 64 )
// log.I.S(ev)
// log.I.S(ev)
b , err := ev . MarshalJSON ( )
// b, err := ev.MarshalJSON()
var err error
var b [ ] byte
if b , err = json . Marshal ( ev ) ; chk . E ( err ) {
if b , err = json . Marshal ( ev ) ; chk . E ( err ) {
t . Fatal ( err )
t . Fatal ( err )
}
}
if err != nil {
t . Fatal ( err )
}
var bc [ ] byte
var bc [ ] byte
bc = append ( bc , b ... )
bc = append ( bc , b ... )
ev2 := New ( )
ev2 := New ( )
@ -49,7 +53,7 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) {
t . Fatal ( err )
t . Fatal ( err )
}
}
var b2 [ ] byte
var b2 [ ] byte
if b2 , err = ev . MarshalJSON ( ) ; err != nil {
if b2 , err = json . Marshal ( ev2 ) ; err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
if ! utils . FastEqual ( bc , b2 ) {
if ! utils . FastEqual ( bc , b2 ) {
@ -65,5 +69,30 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) {
}
}
func TestExamplesCache ( t * testing . T ) {
func TestExamplesCache ( t * testing . T ) {
scanner := bufio . NewScanner ( bytes . NewBuffer ( examples . Cache ) )
scanner . Buffer ( make ( [ ] byte , 0 , 4 * units . Mb ) , 4 * units . Mb )
var err error
for scanner . Scan ( ) {
b := scanner . Bytes ( )
c := bufpool . Get ( )
c = c [ : 0 ]
c = append ( c , b ... )
ev := New ( )
if err = ev . UnmarshalJSON ( b ) ; chk . E ( err ) {
t . Fatal ( err )
}
var b2 [ ] byte
if b2 , err = ev . MarshalJSON ( ) ; err != nil {
t . Fatal ( err )
}
if ! utils . FastEqual ( c , b2 ) {
log . I . F ( "\n%s\n%s" , c , b2 )
t . Fatalf ( "failed to re-marshal back original" )
}
ev . Free ( )
// Don't return scanner.Bytes() to the pool as it's not a buffer we own
// bufpool.PutBytes(b)
bufpool . PutBytes ( b2 )
bufpool . PutBytes ( c )
}
}
}