Skip to content

Commit

Permalink
Refactor out timeout assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa committed Jul 14, 2016
1 parent bae2c55 commit 347b54b
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions twisted/web/test/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,13 @@ def patch_TimeoutMixin_clock(self, connection, reactor):
Unfortunately, TimeoutMixin does not allow passing an explicit reactor
to test timeouts. For that reason, we need to monkeypatch the method
set up by the TimeoutMixin.
@param connection: The HTTP/2 connection object to patch.
@type connection: L{H2Connection}
@param reactor: The reactor whose callLater method we want.
@type reactor: An object implementing
L{twisted.internet.interfaces.IReactorTime}
"""
connection.callLater = reactor.callLater

Expand All @@ -2209,6 +2216,13 @@ def initiateH2Connection(self, initialData, requestFactory):
Performs test setup by building a HTTP/2 connection object, a transport
to back it, a reactor to run in, and sending in some initial data as
needed.
@param initialData: The initial HTTP/2 data to be fed into the
connection after setup.
@type initialData: L{bytes}
@param requestFactory: The L{Request} factory to use with the
connection.
"""
reactor = task.Clock()
conn = H2Connection(reactor)
Expand All @@ -2226,6 +2240,24 @@ def initiateH2Connection(self, initialData, requestFactory):
return (reactor, conn, transport)


def assertTimedOut(self, data, frameCount, errorCode, lastStreamID):
"""
Confirm that the data that was sent matches what we expect from a
timeout: namely, that it ends with a GOAWAY frame carrying an
appropriate error code and last stream ID.
"""
buffer = FrameBuffer()
buffer.receiveData(data)
frames = list(buffer)

self.assertEqual(len(frames), frameCount)
self.assertTrue(
isinstance(frames[-1], hyperframe.frame.GoAwayFrame)
)
self.assertEqual(frames[-1].error_code, errorCode)
self.assertEqual(frames[-1].last_stream_id, lastStreamID)


def test_timeoutAfterInactivity(self):
"""
When a L{H2Connection} does not receive any data for more than the
Expand Down Expand Up @@ -2254,16 +2286,12 @@ def test_timeoutAfterInactivity(self):
# We disconnected after sending a GoAway frame.
self.assertTrue(transport.disconnecting)

buffer = FrameBuffer()
buffer.receiveData(transport.value())
frames = list(buffer)

self.assertEqual(len(frames), 2)
self.assertTrue(
isinstance(frames[-1], hyperframe.frame.GoAwayFrame)
self.assertTimedOut(
transport.value(),
frameCount=2,
errorCode=h2.errors.NO_ERROR,
lastStreamID=0
)
self.assertEqual(frames[-1].error_code, h2.errors.NO_ERROR)
self.assertEqual(frames[-1].last_stream_id, 0)


def test_timeoutResetByData(self):
Expand Down Expand Up @@ -2294,16 +2322,12 @@ def test_timeoutResetByData(self):
# We disconnected after sending a GoAway frame.
self.assertTrue(transport.disconnecting)

buffer = FrameBuffer()
buffer.receiveData(transport.value())
frames = list(buffer)

self.assertEqual(len(frames), 2)
self.assertTrue(
isinstance(frames[-1], hyperframe.frame.GoAwayFrame)
self.assertTimedOut(
transport.value(),
frameCount=2,
errorCode=h2.errors.NO_ERROR,
lastStreamID=0
)
self.assertEqual(frames[-1].error_code, h2.errors.NO_ERROR)
self.assertEqual(frames[-1].last_stream_id, 0)


def test_timeoutWithProtocolErrorIfStreamsOpen(self):
Expand All @@ -2326,16 +2350,12 @@ def test_timeoutWithProtocolErrorIfStreamsOpen(self):
# We disconnected after sending a GoAway frame.
self.assertTrue(transport.disconnecting)

buffer = FrameBuffer()
buffer.receiveData(transport.value())
frames = list(buffer)

self.assertEqual(len(frames), 2)
self.assertTrue(
isinstance(frames[-1], hyperframe.frame.GoAwayFrame)
self.assertTimedOut(
transport.value(),
frameCount=2,
errorCode=h2.errors.PROTOCOL_ERROR,
lastStreamID=1
)
self.assertEqual(frames[-1].error_code, h2.errors.PROTOCOL_ERROR)
self.assertEqual(frames[-1].last_stream_id, 1)


def test_noTimeoutIfConnectionLost(self):
Expand Down

0 comments on commit 347b54b

Please sign in to comment.