Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export MockBroker in a separate package #312

Closed
eapache opened this issue Mar 4, 2015 · 17 comments
Closed

Export MockBroker in a separate package #312

eapache opened this issue Mar 4, 2015 · 17 comments

Comments

@eapache
Copy link
Contributor

eapache commented Mar 4, 2015

Per #307 (comment) and #307 (comment) this functionality is useful to other people, it just shouldn't live as part of Sarama's API.

I know @wvanbergen had plans to add a bunch of functionality to it, so splitting it out will also make that easier.

Currently the only road-block is it's use of the private encoder type, but that can just be replaced with []byte and the tests can encode the packets ahead of time. This will actually make it more flexible too, by permitting us to test what happens when the broker gives us back garbage packets.

@yagnik this is a nice not-entirely-trivial but danger-free ticket if you're looking to work on your go.

cc @Shopify/kafka @schleppy

@eapache
Copy link
Contributor Author

eapache commented Mar 4, 2015

Related to the last point (DRY up the tests) of #271 since as-is removing the use of encoder will add a bit more duplication to the test suite.

@yagnik
Copy link

yagnik commented Mar 6, 2015

I can try to take a stab at this over the weekend

@eapache
Copy link
Contributor Author

eapache commented Mar 6, 2015

Current open sarama tickets/PRs dealing with the mockbroker that will have to be migrated in some form:

eapache added a commit that referenced this issue Mar 11, 2015
Until #312 lands it in its own package, we should avoid pulling go's testing lib
into the production library. Fixes #334.
@wvanbergen
Copy link
Contributor

I finally have an idea for MockBroker and MockClient.

  1. We implement in a separate package that avoids circular dependencies e.g. sarama/internal/mocks
  2. We expose them to the outside world by simply importing them into sarama/mocks.
package mocks

import internal_mocks "sarama/internal/mocks"
type Consumer internal_mocks.Consumer

Would this work?

@eapache
Copy link
Contributor Author

eapache commented Mar 17, 2015

I believe that would work, 👍 clever

@sybrandy
Copy link

Do you plan on having the MockBroker and MockClient available? I have the 1.0.0 release and I don't see them in there. This is important to me as I have code that I want to test to ensure that I'm properly setting up connections, checking broker health, etc.

@sybrandy
Copy link

FYI: I'm writing tests right now for what I need to test my interfaces and I'm following the pattern described here: Creating Fakes in Go With Channels. So far, it looks very clean and very easy to integrate into tests. I just wanted to pass this along as I think this pattern would make for very nice mocks without too much effort.

@eapache
Copy link
Contributor Author

eapache commented Mar 20, 2015

These are planned, but we keep running into technical roadblocks (mostly around circular imports, which go doesn't allow). We don't want to export these types in the main sarama package obviously, but they depend on types from the main sarama package, which means we can't actually use them in tests by default without causing a circular import. It's a bit of a puzzle.

@wvanbergen
Copy link
Contributor

@sybrandy Can you elaborate a bit on what your application does?

If it is a consumer or producer, I would suggest you use the mock producer or mock consumer interface to test your application, and assume that sarama implements those interfaces correctly.

If you don't want to assume sarama's correctness, the right place to add tests is sarama itself, where you have access to the mock broker. We definitely are interested in more tests around failovers and error handling, so we'd really appreciate any contributions there. I'll gladly help you get started with this if you are interested.

@sybrandy
Copy link

@wvanbergen It is both a producer/consumer, so I do use both. However, I wanted to test logic that I have that requests topic and partition information. I currently cannot do that with sarama, so I faked it myself (See my previous comment). The fact that Client is an interface helped a lot.

What I'm really looking for is a way to simulate interacting with a broker. Whether that be via a mock broker or a mock Client/Broker, I don't care. I ended up with the latter approach and it worked great. What I have fits my needs right now, but someone else may need this as well.

@wvanbergen
Copy link
Contributor

We definitely need to add a mock Client implementation next to the producer and consumer. Any chance that your mock implementation can be contributed?

@wvanbergen
Copy link
Contributor

Also, we should probably add a Topic() and Partitions(topic) method to Consumer, so you don't have to instantiate Client to get this info (see #366)

@sybrandy
Copy link

Unfortunately, no, not at this time. If I get a chance, I'll try to make one. It wasn't hard to do. It's more just finding some free time.

@wvanbergen
Copy link
Contributor

yeah, same here :)

@sybrandy
Copy link

As for adding topic/partition to the consumer, will they still be part of the client? Since I instantiate both in my code, I'd rather instantiate just the client once, then create the producer and consumers from that one client. (This reads from multiple topics. I know, probably an odd use case.)

@wvanbergen
Copy link
Contributor

@sybrandy it would just delegate it to the internal Client instance. If you instantiated your Consumer using NewConsumerFromClient, that would be the same client.

@eapache
Copy link
Contributor Author

eapache commented Feb 21, 2016

Superseded by #570.

@eapache eapache closed this as completed Feb 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants