Merge pull request #431 from python-escpos/handle-exception-on-network-close
Handle exception on network close
This commit is contained in:
commit
dd3c768f13
|
@ -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()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue