pstest is great for testing pubsub, makes using queues so much easier.
Could there be an option to simulate a Queue() failure? Maybe something along the lines of:
srv := pstest.NewServer()
...
srv.NextQueueError(errors.New("oops"))
It could either return an error only for the next call to Queue(), or for every call after this.
Hi, thanks for opening this feature request. Could you elaborate further on the Queue feature you would like to implement? I might be missing something, but I'm not sure exactly how it relates to testing pubsub. Thanks!
Hi @hongalex.
sorry, I misnamed the method call. Let me explain.
In normal pubsub usage you do a:
res := topic.Publish(ctx, &pubsub.Message{Data: []byte("payload")})
res is a PublishResult, which you then call res.Get() on to get the result.
Usually the Publish() will be fine, but it's not impossible it fails. Then the res.Get() will return an error, and I have to deal with it.
Currently I can test the topic.Publish() very nicely with pstest, but there is no way to have res.Get() return an error, so I can't test that code path.
Thanks for the clarification. Yeah this seems like a potentially useful feature, though I'm currently prioritizing other items so I won't be able to get to adding this right away. Alternatively, if you're willing to take a look at our CONTRIBUTING.md and feel like adding this feature, I'd be happy to review it for you.
In any case, leaving this issue open until the feature is added.
This is now available as part of pubsub v.1.7.0 with the Reactor options. Specifically, passing in WithErrorInjection into pstest server instantiation will get you what you want. This isn't a queue, but will call your reactor function for every function for a specific RPC. For more info see here.
:confetti_ball:
Thanks for adding this!
You're welcome! To avoid confusion, we recommend using pubsub/v1.8.0 or higher. Unfortunately, we had to make a breaking change between 1.7.0 -> 1.8.0 (specifically WithErrorInjection now takes in an additional grpc Code argument instead of just a message string). In addition, ErrorInjectionReactor is no longer exported.
In any case, if look at the types in https://pkg.go.dev/cloud.google.com/go/[email protected]/pstest, that shows you the types you should be able to use.
Most helpful comment
Hi @hongalex.
sorry, I misnamed the method call. Let me explain.
In normal pubsub usage you do a:
resis a PublishResult, which you then callres.Get()on to get the result.Usually the Publish() will be fine, but it's not impossible it fails. Then the
res.Get()will return an error, and I have to deal with it.Currently I can test the
topic.Publish()very nicely with pstest, but there is no way to haveres.Get()return an error, so I can't test that code path.