-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from ikalchev/v2.4.2
Release v2.4.2
- Loading branch information
Showing
5 changed files
with
89 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Tests for the HAPServer.""" | ||
from socket import timeout | ||
from unittest.mock import Mock, MagicMock, patch | ||
|
||
import pytest | ||
|
||
from pyhap import hap_server | ||
|
||
|
||
@patch('pyhap.hap_server.HAPServer.server_bind', new=MagicMock()) | ||
@patch('pyhap.hap_server.HAPServer.server_activate', new=MagicMock()) | ||
def test_finish_request_pops_socket(): | ||
"""Test that ``finish_request`` always clears the connection after a request.""" | ||
amock = Mock() | ||
client_addr = ('192.168.1.1', 55555) | ||
server_addr = ('', 51826) | ||
|
||
# Positive case: The request is handled | ||
server = hap_server.HAPServer(server_addr, amock, | ||
handler_type=lambda *args: MagicMock()) | ||
|
||
server.connections[client_addr] = amock | ||
server.finish_request(amock, client_addr) | ||
|
||
assert len(server.connections) == 0 | ||
|
||
# Negative case: The request fails with a timeout | ||
def raises(*args): | ||
raise timeout() | ||
server = hap_server.HAPServer(server_addr, amock, | ||
handler_type=raises) | ||
server.connections[client_addr] = amock | ||
server.finish_request(amock, client_addr) | ||
|
||
assert len(server.connections) == 0 | ||
|
||
# Negative case: The request raises some other exception | ||
server = hap_server.HAPServer(server_addr, amock, | ||
handler_type=lambda *args: 1 / 0) | ||
server.connections[client_addr] = amock | ||
|
||
with pytest.raises(Exception): | ||
server.finish_request(amock, client_addr) | ||
|
||
assert len(server.connections) == 0 |