summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames E. Blair <jeblair@hp.com>2014-12-18 13:49:22 -0800
committerJames E. Blair <jeblair@hp.com>2014-12-18 14:04:17 -0800
commit178eae1dcf43db3647d18e7f5fe63aef2c559d72 (patch)
tree425b0f4ae2fa53ff6b886b1e4ec9aac7e4c44651
parent5ba75492f115ac7e747957f2b00e29b6c560c797 (diff)
downloadgear-178eae1dcf43db3647d18e7f5fe63aef2c559d72.tar.gz
Add readPacket unit test
Add a unit test for readPacket as used by ServerConnection. Change-Id: I8117152b2429407b9d41d146facb63fb70a185f7
-rw-r--r--gear/__init__.py12
-rw-r--r--gear/tests/__init__.py39
-rw-r--r--gear/tests/test_gear.py58
3 files changed, 109 insertions, 0 deletions
diff --git a/gear/__init__.py b/gear/__init__.py
index 76b5b16..f27ebc5 100644
--- a/gear/__init__.py
+++ b/gear/__init__.py
@@ -573,6 +573,18 @@ class Packet(object):
extra = ''
return '<gear.Packet 0x%x type: %s%s>' % (id(self), ptype, extra)
+ def __eq__(self, other):
+ if not isinstance(other, Packet):
+ return False
+ if (self.code == other.code and
+ self.ptype == other.ptype and
+ self.data == other.data):
+ return True
+ return False
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
def _formatExtraData(self):
if self.ptype in [constants.JOB_CREATED,
constants.JOB_ASSIGN,
diff --git a/gear/tests/__init__.py b/gear/tests/__init__.py
index a6543f0..dd5fb75 100644
--- a/gear/tests/__init__.py
+++ b/gear/tests/__init__.py
@@ -16,7 +16,9 @@
"""Common utilities used in testing"""
+import errno
import os
+import socket
import fixtures
import testresources
@@ -49,3 +51,40 @@ class BaseTestCase(testtools.TestCase, testresources.ResourcedTestCase):
self.useFixture(fixtures.FakeLogger())
self.useFixture(fixtures.NestedTempfile())
+
+
+def raise_eagain():
+ e = socket.error("socket error [Errno 11] "
+ "Resource temporarily unavailable")
+ e.errno = errno.EAGAIN
+ raise e
+
+
+class FakeSocket(object):
+ def __init__(self):
+ self.packets = []
+ self.packet_num = 0
+ self.packet_pos = 0
+ self.blocking = 1
+
+ def _set_data(self, data):
+ self.packets = data
+
+ def setblocking(self, blocking):
+ self.blocking = blocking
+
+ def recv(self, count):
+ if self.packet_num + 1 > len(self.packets):
+ if self.blocking:
+ raise Exception("End of data reached in blocking mode")
+ raise_eagain()
+ packet = self.packets[self.packet_num]
+ if self.packet_pos + 1 > len(packet):
+ self.packet_num += 1
+ self.packet_pos = 0
+ if not self.blocking:
+ raise_eagain()
+ start = self.packet_pos
+ end = min(start + count, len(packet))
+ self.packet_pos = end
+ return bytes(packet[start:end])
diff --git a/gear/tests/test_gear.py b/gear/tests/test_gear.py
index 31e0d9d..c4d6d62 100644
--- a/gear/tests/test_gear.py
+++ b/gear/tests/test_gear.py
@@ -13,7 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import socket
+
import testscenarios
+import testtools
import gear
from gear import tests
@@ -39,6 +42,61 @@ class ConnectionTestCase(tests.BaseTestCase):
'host: %s port: %s>' % (self.host, self.port)))
+class TestServerConnection(tests.BaseTestCase):
+
+ def setUp(self):
+ super(TestServerConnection, self).setUp()
+ self.socket = tests.FakeSocket()
+ self.conn = gear.ServerConnection('127.0.0.1', self.socket,
+ False, 'test')
+
+ def assertEndOfData(self):
+ # End of data
+ with testtools.ExpectedException(
+ socket.error, ".* Resource temporarily unavailable"):
+ self.conn.readPacket()
+ # Still end of data
+ with testtools.ExpectedException(
+ socket.error, ".* Resource temporarily unavailable"):
+ self.conn.readPacket()
+
+ def test_readPacket_large(self):
+ p1 = gear.Packet(
+ gear.constants.REQ,
+ gear.constants.WORK_COMPLETE,
+ b'H:127.0.0.1:11\x00' + (b'x' * 10000)
+ )
+ self.socket._set_data([p1.toBinary()])
+ r1 = self.conn.readPacket()
+ self.assertEquals(r1, p1)
+ self.assertEndOfData()
+
+ def test_readPacket_multi_pdu(self):
+ p1 = gear.Packet(
+ gear.constants.REQ,
+ gear.constants.WORK_COMPLETE,
+ b'H:127.0.0.1:11\x00' + (b'x' * 2600)
+ )
+ p2 = gear.Packet(
+ gear.constants.REQ,
+ gear.constants.GRAB_JOB_UNIQ,
+ b''
+ )
+ self.socket._set_data([p1.toBinary()[:1448],
+ p1.toBinary()[1448:] + p2.toBinary()])
+ # First half of first packet
+ with testtools.ExpectedException(
+ socket.error, ".* Resource temporarily unavailable"):
+ self.conn.readPacket()
+ # Second half of first packet
+ r1 = self.conn.readPacket()
+ self.assertEquals(r1, p1)
+ # Second packet
+ r2 = self.conn.readPacket()
+ self.assertEquals(r2, p2)
+ self.assertEndOfData()
+
+
class TestClient(tests.BaseTestCase):
def test_handleStatusRes_1(self):