Home > Blockchain >  How to mock kafka dependencies and writer in Golang?
How to mock kafka dependencies and writer in Golang?

Time:06-26

I have a struct

type Server struct {
    grpcServerPort  int
    grpcServer      *grpc.Server
    writer *kafka.Writer

}

this Server has lot of methods including -

func NewServer(port int) *Server {
    server := &Server{
        grpcServerPort: port,
    }
    gs := grpc.NewServer()
    server.grpcServer = gs

    return server
}


func (s *Server) StartServer() {
    createKafkaTopic("brokker_url", "my_topic")

    s.writer = &kafka.Writer{
        Addr:        kafka.TCP("urls"),
        Topic:       "my_topic",
        Balancer:    &kafka.Hash{},
        MaxAttempts: 1,
        BatchSize:   1,
    }

    listener, err := net.Listen("tcp", fmt.Sprintf(":%d", s.grpcServerPort))
    if err != nil {
        fmt.Printf("failed to listen: %v", err)
    }

    go s.grpcServer.Serve(listener)
}

func (s *Server) produceEvents(key string, val string) error {

    msg := kafka.Message{
        Key:   []byte(key),
        Value: []byte(val),
    }
    err = s.writer.WriteMessages(context.Background(), msg)
    if err != nil {
        return err
    }
    return nil

}

Now createTopic method panic if no kafka broker is up and running at my machine. I want to know how to mock createTopic and produceEvents so that I can write unit test case for other methods of server.

CodePudding user response:

I just met your case last week. And this is how I resolve that:

type KafkaWriter interface {
    Method001fKafkaWriter()
    Method002fKafkaWriter()
}

type Server struct {
    grpcServerPort  int
    grpcServer      *grpc.Server
    writer KafkaWriter
}

Now, you can mock Kafka dependencies with the interface

type mockKafkaWriter struct {
}

func (*mockKafkaWriter) Method001fKafkaWriter() {}
func (*mockKafkaWriter) Method002fKafkaWriter() {}

I hope this can help you!

CodePudding user response:

you can define an interface and then define a mock struct to implement it, now you can test it.

type Server struct {
    grpcServerPort int
    grpcServer     *grpc.Server
    writer         KafkaWriterWrapper
}

type KafkaWriterWrapper interface {
    Write(msg string) error // Suppose kafka.Writer has func Write(msg string) error
}

type KafkaWriterWrapperImpl struct {
    *kafka.Writer
}

type MockKafkaWriterWrapperImpl struct {
}

func (m *MockKafkaWriterWrapperImpl) Write(msg string) error {
    // logic
    return nil
}

  • Related