No new public API; use Clock and hamcrest in tests.

This commit is contained in:
Mark Williams 2019-08-09 09:13:40 -07:00
parent 2487b320bb
commit c0def76446
3 changed files with 26 additions and 42 deletions

View File

@ -712,22 +712,6 @@ class TimeoutMixin:
if self.__timeoutCall is not None and self.timeOut is not None:
self.__timeoutCall.reset(self.timeOut)
def cancelTimeout(self):
"""
Cancel the timeout.
If the timeout was already cancelled, this does nothing.
"""
self.timeOut = None
if self.__timeoutCall:
try:
self.__timeoutCall.cancel()
except (error.AlreadyCalled, error.AlreadyCancelled):
pass
self.__timeoutCall = None
def setTimeout(self, period):
"""
Change the timeout period

View File

@ -2956,7 +2956,7 @@ class _GenericHTTPChannelProtocol(proxyForInterface(IProtocol, "_channel")):
self._channel._networkProducer.unregisterProducer()
# Cancel the old channel's timeout.
self._channel.cancelTimeout()
self._channel.setTimeout(None)
transport = self._channel.transport
self._channel = H2Connection()

View File

@ -7,12 +7,13 @@ Test HTTP support.
from __future__ import absolute_import, division
import attr
import base64
import calendar
import cgi
import random
import hamcrest
try:
from urlparse import urlparse, urlunsplit, clear_cache
except ImportError:
@ -780,31 +781,14 @@ class GenericHTTPChannelTests(unittest.TestCase):
When the transport is switched to H2, the HTTPChannel timeouts are
cancelled.
"""
@attr.s
class FakeDelayedCall(object):
period = attr.ib()
callback = attr.ib()
cancelled = attr.ib(default=False)
def cancel(self):
self.cancelled = True
def reset(self, period):
self.period = period
delayedCalls = []
def fakeCallLater(period, callback):
c = FakeDelayedCall(period, callback)
delayedCalls.append(c)
return c
clock = Clock()
a = http._genericHTTPChannelProtocolFactory(b'')
a.requestFactory = DummyHTTPHandlerProxy
# Set the original timeout to be 100s
a.timeOut = 100
a.callLater = fakeCallLater
a.callLater = clock.callLater
b = StringTransport()
b.negotiatedProtocol = b'h2'
@ -813,8 +797,16 @@ class GenericHTTPChannelTests(unittest.TestCase):
# We've made the connection, but we actually check if we've negotiated
# H2 when data arrives. Right now, the HTTPChannel will have set up a
# single delayed call.
self.assertEqual(len(delayedCalls), 1)
self.assertFalse(delayedCalls[0].cancelled)
hamcrest.assert_that(
clock.getDelayedCalls(),
hamcrest.contains(
hamcrest.has_property(
"cancelled",
hamcrest.equal_to(False),
),
),
)
h11Timeout = clock.getDelayedCalls()[0]
# We give it the HTTP data, and it switches out for H2.
a.dataReceived(b'')
@ -822,9 +814,17 @@ class GenericHTTPChannelTests(unittest.TestCase):
# The first delayed call is cancelled, and H2 creates a new one for its
# own timeouts.
self.assertEqual(len(delayedCalls), 2)
self.assertTrue(delayedCalls[0].cancelled)
self.assertFalse(delayedCalls[1].cancelled)
self.assertTrue(h11Timeout.cancelled)
hamcrest.assert_that(
clock.getDelayedCalls(),
hamcrest.contains(
hamcrest.has_property(
"cancelled",
hamcrest.equal_to(False),
),
),
)
if not http.H2_ENABLED:
test_h2CancelsH11Timeout.skip = "HTTP/2 support not present"