Azure / azure-event-hubs-go

Golang client library for Azure Event Hubs https://azure.microsoft.com/services/event-hubs
MIT License
88 stars 69 forks source link

create a Hub interface #272

Closed atoulme closed 2 years ago

atoulme commented 2 years ago

Enhancement: create an interface for Hub.

Fixes #271

Environment

ghost commented 2 years ago

CLA assistant check
All CLA requirements met.

richardpark-msft commented 2 years ago

In general, we try not to export types if possible since they become part of our public contract.

In my tests I usually create an interface in my own code to represent 3rd party libraries. It sounds like you're not able to do this, but I'm curious if you could help me get your use case a bit more.

So, for instance, I'd define this interface in my own code (ie, not in eventhubs): https://github.com/Azure/azure-event-hubs-go/pull/272/files#diff-953fe0899f69d47c633cae4a70be7c2838ecc0098165d06066e295f6d019d6e6R60

And then here: https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/12795/files#diff-81400d54e06c8ca9e9898e2be70ab7318917716a47612dd658b5933191a44abfR44

I'd create a function that takes that interface, and pass the result of the constructor function to it.

Closing this for now, but please feel free to reopen/reply.

atoulme commented 1 year ago

The Hub struct implements Receive(ctx context.Context, partitionID string, handler eventhub.Handler, opts ...eventhub.ReceiveOption) (*eventhub.ListenerHandle, error), which returns a ListenerHandle struct which is a struct I cannot mock. ListenerHandle has unexported fields, so I cannot set ctx orr in it. When I try to bind to the lifecycle of the handle, I get a panic since ctx is not set:

// Done will close the channel when the listener has stopped
func (lc *ListenerHandle) Done() <-chan struct{} {
    return lc.ctx.Done()
}

Not sure how to address this, except by building a complete wrapper around your implementation and expanding quite some time doing so.