mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Use types.SimpleNamespace where appropriate.
This commit is contained in:
parent
337900037f
commit
882e00f6b5
@ -32,3 +32,18 @@ def requires_mac_ver(*min_version):
|
||||
wrapper.min_version = min_version
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
try:
|
||||
from types import SimpleNamespace
|
||||
except ImportError:
|
||||
class SimpleNamespace(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
keys = sorted(self.__dict__)
|
||||
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
|
||||
return "{}({})".format(type(self).__name__, ", ".join(items))
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__dict__ == other.__dict__
|
||||
|
||||
@ -5,23 +5,21 @@ import t
|
||||
from gunicorn.config import Config
|
||||
from gunicorn.glogging import Logger
|
||||
|
||||
|
||||
class Mock(object):
|
||||
def __init__(self, **kwargs):
|
||||
for attr in kwargs:
|
||||
setattr(self, attr, kwargs[attr])
|
||||
from support import SimpleNamespace
|
||||
|
||||
|
||||
def test_atoms_defaults():
|
||||
response = Mock(status='200', response_length=1024,
|
||||
headers=(('Content-Type', 'application/json'),),
|
||||
sent=1024)
|
||||
request = Mock(headers=(('Accept', 'application/json'), ))
|
||||
environ = {'REQUEST_METHOD': 'GET', 'RAW_URI': 'http://my.uri',
|
||||
'SERVER_PROTOCOL': 'HTTP/1.1'}
|
||||
response = SimpleNamespace(
|
||||
status='200', response_length=1024,
|
||||
headers=(('Content-Type', 'application/json'),), sent=1024,
|
||||
)
|
||||
request = SimpleNamespace(headers=(('Accept', 'application/json'),))
|
||||
environ = {
|
||||
'REQUEST_METHOD': 'GET', 'RAW_URI': 'http://my.uri',
|
||||
'SERVER_PROTOCOL': 'HTTP/1.1',
|
||||
}
|
||||
logger = Logger(Config())
|
||||
atoms = logger.atoms(response, request, environ,
|
||||
datetime.timedelta(seconds=1))
|
||||
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
|
||||
assert isinstance(atoms, dict)
|
||||
assert atoms['r'] == 'GET http://my.uri HTTP/1.1'
|
||||
assert atoms['{accept}i'] == 'application/json'
|
||||
|
||||
@ -16,6 +16,7 @@ import os
|
||||
from gunicorn.config import Config
|
||||
from gunicorn.instrument.statsd import Statsd
|
||||
|
||||
from support import SimpleNamespace
|
||||
|
||||
|
||||
|
||||
@ -50,13 +51,9 @@ class MockSocket(object):
|
||||
server.close()
|
||||
shutil.rmtree(sock_dir)
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.msgs = []
|
||||
|
||||
class MockResponse(object):
|
||||
def __init__(self, status):
|
||||
self.status = status
|
||||
|
||||
def test_statsd_fail():
|
||||
"UDP socket fails"
|
||||
@ -98,7 +95,7 @@ def test_instrument():
|
||||
assert logger.sock.msgs[0] == b"gunicorn.log.critical:1|c|@1.0"
|
||||
logger.sock.reset()
|
||||
|
||||
logger.access(MockResponse("200 OK"), None, {}, timedelta(seconds=7))
|
||||
logger.access(SimpleNamespace(status="200 OK"), None, {}, timedelta(seconds=7))
|
||||
assert logger.sock.msgs[0] == b"gunicorn.request.duration:7000.0|ms"
|
||||
assert logger.sock.msgs[1] == b"gunicorn.requests:1|c|@1.0"
|
||||
assert logger.sock.msgs[2] == b"gunicorn.request.status.200:1|c|@1.0"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user