[stem/master] Finishing basic DescriptorReader implementation

commit 0aba5ba4704acb620915ba5f2115b7d036c9477a Author: Damian Johnson <atagar@torproject.org> Date: Sat Mar 10 22:55:50 2012 -0800 Finishing basic DescriptorReader implementation Fixing the minor issue that was preventing the DescriptorReader from working and making its first integration test do automated verification, rather than needing a manual check. --- stem/descriptor/reader.py | 8 +++++--- test/integ/descriptor/reader.py | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py index 8bdb5b4..0784ce4 100644 --- a/stem/descriptor/reader.py +++ b/stem/descriptor/reader.py @@ -171,6 +171,7 @@ class DescriptorReader(threading.Thread): # TODO: implement # flag that's set when we're done self._stop_event = threading.Event() + self._finished_reading = threading.Event() def get_processed_files(self): """ @@ -266,9 +267,7 @@ class DescriptorReader(threading.Thread): elif target_type[1] == 'bzip2': pass # TODO: implement - # TODO: bug: __iter__ should finish with the _unreturned_descriptors - # contents. Could be fixed by adding a 'is done reading' event. - self._stop_event.set() + self._finished_reading.set() self._iter_notice.set() def __iter__(self): @@ -277,6 +276,9 @@ class DescriptorReader(threading.Thread): try: yield self._unreturned_descriptors.get_nowait() except Queue.Empty: + # if we've finished and there aren't any descriptors then we're done + if self._finished_reading.isSet(): break + self._iter_notice.wait() self._iter_notice.clear() diff --git a/test/integ/descriptor/reader.py b/test/integ/descriptor/reader.py index 4ab2792..ca77e26 100644 --- a/test/integ/descriptor/reader.py +++ b/test/integ/descriptor/reader.py @@ -108,12 +108,30 @@ class TestDescriptorReader(unittest.TestCase): def test_basic_example(self): """ Exercises something similar to the first example in the header - documentation, checking that the contetns match the actual file. + documentation, checking that some of the contents match what we'd expect. """ + # snag some of the plaintext descriptors so we can later make sure that we + # iterate over them + + descriptor_entries = [] + + descriptor_path = os.path.join(DESCRIPTOR_TEST_DATA, "example_descriptor") + with open(descriptor_path) as descriptor_file: + descriptor_entries.append(descriptor_file.read()) + reader = stem.descriptor.reader.DescriptorReader([DESCRIPTOR_TEST_DATA]) with reader: for descriptor in reader: - print descriptor # TODO: change to be an automated check + descriptor_str = str(descriptor) + + if descriptor_str in descriptor_entries: + descriptor_entries.remove(descriptor_str) + else: + # iterator is providing output that we didn't expect + self.fail() + + # check that we've seen all of the descriptor_entries + self.assertTrue(len(descriptor_entries) == 0)
participants (1)
-
atagar@torproject.org