Merge pull request #431 from python-escpos/handle-exception-on-network-close

Handle exception on network close
This commit is contained in:
Patrick Kanzler 2020-11-08 22:37:13 +01:00 committed by GitHub
commit dd3c768f13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -242,7 +242,10 @@ class Network(Escpos):
def close(self): def close(self):
""" Close TCP connection """ """ Close TCP connection """
if self.device is not None: if self.device is not None:
try:
self.device.shutdown(socket.SHUT_RDWR) self.device.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
self.device.close() self.device.close()

View File

@ -0,0 +1,23 @@
#!/usr/bin/python
import escpos.printer as printer
import pytest
import mock
import socket
@pytest.fixture
def instance():
socket.socket.connect = mock.Mock()
return printer.Network("localhost")
def test_close_without_open(instance):
"""try to close without opening (should fail gracefully)
Currently we never open from our fixture, so calling close once
should be enough. In the future this might not be enough,
therefore we have to close twice in order to provoke an error
(if possible, this should not raise)
"""
instance.close()
instance.close()