Add unit test for new component dependency stopping code.

This commit is contained in:
Chase Sterling 2012-12-08 16:18:58 -05:00
parent cc943cea4a
commit 2a0048afbb
1 changed files with 21 additions and 20 deletions

View File

@ -3,8 +3,8 @@ from twisted.internet import threads
import deluge.component as component import deluge.component as component
class testcomponent(component.Component): class testcomponent(component.Component):
def __init__(self, name): def __init__(self, name, depend=None):
component.Component.__init__(self, name) component.Component.__init__(self, name, depend=depend)
self.start_count = 0 self.start_count = 0
self.stop_count = 0 self.stop_count = 0
@ -51,31 +51,35 @@ class testcomponent_shutdown(component.Component):
class ComponentTestClass(unittest.TestCase): class ComponentTestClass(unittest.TestCase):
def tearDown(self): def tearDown(self):
component.stop()
component._ComponentRegistry.components = {} component._ComponentRegistry.components = {}
def test_start_component(self): def test_start_component(self):
def on_start(result, c): def on_start(result, c):
self.assertEquals(c._component_state, "Started") self.assertEquals(c._component_state, "Started")
self.assertEquals(c.start_count, 1) self.assertEquals(c.start_count, 1)
c._component_timer.stop()
c = testcomponent("test_start_c1") c = testcomponent("test_start_c1")
d = component.start(["test_start_c1"]) d = component.start(["test_start_c1"])
d.addCallback(on_start, c) d.addCallback(on_start, c)
return d return d
def test_start_depends(self): def test_start_stop_depends(self):
def on_stop(result, c1, c2):
self.assertEquals(c1._component_state, "Stopped")
self.assertEquals(c2._component_state, "Stopped")
self.assertEquals(c1.stop_count, 1)
self.assertEquals(c2.stop_count, 1)
def on_start(result, c1, c2): def on_start(result, c1, c2):
self.assertEquals(c1._component_state, "Started") self.assertEquals(c1._component_state, "Started")
self.assertEquals(c2._component_state, "Started") self.assertEquals(c2._component_state, "Started")
self.assertEquals(c1.start_count, 1) self.assertEquals(c1.start_count, 1)
self.assertEquals(c2.start_count, 1) self.assertEquals(c2.start_count, 1)
c1._component_timer.stop() return component.stop(["test_start_depends_c1"]).addCallback(on_stop, c1, c2)
c2._component_timer.stop()
c1 = testcomponent("test_start_depends_c1") c1 = testcomponent("test_start_depends_c1")
c2 = testcomponent("test_start_depends_c2") c2 = testcomponent("test_start_depends_c2", depend=["test_start_depends_c1"])
c2._component_depend = ["test_start_depends_c1"]
d = component.start(["test_start_depends_c2"]) d = component.start(["test_start_depends_c2"])
d.addCallback(on_start, c1, c2) d.addCallback(on_start, c1, c2)
@ -83,15 +87,12 @@ class ComponentTestClass(unittest.TestCase):
def start_with_depends(self): def start_with_depends(self):
c1 = testcomponent_delaystart("test_start_all_c1") c1 = testcomponent_delaystart("test_start_all_c1")
c2 = testcomponent("test_start_all_c2") c2 = testcomponent("test_start_all_c2", depend=["test_start_all_c4"])
c3 = testcomponent_delaystart("test_start_all_c3") c3 = testcomponent_delaystart("test_start_all_c3",
c4 = testcomponent("test_start_all_c4") depend=["test_start_all_c5", "test_start_all_c1"])
c4 = testcomponent("test_start_all_c4", depend=["test_start_all_c3"])
c5 = testcomponent("test_start_all_c5") c5 = testcomponent("test_start_all_c5")
c3._component_depend = ["test_start_all_c5", "test_start_all_c1"]
c4._component_depend = ["test_start_all_c3"]
c2._component_depend = ["test_start_all_c4"]
d = component.start() d = component.start()
return (d, c1, c2, c3, c4, c5) return (d, c1, c2, c3, c4, c5)
@ -133,15 +134,15 @@ class ComponentTestClass(unittest.TestCase):
return d return d
def test_stop_all(self): def test_stop_all(self):
def on_stop(*args): def on_stop(result, *args):
for c in args[1:]: for c in args:
self.assertEquals(c._component_state, "Stopped") self.assertEquals(c._component_state, "Stopped")
self.assertEquals(c.stop_count, 1) self.assertEquals(c.stop_count, 1)
def on_start(*args): def on_start(result, *args):
for c in args[1:]: for c in args:
self.assertEquals(c._component_state, "Started") self.assertEquals(c._component_state, "Started")
return component.stop().addCallback(on_stop, *args[1:]) return component.stop().addCallback(on_stop, *args)
ret = self.start_with_depends() ret = self.start_with_depends()
ret[0].addCallback(on_start, *ret[1:]) ret[0].addCallback(on_start, *ret[1:])