14
14
__metaclass__ = type
17
from maasserver import eventloop
17
from crochet import wait_for_reactor
18
from django.db import connections
19
from maasserver import (
18
23
from maasserver.rpc import regionservice
24
from maasserver.testing.eventloop import RegionEventLoopFixture
25
from maasserver.utils.async import transactional
19
26
from maastesting.testcase import MAASTestCase
20
27
from testtools.matchers import IsInstance
21
from twisted.application.service import (
28
from twisted.application.service import MultiService
29
from twisted.python.threadable import isInIOThread
27
32
class TestRegionEventLoop(MAASTestCase):
34
39
def test_start_and_stop(self):
35
40
# Replace the factories in RegionEventLoop with non-functional
36
41
# dummies to avoid bringing up real services here.
37
self.patch(eventloop.loop, "factories", tuple(
38
(name, Service) for name, _ in eventloop.loop.factories))
42
self.useFixture(RegionEventLoopFixture())
39
43
# Reset the services list.
40
44
self.patch(eventloop.loop, "services", MultiService())
41
45
# At the outset, the eventloop's services are dorment.
92
96
eventloop.make_RegionAdvertisingService,
93
97
{factory for _, factory in eventloop.loop.factories})
99
def test_make_NonceCleanupService(self):
100
service = eventloop.make_NonceCleanupService()
101
self.assertThat(service, IsInstance(
102
nonces_cleanup.NonceCleanupService))
103
# It is registered as a factory in RegionEventLoop.
105
eventloop.make_NonceCleanupService,
106
{factory for _, factory in eventloop.loop.factories})
109
class TestDisablingDatabaseConnections(MAASTestCase):
112
def test_connections_are_all_stubs_in_the_event_loop(self):
113
self.assertTrue(isInIOThread())
114
for alias in connections:
115
connection = connections[alias]
116
# isinstance() fails because it references __bases__, so
117
# compare types here.
119
eventloop.DisabledDatabaseConnection,
123
def test_connections_are_all_usable_outside_the_event_loop(self):
124
self.assertFalse(isInIOThread())
125
for alias in connections:
126
connection = connections[alias]
127
self.assertTrue(connection.is_usable())
129
def test_DisabledDatabaseConnection(self):
130
connection = eventloop.DisabledDatabaseConnection()
131
self.assertRaises(RuntimeError, getattr, connection, "connect")
132
self.assertRaises(RuntimeError, getattr, connection, "__call__")
133
self.assertRaises(RuntimeError, setattr, connection, "foo", "bar")
134
self.assertRaises(RuntimeError, delattr, connection, "baz")