fixed unit tests

This commit is contained in:
Yun Xu 2018-02-27 22:25:38 -08:00
parent c39ddd00d3
commit d1a8e8b042
3 changed files with 53 additions and 24 deletions

View File

@ -59,7 +59,9 @@ class SanicTestClient:
@self.app.exception(MethodNotSupported) @self.app.exception(MethodNotSupported)
async def error_handler(request, exception): async def error_handler(request, exception):
if request.method in ['HEAD', 'PATCH', 'PUT', 'DELETE']: if request.method in ['HEAD', 'PATCH', 'PUT', 'DELETE']:
return text('', exception.status_code, headers=exception.headers) return text(
'', exception.status_code, headers=exception.headers
)
else: else:
return self.app.error_handler.default(request, exception) return self.app.error_handler.default(request, exception)

View File

@ -15,17 +15,30 @@ class ReuseableTCPConnector(TCPConnector):
super(ReuseableTCPConnector, self).__init__(*args, **kwargs) super(ReuseableTCPConnector, self).__init__(*args, **kwargs)
self.old_proto = None self.old_proto = None
@asyncio.coroutine if aiohttp.__version__ >= '3.0':
def connect(self, req, traces=None): @asyncio.coroutine
new_conn = yield from super(ReuseableTCPConnector, self)\ def connect(self, req, traces=None):
.connect(req, traces=traces) new_conn = yield from super(ReuseableTCPConnector, self)\
if self.old_proto is not None: .connect(req, traces=traces)
if self.old_proto != new_conn._protocol: if self.old_proto is not None:
raise RuntimeError( if self.old_proto != new_conn._protocol:
"We got a new connection, wanted the same one!") raise RuntimeError(
print(new_conn.__dict__) "We got a new connection, wanted the same one!")
self.old_proto = new_conn._protocol print(new_conn.__dict__)
return new_conn self.old_proto = new_conn._protocol
return new_conn
else:
@asyncio.coroutine
def connect(self, req):
new_conn = yield from super(ReuseableTCPConnector, self)\
.connect(req)
if self.old_proto is not None:
if self.old_proto != new_conn._protocol:
raise RuntimeError(
"We got a new connection, wanted the same one!")
print(new_conn.__dict__)
self.old_proto = new_conn._protocol
return new_conn
class ReuseableSanicTestClient(SanicTestClient): class ReuseableSanicTestClient(SanicTestClient):

View File

@ -80,18 +80,32 @@ class DelayableTCPConnector(TCPConnector):
self._post_connect_delay = _post_connect_delay self._post_connect_delay = _post_connect_delay
self._pre_request_delay = _pre_request_delay self._pre_request_delay = _pre_request_delay
@asyncio.coroutine if aiohttp.__version__ >= '3.0':
def connect(self, req, traces=None): @asyncio.coroutine
d_req = DelayableTCPConnector.\ def connect(self, req, traces=None):
RequestContextManager(req, self._pre_request_delay) d_req = DelayableTCPConnector.\
conn = yield from super(DelayableTCPConnector, self).connect(req, traces=traces) RequestContextManager(req, self._pre_request_delay)
if self._post_connect_delay and self._post_connect_delay > 0: conn = yield from super(DelayableTCPConnector, self).connect(req, traces=traces)
_ = yield from asyncio.sleep(self._post_connect_delay, if self._post_connect_delay and self._post_connect_delay > 0:
loop=self._loop) _ = yield from asyncio.sleep(self._post_connect_delay,
req.send = d_req.send loop=self._loop)
t = req.loop.time() req.send = d_req.send
print("Connected at {}".format(t), flush=True) t = req.loop.time()
return conn print("Connected at {}".format(t), flush=True)
return conn
else:
@asyncio.coroutine
def connect(self, req):
d_req = DelayableTCPConnector.\
RequestContextManager(req, self._pre_request_delay)
conn = yield from super(DelayableTCPConnector, self).connect(req)
if self._post_connect_delay and self._post_connect_delay > 0:
_ = yield from asyncio.sleep(self._post_connect_delay,
loop=self._loop)
req.send = d_req.send
t = req.loop.time()
print("Connected at {}".format(t), flush=True)
return conn
class DelayableSanicTestClient(SanicTestClient): class DelayableSanicTestClient(SanicTestClient):