From 153011ac49ffcbce157f864a3e41b76f0842dddd Mon Sep 17 00:00:00 2001 From: kareza Date: Sun, 30 Jul 2023 15:51:16 +0800 Subject: [PATCH 1/4] Use `_read_response_body` to asyn read the body instead of `readBody`. Signed-off-by: kareza --- tests/test_twisted.py | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/tests/test_twisted.py b/tests/test_twisted.py index 3f72d1b1..d506be4a 100644 --- a/tests/test_twisted.py +++ b/tests/test_twisted.py @@ -3,9 +3,9 @@ from prometheus_client import CollectorRegistry, Counter, generate_latest try: - from twisted.internet import reactor + from twisted.internet import defer, protocol, reactor from twisted.trial.unittest import TestCase - from twisted.web.client import Agent, readBody + from twisted.web.client import Agent from twisted.web.resource import Resource from twisted.web.server import Site @@ -23,24 +23,44 @@ class MetricsResourceTest(TestCase): def setUp(self): self.registry = CollectorRegistry() - def test_reports_metrics(self): - """ - ``MetricsResource`` serves the metrics from the provided registry. - """ - c = Counter('cc', 'A counter', registry=self.registry) - c.inc() + @staticmethod + def _read_response_body(response): + class BodyReaderProtocol(protocol.Protocol): + def __init__(self, finished): + self.finished = finished + self.data = b"" - root = Resource() - root.putChild(b'metrics', MetricsResource(registry=self.registry)) - server = reactor.listenTCP(0, Site(root)) - self.addCleanup(server.stopListening) + def dataReceived(self, data): + self.data += data - agent = Agent(reactor) - port = server.getHost().port - url = f"http://localhost:{port}/metrics" - d = agent.request(b"GET", url.encode("ascii")) + def connectionLost(self, reason): + self.finished.callback(self.data) - d.addCallback(readBody) - d.addCallback(self.assertEqual, generate_latest(self.registry)) + finished = defer.Deferred() + response.deliverBody(BodyReaderProtocol(finished)) + return finished - return d + if HAVE_TWISTED: + @defer.inlineCallbacks + def test_reports_metrics(self): + """ + ``MetricsResource`` serves the metrics from the provided registry. + """ + c = Counter('cc', 'A counter', registry=self.registry) + c.inc() + + root = Resource() + root.putChild(b'metrics', MetricsResource(registry=self.registry)) + server = reactor.listenTCP(0, Site(root)) + self.addCleanup(server.stopListening) + + agent = Agent(reactor) + port = server.getHost().port + url = f"http://localhost:{port}/metrics" + response = yield agent.request(b"GET", url.encode("ascii")) + body = yield self._read_response_body(response) + + self.assertEqual(body, generate_latest(self.registry)) + else: + def test_reports_metrics(self): + pass From 77a5ca6c129a72b5c47a0b5c2f131afda5741add Mon Sep 17 00:00:00 2001 From: kareza Date: Tue, 22 Aug 2023 01:16:16 +0800 Subject: [PATCH 2/4] Revert "Use `_read_response_body` to asyn read the body instead of `readBody`." This reverts commit 78fb5863db5c0f96444f70a161d42261a1fc5308. Signed-off-by: kareza --- tests/test_twisted.py | 58 ++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/tests/test_twisted.py b/tests/test_twisted.py index d506be4a..3f72d1b1 100644 --- a/tests/test_twisted.py +++ b/tests/test_twisted.py @@ -3,9 +3,9 @@ from prometheus_client import CollectorRegistry, Counter, generate_latest try: - from twisted.internet import defer, protocol, reactor + from twisted.internet import reactor from twisted.trial.unittest import TestCase - from twisted.web.client import Agent + from twisted.web.client import Agent, readBody from twisted.web.resource import Resource from twisted.web.server import Site @@ -23,44 +23,24 @@ class MetricsResourceTest(TestCase): def setUp(self): self.registry = CollectorRegistry() - @staticmethod - def _read_response_body(response): - class BodyReaderProtocol(protocol.Protocol): - def __init__(self, finished): - self.finished = finished - self.data = b"" + def test_reports_metrics(self): + """ + ``MetricsResource`` serves the metrics from the provided registry. + """ + c = Counter('cc', 'A counter', registry=self.registry) + c.inc() - def dataReceived(self, data): - self.data += data + root = Resource() + root.putChild(b'metrics', MetricsResource(registry=self.registry)) + server = reactor.listenTCP(0, Site(root)) + self.addCleanup(server.stopListening) - def connectionLost(self, reason): - self.finished.callback(self.data) + agent = Agent(reactor) + port = server.getHost().port + url = f"http://localhost:{port}/metrics" + d = agent.request(b"GET", url.encode("ascii")) - finished = defer.Deferred() - response.deliverBody(BodyReaderProtocol(finished)) - return finished + d.addCallback(readBody) + d.addCallback(self.assertEqual, generate_latest(self.registry)) - if HAVE_TWISTED: - @defer.inlineCallbacks - def test_reports_metrics(self): - """ - ``MetricsResource`` serves the metrics from the provided registry. - """ - c = Counter('cc', 'A counter', registry=self.registry) - c.inc() - - root = Resource() - root.putChild(b'metrics', MetricsResource(registry=self.registry)) - server = reactor.listenTCP(0, Site(root)) - self.addCleanup(server.stopListening) - - agent = Agent(reactor) - port = server.getHost().port - url = f"http://localhost:{port}/metrics" - response = yield agent.request(b"GET", url.encode("ascii")) - body = yield self._read_response_body(response) - - self.assertEqual(body, generate_latest(self.registry)) - else: - def test_reports_metrics(self): - pass + return d From 07430ff5e59e805ffa9d8ba7c93c8acaf13d0982 Mon Sep 17 00:00:00 2001 From: kareza Date: Tue, 22 Aug 2023 01:25:18 +0800 Subject: [PATCH 3/4] ignore DeprecationWarning. Signed-off-by: kareza --- tests/test_twisted.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_twisted.py b/tests/test_twisted.py index 3f72d1b1..02217d1f 100644 --- a/tests/test_twisted.py +++ b/tests/test_twisted.py @@ -3,6 +3,8 @@ from prometheus_client import CollectorRegistry, Counter, generate_latest try: + from warnings import filterwarnings + from twisted.internet import reactor from twisted.trial.unittest import TestCase from twisted.web.client import Agent, readBody @@ -40,6 +42,7 @@ def test_reports_metrics(self): url = f"http://localhost:{port}/metrics" d = agent.request(b"GET", url.encode("ascii")) + filterwarnings('ignore', category=DeprecationWarning) d.addCallback(readBody) d.addCallback(self.assertEqual, generate_latest(self.registry)) From b7fa477ab4fdca33b1920073ea33551efba3bb2f Mon Sep 17 00:00:00 2001 From: kareza Date: Sun, 17 Sep 2023 22:59:33 +0800 Subject: [PATCH 4/4] Add parameter. Signed-off-by: kareza --- tests/test_twisted.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_twisted.py b/tests/test_twisted.py index 02217d1f..e63c903e 100644 --- a/tests/test_twisted.py +++ b/tests/test_twisted.py @@ -42,7 +42,10 @@ def test_reports_metrics(self): url = f"http://localhost:{port}/metrics" d = agent.request(b"GET", url.encode("ascii")) - filterwarnings('ignore', category=DeprecationWarning) + # Ignore expected DeprecationWarning. + filterwarnings("ignore", category=DeprecationWarning, message="Using readBody " + "with a transport that does not have an abortConnection method") + d.addCallback(readBody) d.addCallback(self.assertEqual, generate_latest(self.registry))