How to set up an application for test-ability?

Hi All,

I’m working on an application which utilizes Kafka as a message broker. I want to be able to test the application without having to have a Kafka broker online. How do I inject the Kafka code into my application such that I can replace it with an in-memory solution for my tests?

I have the interface boiled down to:

module type Broker : sig
  type client

  type message

  type ack

  val send_message : client -> string -> message -> ack Lwt.t

  val listen : client -> string array -> ack Lwt.t
end

I’m not even sure what this means…

You’d want the pieces that interact with the broker to be implemented as functors that are then given a broker module. You can then have an in-memory broker or a kafka broker depending on your needs.

Now that I’ve read your answer, I think I understand the original question better. :slight_smile:

Does that mean I have to have 2 separate builds? One which uses the in-memory functor and on which uses the Kafka functor?

Yeah, I’d imagine you could structure it something like this:

lib/app.ml

module type BROKER : sig
  ...
end

module App (Broker : BROKER) = struct
  ...
end

bin/main.ml

module MyApp = MyLib.App(KafkaBroker)
let () = MyApp.start ()

test/main.ml

module MyApp = MyLib.App(InMemoryKafkaBroker)
let test1 () =
  let actual = MyApp.Broker.send_message "123" in
  let expected = ... in
  ...

You might need to play around a bit with organization… but something like this to give a gist.

1 Like

That’s how I’ve gone about it. Thank you.