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

Howto create anonymous listener for testing #6

Closed
andig opened this issue Dec 11, 2021 · 3 comments
Closed

Howto create anonymous listener for testing #6

andig opened this issue Dec 11, 2021 · 3 comments
Labels
question Further information is requested

Comments

@andig
Copy link

andig commented Dec 11, 2021

For testing I want to use an anonymous server port like in httptest:

tcp := listeners.NewTCP("t1", "")

Doing so works, but I habe no option to retrieve the port as the TCP listener is private.

Alternatively, it would be nice if the listener could be created and passed into the server on the consumer side.

@mochi-co
Copy link
Collaborator

Hi @andig - If you are just using it for testing, you could create a mock listener using the Listener interface https://github.com/mochi-co/mqtt/blob/master/server/listeners/listeners.go#L31

Otherwise you will need to specify a port when creating the TCP listener, for example:

tcp := listeners.NewTCP("t1", ":1883")

@andig
Copy link
Author

andig commented Dec 12, 2021

Thank you for the quick response. I must admit that I've given up and moved to a library that allows to provide your own listener. Imho that would be a preferable pattern if possible. Please feel free to close.

@mochi-co
Copy link
Collaborator

No worries @andig. Note that you can create your own Listeners of any kind using the Listener interface above. Some examples are provided:

A mock listener:
https://github.com/mochi-co/mqtt/blob/master/server/listeners/mock.go

TCP listener:
https://github.com/mochi-co/mqtt/blob/master/server/listeners/tcp.go

Websocket listener:
https://github.com/mochi-co/mqtt/blob/master/server/listeners/websocket.go

@mochi-co mochi-co added the question Further information is requested label Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants