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

feat(pubsub): Add pubsub.AddCallbackForTesting() #2920

Closed
wants to merge 2 commits into from

Conversation

Jille
Copy link
Contributor

@Jille Jille commented Sep 25, 2020

Add pubsub.AddCallbackForTesting()

Otherwise crafting a *pubsub.Message panics when Ack() is called.

Otherwise crafting a *pubsub.Message panics when Ack() is called.
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Sep 25, 2020
@Jille Jille changed the title Add pubsub.AddCallbackForTesting() feat(pubsub): Add pubsub.AddCallbackForTesting() Sep 25, 2020
@hongalex
Copy link
Member

Could you clarify what you mean by "crafting a *pubsub.Message panics when Ack() is called"?
Are you using this in conjunction with the pstest fake?

@product-auto-label product-auto-label bot added the api: pubsub Issues related to the Pub/Sub API. label Sep 26, 2020
@Jille
Copy link
Contributor Author

Jille commented Sep 26, 2020

No, I didn't know of the pstest package. My test had:

m := pubsub.Message{Data: d}
handleMyPubsubMessage(ctx, m)

and my code-under-test:

func handleMyPubsubMessage(ctx context.Context, m *pubsub.Message) {
  // ...
  m.Ack()
}

@hongalex
Copy link
Member

hongalex commented Oct 27, 2020

This proposed change replaces the client populated doneFunc (from iterator.go), which is concerning to me. In general, we want to keep the API surface small and simple so as to avoid user confusion. The doneFunc is private for the reason that we don't want users manipulating acknowledge.

I believe a simpler solution is to guard against calling the doneFunc (with if m.doneFunc != nil, so that users can still test with exposed Message object.

@Jille
Copy link
Contributor Author

Jille commented Oct 28, 2020

With if m.doneFunc != nil, how would you propose people test whether their code calls Ack (at the right moment)?

@tbpg
Copy link
Contributor

tbpg commented Oct 28, 2020

Hi. Unfortunately, I'm a -1 on this. There are other ways to achieve this behavior without adding to the API surface. Please check out our new testing guide here: https://github.com/googleapis/google-cloud-go/blob/master/testing.md.

@tbpg tbpg added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 28, 2020
@hongalex
Copy link
Member

hongalex commented Oct 28, 2020

how would you propose people test whether their code calls Ack (at the right moment)?

The interesting thing is that Ack always succeeds. This is true for both the message.Ack function as well as the Acknowledge RPC. More specifically, all message.Ack does is adding the ackID to a queue that eventually calls Acknowledge on a timer. If you really want to test that an Ack is received by the server, you would need to instantiate a fake server as tbpg@ mentioned. This is more of an end-to-end test since you'll have to actually receive a message from the fake server as well.

Jille added a commit to Jille/google-cloud-go that referenced this pull request Nov 4, 2020
This allows for simple testing.

I personally prefer googleapis#2920, but I didn't win that.
@Jille
Copy link
Contributor Author

Jille commented Nov 4, 2020

@tbpg, that seems suitable for end-to-end tests, not for unit tests.

@hongalex, I don't want to test whether the Ack is received. I just want to test whether it's called. That seems like the right scope for a unit test, no?

I sent #3139 to unblock myself and just live crappier testing :(

@hongalex
Copy link
Member

hongalex commented Nov 4, 2020

We're actually looking to expose user defined ack/nack handlers for a message. This was requested recently as part of the design of the pubsub lite library which will share the same pubsub.Message struct. I'm not certain on the timing of this, but I'll approve and merge you current PR in the meantime and update you once the custom ack handler is added in.

Thanks for your patience!

hongalex added a commit that referenced this pull request Nov 6, 2020
This allows for simple testing.

I personally prefer #2920, but I didn't win that.

Co-authored-by: Alex Hong <9397363+hongalex@users.noreply.github.com>
@tbpg
Copy link
Contributor

tbpg commented Nov 10, 2020

One idea is to define an interface with the functions you need, one of which is Ack. Then, while testing, you can use a test implementation of that interface where you can confirm Ack is called. Unfortunately, I still don't think we should extend the exported API for this.

Another idea is to create your own client type that wraps the cloud.google.com/go/pubsub.Client type. Then you can add whatever hooks you need in various functions and you don't need test/prod implementations.

@hongalex
Copy link
Member

Closing in favor of #3200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. cla: yes This human has signed the Contributor License Agreement. do not merge Indicates a pull request not ready for merge, due to either quality or timing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants