Unit test with GossipPubSub


I am implementing the network module which use GossipPubSub in golang. During the implementation, I found it a little difficult to implement the unit test cases because of the fact that both PubSub and Subscription are solid class instead of an interface. So my questions is, how to properly implement the unit test case with fake PubSub? Is there any examples that I can follow?

Can you use PubSubRouter?

More generally, the design of Go interfaces is motivated by the idea that packages should “own” their interfaces. As such, the the solution to your problem might look like this (assuming your package is called mypkg):

  1. Declare a mypkg.PubSub and mypkg.Subscription interface that is satisfied by pubsub.PubSub and pubsub.Subcription, respectively.
  2. Update all your functions/methods to take your local interfaces as input arguments.
  3. Mock your interfaces during tests.

Does this help?