jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/509132 )
Change subject: [tests] Test whether sseclient.EventSource works as expected ......................................................................
[tests] Test whether sseclient.EventSource works as expected
As found in sseclient 0.0.24 the EventSource gives randomly a ValueError 'Unterminated string' when json.load is processed if the limit is high enough.
Bug: T222885 Change-Id: I082a63180eb26b02bb19b53b12490bd137e322a5 --- M tests/eventstreams_tests.py 1 file changed, 50 insertions(+), 1 deletion(-)
Approvals: Zhuyifei1999: Looks good to me, but someone else must approve D3r1ck01: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/eventstreams_tests.py b/tests/eventstreams_tests.py index 4007e97..5df0a45 100644 --- a/tests/eventstreams_tests.py +++ b/tests/eventstreams_tests.py @@ -7,9 +7,11 @@ # from __future__ import absolute_import, division, unicode_literals
+import json + from tests import mock
-from pywikibot.comms.eventstreams import EventStreams +from pywikibot.comms.eventstreams import EventStreams, EventSource from pywikibot import config from pywikibot.family import WikimediaFamily
@@ -248,6 +250,53 @@ self._test_filter(none_type, all_type, any_type, result)
+class EventStreamsTestClass(EventStreams): + + """Test class of EventStreams.""" + + def __iter__(self): + """Iterator.""" + n = 0 + while self._total is None or n < self._total: + if not hasattr(self, 'source'): + self.source = EventSource(**self.sse_kwargs) + event = next(self.source) + if event.event == 'message': + if not event.data: + continue + n += 1 + try: + element = json.loads(event.data) + except ValueError as e: + self.source.resp.close() # close SSLSocket + del self.source + raise ValueError( + '{error}\n\nEvent no {number}: ' + 'Could not load json data from source\n${event}$' + .format(number=n, event=event, error=e)) + yield element + del self.source + + +class TestEventSource(TestCase): + + """Test sseclient.EventSource.""" + + net = True + + def test_stream(self): + """Verify that the EventSource delivers events without problems. + + As found in sseclient 0.0.24 the EventSource gives randomly a + ValueError 'Unterminated string' when json.load is processed + if the limit is high enough. + """ + limit = 50 + self.es = EventStreamsTestClass(streams='recentchange') + self.es.set_maximum_items(limit) + self.assertLength(list(self.es), limit) + + if __name__ == '__main__': # pragma: no cover try: unittest.main()
pywikibot-commits@lists.wikimedia.org