Skip to content

Commit 737212e

Browse files
author
Sam Clements
committed
Raise an error when transports are used without connecting (closes #6)
* Move socket related functionality from Transport into a new SocketTransport parent class * Added some simple tests for Transport and SocketTransport
1 parent 504d9fd commit 737212e

2 files changed

Lines changed: 53 additions & 10 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
import py.test
4+
5+
import riemann_client.riemann_pb2
6+
import riemann_client.transport
7+
8+
9+
@py.test.fixture
10+
def tcp_transport():
11+
return riemann_client.transport.TCPTransport()
12+
13+
14+
def test_not_yet_connected(tcp_transport):
15+
with py.test.raises(RuntimeError):
16+
tcp_transport.send(riemann_client.riemann_pb2.Msg())
17+
18+
19+
def test_address_property(tcp_transport):
20+
assert tcp_transport.address == (tcp_transport.host, tcp_transport.port)
21+
22+
23+
def test_transport_enter(string_transport):
24+
assert not hasattr(string_transport, 'string')
25+
with string_transport:
26+
assert hasattr(string_transport, 'string')

riemann_client/transport.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ class RiemannError(Exception):
2424

2525

2626
class Transport(object):
27-
__metaclass__ = abc.ABCMeta
27+
"""Abstract Transport definition"""
2828

29-
def __init__(self, host='localhost', port=5555):
30-
self.host = host
31-
self.port = port
29+
__metaclass__ = abc.ABCMeta
3230

3331
def __enter__(self):
3432
self.connect()
@@ -37,10 +35,6 @@ def __enter__(self):
3735
def __exit__(self, exc_type, exc_value, traceback):
3836
self.disconnect()
3937

40-
@property
41-
def address(self):
42-
return self.host, self.port
43-
4438
@abc.abstractmethod
4539
def connect(self):
4640
pass
@@ -54,7 +48,30 @@ def send(self):
5448
pass
5549

5650

57-
class UDPTransport(Transport):
51+
class SocketTransport(Transport):
52+
"""Provides common functionality for Transports using sockets"""
53+
54+
def __init__(self, host='localhost', port=5555):
55+
self.host = host
56+
self.port = port
57+
58+
@property
59+
def address(self):
60+
return self.host, self.port
61+
62+
@property
63+
def socket(self):
64+
"""Checks that the socket attribute has been created before use"""
65+
if not hasattr(self, '_socket'):
66+
raise RuntimeError("Transport has not been connected!")
67+
return self._socket
68+
69+
@socket.setter
70+
def socket(self, value):
71+
self._socket = value
72+
73+
74+
class UDPTransport(SocketTransport):
5875
def connect(self):
5976
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
6077

@@ -66,7 +83,7 @@ def send(self, message):
6683
return NotImplemented
6784

6885

69-
class TCPTransport(Transport):
86+
class TCPTransport(SocketTransport):
7087
def connect(self):
7188
self.socket = socket.create_connection(self.address)
7289
self.socket.setblocking(True)

0 commit comments

Comments
 (0)