Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #1026 from tahoe-lafs/3657.util-python-3
Port the rest of allmydata.util to Python 3 Fixes ticket:3657
- Loading branch information
Showing
12 changed files
with
227 additions
and
407 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Tests for allmydata.util.consumer. | ||
Ported to Python 3. | ||
""" | ||
|
||
from __future__ import unicode_literals | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from future.utils import PY2 | ||
if PY2: | ||
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 | ||
|
||
from zope.interface import implementer | ||
from twisted.trial.unittest import TestCase | ||
from twisted.internet.interfaces import IPushProducer, IPullProducer | ||
|
||
from allmydata.util.consumer import MemoryConsumer | ||
|
||
|
||
@implementer(IPushProducer) | ||
@implementer(IPullProducer) | ||
class Producer(object): | ||
"""Can be used as either streaming or non-streaming producer. | ||
If used as streaming, the test should call iterate() manually. | ||
""" | ||
|
||
def __init__(self, consumer, data): | ||
self.data = data | ||
self.consumer = consumer | ||
self.done = False | ||
|
||
def resumeProducing(self): | ||
"""Kick off streaming.""" | ||
self.iterate() | ||
|
||
def iterate(self): | ||
"""Do another iteration of writing.""" | ||
if self.done: | ||
raise RuntimeError( | ||
"There's a bug somewhere, shouldn't iterate after being done" | ||
) | ||
if self.data: | ||
self.consumer.write(self.data.pop(0)) | ||
else: | ||
self.done = True | ||
self.consumer.unregisterProducer() | ||
|
||
|
||
class MemoryConsumerTests(TestCase): | ||
"""Tests for MemoryConsumer.""" | ||
|
||
def test_push_producer(self): | ||
""" | ||
A MemoryConsumer accumulates all data sent by a streaming producer. | ||
""" | ||
consumer = MemoryConsumer() | ||
producer = Producer(consumer, [b"abc", b"def", b"ghi"]) | ||
consumer.registerProducer(producer, True) | ||
self.assertEqual(consumer.chunks, [b"abc"]) | ||
producer.iterate() | ||
producer.iterate() | ||
self.assertEqual(consumer.chunks, [b"abc", b"def", b"ghi"]) | ||
self.assertEqual(consumer.done, False) | ||
producer.iterate() | ||
self.assertEqual(consumer.chunks, [b"abc", b"def", b"ghi"]) | ||
self.assertEqual(consumer.done, True) | ||
|
||
def test_pull_producer(self): | ||
""" | ||
A MemoryConsumer accumulates all data sent by a non-streaming producer. | ||
""" | ||
consumer = MemoryConsumer() | ||
producer = Producer(consumer, [b"abc", b"def", b"ghi"]) | ||
consumer.registerProducer(producer, False) | ||
self.assertEqual(consumer.chunks, [b"abc", b"def", b"ghi"]) | ||
self.assertEqual(consumer.done, True) | ||
|
||
|
||
# download_to_data() is effectively tested by some of the filenode tests, e.g. | ||
# test_immutable.py. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.