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

unit testing tcp connection to the broker #101

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ passlib = "^1.7.0"
docopt = "^0.6.0"
PyYAML = ">=5.4.0,<7.0"
coveralls = {version = "^3.0.1", optional = true}
psutil = "^5.9.0"


[tool.poetry.extras]
Expand Down
38 changes: 38 additions & 0 deletions tests/test_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import asyncio
import logging
from unittest.mock import call, MagicMock
import socket
import psutil

import pytest

Expand Down Expand Up @@ -93,6 +95,42 @@ async def test_client_connect(broker, mock_plugin_manager):
)


@pytest.mark.asyncio
async def test_connect_tcp(broker):
process = psutil.Process()
connections_number = 10
sockets = [
socket.create_connection(("127.0.0.1", 1883)) for _ in range(connections_number)
]
connections = process.connections()
await asyncio.sleep(0.1)

# max number of connections is 10
assert broker._servers["default"].conn_count == connections_number
# Extra connection for the listening Broker
assert len(connections) == connections_number + 1
for conn in connections:
assert conn.status == "ESTABLISHED" or conn.status == "LISTEN"

# close all connections
for s in sockets:
s.close()
connections = process.connections()
for conn in connections:
assert conn.status == "CLOSE_WAIT" or conn.status == "LISTEN"
await asyncio.sleep(0.1)
assert broker._servers["default"].conn_count == 0

# Add one more connection
s = socket.create_connection(("127.0.0.1", 1883))
open_connections = []
for conn in process.connections():
if conn.status == "ESTABLISHED":
open_connections.append(conn)
assert len(open_connections) == 1
assert broker._servers["default"].conn_count == 1


@pytest.mark.asyncio
async def test_client_connect_will_flag(broker):
conn_reader, conn_writer = await asyncio.open_connection("127.0.0.1", 1883)
Expand Down