heetch / felice

Felice is a nascent, opinionated Kafka library for Go, in Go
MIT License
20 stars 1 forks source link

Simple way of creating a message for testing purpose #51

Open asdine opened 4 years ago

asdine commented 4 years ago

It is a bit difficult to create a consumer.Message or producer.Message the first time when testing our handlers. It would be nice if we could have some kind of helper method to do that.

Currently, we need to do something like this:

var m consumer.Message
m.Body = codec.JSONDecoder([]byte(`{"foo":"bar"}`))
m.Headers = make(map[string]string)
m.ID = strconv.Itoa(rand.Intn(1e5))
m.ProducedAt = time.Now().UTC()
key, err := codec.Int64().Encode(i)
require.NoError(t, err)
m.Key = codec.Int64Decoder(key)

err := c.HandleMessage(&m)
require.NoError(t, err)

This is not a lot of lines but I had to browse the source code and deal with a panic (the nil headers) to be able to write them.