[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [ooni-probe/master] Write some unittests for NetTest class
commit ac129c6be76d35f6fa4c1ef6ac3d361dd69e1459
Author: aagbsn <aagbsn@xxxxxxxxxxxxxx>
Date: Sat Jan 12 18:23:50 2013 +0100
Write some unittests for NetTest class
---
tests/test_manager.py | 102 +++++++++++++++++++++++++++++++++----------------
1 files changed, 69 insertions(+), 33 deletions(-)
diff --git a/tests/test_manager.py b/tests/test_manager.py
index f5ac052..323aa1f 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1,12 +1,13 @@
+from ooni.measurements import Measurement, NetTest
+from ooni.managers import Director, MeasurementsManager
+from ooni.tasks import BaseTask, TaskWithTimeout
+from os import unlink
from StringIO import StringIO
+from tempfile import TemporaryFile, mkstemp
from twisted.trial import unittest
+from twisted.internet import defer, reactor
-from ooni.manager import Measurement, Measurements
-from ooni.manager import NetTest, OManager
-from ooni.manager import BaseTask, TaskWithTimeout
-
-net_test_file = StringIO()
-net_test_file.write("""
+net_test_string = """
from twisted.python import usage
from ooni.nettest import NetTestCase
@@ -21,9 +22,9 @@ class DummyTestCase(NetTestCase):
def test_b(self):
self.report['foo'] = 'foo'
-""")
+"""
-dummyInputs = range(10)
+dummyInputs = range(1)
dummyOptions = {'spam': 'notham'}
#dummyNetTest = NetTest(net_test_file, inputs, options)
@@ -34,7 +35,7 @@ class DummyMeasurement(BaseTask):
f.write('testing')
f.close()
- return defer.succeed()
+ return defer.succeed(self)
class DummyMeasurementFailOnce(BaseTask):
def run(self):
@@ -47,13 +48,22 @@ class DummyMeasurementFailOnce(BaseTask):
return defer.fail()
class DummyNetTest(NetTest):
+ def __init__(self, num_measurements=1):
+ NetTest.__init__(self, StringIO(net_test_string), dummyInputs, dummyOptions)
+ self.num_measurements = num_measurements
def generateMeasurements(self):
- for i in range(10):
- yield DummyTask()
+ for i in range(self.num_measurements):
+ yield DummyMeasurement()
+
+class DummyDirector(object):
+ def __init__(self):
+ pass
-class DummyManager(object):
+class DummyReporter(object):
def __init__(self):
pass
+ def write(self, result):
+ pass
class TestNetTest(unittest.TestCase):
def test_load_net_test_from_file(self):
@@ -61,32 +71,62 @@ class TestNetTest(unittest.TestCase):
Given a file like object verify that the net test cases are properly
generated.
"""
- net_test = NetTest(net_test_file, dummyInputs, dummyOptions)
- self.assertEqual([(DummyTestCase, 'test_a'), (DummyTestCase,
- 'test_b')], net_test.test_cases)
+ __, net_test_file = mkstemp()
+ with open(net_test_file, 'w') as f:
+ f.write(net_test_string)
+ f.close()
+
+ net_test_from_string = NetTest(StringIO(net_test_string),
+ dummyInputs, dummyOptions, DummyReporter())
+ net_test_from_file = NetTest(net_test_file, dummyInputs,
+ dummyOptions, DummyReporter())
+
+ # XXX: the returned classes are not the same because the
+ # module path is not correct, so the test below fails.
+ # TODO: figure out how to verify that the instantiated
+ # classes are done so properly.
- def test_net_test_timeout(self):
- """Instantiate a test and verify that the timeout works properly when we call it."""
- net_test = NetTest(net_test_file, dummyInputs, dummyOptions)
- # Where net_test_file is a test that will take longer than
+ #self.assertEqual(net_test_from_string.test_cases,
+ # net_test_from_file.test_cases)
+ unlink(net_test_file)
-class TestMeasurementsTracker(unittest.TestCase):
+class TestMeasurementsManager(unittest.TestCase):
def setUp(self):
- self.mock_mt = MeasurementsTracker(DummyManager())
- self.mock_mt.netTests = [DummyNetTest()]
- self.mock_mt.start()
+ self.mock_mm = MeasurementsManager()
+ self.mock_mm.director = DummyDirector()
def test_schedule_measurement(self):
# testing schedule()
# run a single measurement
measurement = DummyMeasurement()
- self.mock_mt.schedule(measurement)
+ self.mock_mm.concurrency = 1
+ self.mock_mm.start()
+ self.mock_mm.schedule(measurement)
+ d = self.mock_mm.run(measurement)
+ def f(x):
+ self.assertEqual(self.mock_mm.completedMeasurements, 1)
+ d.addCallback(f)
+ return d
def test_all_slots_full(self):
"""
Test case where active_measurements is full
"""
- pass
+ self.mock_mm.concurrency = 1
+ self.mock_mm.start()
+
+ dl = []
+ for x in xrange(2):
+ measurement = DummyMeasurement()
+ dl.append(self.mock_mm.schedule(measurement))
+
+ def f(z): print self.mock_mm.completedMeasurements
+
+ assert dl is not None
+ d = defer.DeferredList(dl)
+ assert d is not None
+ d.addCallback(f)
+ return d
def test_populate_active_measurements(self):
"""
@@ -98,7 +138,7 @@ class TestMeasurementsTracker(unittest.TestCase):
"""docstring for test_fail_and_reschedule"""
pass
- def test_fail_timeout_and_reschedule():
+ def test_fail_timeout_and_reschedule(self):
pass
def test_all_completed(self):
@@ -107,17 +147,15 @@ class TestMeasurementsTracker(unittest.TestCase):
"""
pass
- def test_measurements_exhausted():
+ def test_measurements_exhausted(self):
"""
inputs consumed, but still have running measurements
"""
pass
-
-
-class TestManager(unittest.TestCase):
+class TestDirector(unittest.TestCase):
def setUp(self):
- self.manager = OManager
+ self.director = Director()
def test_successful_measurement(self):
pass
@@ -127,5 +165,3 @@ class TestManager(unittest.TestCase):
def test_retry_twice_measurement(self):
pass
-
-
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits