github.com/quic-go/quic-go@v0.44.0/qlog/event_test.go (about)

     1  package qlog
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"time"
     7  
     8  	"github.com/francoispqt/gojay"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  type mevent struct{}
    15  
    16  var _ eventDetails = mevent{}
    17  
    18  func (mevent) Category() category                   { return categoryConnectivity }
    19  func (mevent) Name() string                         { return "mevent" }
    20  func (mevent) IsNil() bool                          { return false }
    21  func (mevent) MarshalJSONObject(enc *gojay.Encoder) { enc.StringKey("event", "details") }
    22  
    23  var _ = Describe("Events", func() {
    24  	It("marshals the fields before the event details", func() {
    25  		buf := &bytes.Buffer{}
    26  		enc := gojay.NewEncoder(buf)
    27  		Expect(enc.Encode(event{
    28  			RelativeTime: 1337 * time.Microsecond,
    29  			eventDetails: mevent{},
    30  		})).To(Succeed())
    31  
    32  		var decoded interface{}
    33  		Expect(json.Unmarshal(buf.Bytes(), &decoded)).To(Succeed())
    34  		Expect(decoded).To(HaveLen(3))
    35  
    36  		Expect(decoded).To(HaveKeyWithValue("time", 1.337))
    37  		Expect(decoded).To(HaveKeyWithValue("name", "connectivity:mevent"))
    38  		Expect(decoded).To(HaveKey("data"))
    39  		data := decoded.(map[string]interface{})["data"].(map[string]interface{})
    40  		Expect(data).To(HaveLen(1))
    41  		Expect(data).To(HaveKeyWithValue("event", "details"))
    42  	})
    43  })