~ltrager/maas/lp1654432_2.1

« back to all changes in this revision

Viewing changes to src/provisioningserver/tests/test_monkey.py

  • Committer: LaMont Jones
  • Date: 2016-10-12 15:26:17 UTC
  • mfrom: (5469 maas)
  • mto: This revision was merged to the branch mainline in revision 5482.
  • Revision ID: lamont@canonical.com-20161012152617-t14n2jt7y5b7hidb
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
"""Test monkey patches."""
5
5
 
6
 
__all__ = [
7
 
    ]
8
 
 
9
 
 
10
 
import sys
 
6
__all__ = []
 
7
 
11
8
from unittest.mock import sentinel
12
9
 
13
 
from maastesting.testcase import MAASTestCase
14
 
from provisioningserver.monkey import add_patches_to_txtftp
 
10
from maastesting.testcase import (
 
11
    MAASTestCase,
 
12
    MAASTwistedRunTest,
 
13
)
 
14
from provisioningserver.monkey import (
 
15
    add_patches_to_txtftp,
 
16
    augment_twisted_deferToThreadPool,
 
17
)
 
18
from testtools.deferredruntest import assert_fails_with
15
19
import tftp.datagram
16
 
 
17
 
 
18
 
if sys.version_info > (3, 0):
19
 
    import urllib.request as urllib_request
20
 
    import urllib.error as urllib_error
21
 
else:
22
 
    import urllib2 as urllib_request
23
 
    urllib_error = urllib_request
24
 
 
25
 
 
26
 
class TestAddTermErrorCodeToTFT(MAASTestCase):
 
20
from twisted.internet.defer import Deferred
 
21
from twisted.internet.threads import deferToThread
 
22
 
 
23
 
 
24
class TestAddTermErrorCodeToTFTP(MAASTestCase):
27
25
 
28
26
    def test_adds_error_code_8(self):
29
27
        self.patch(tftp.datagram, 'errors', {})
38
36
        add_patches_to_txtftp()
39
37
        self.assertEqual(
40
38
            sentinel.error_8, tftp.datagram.errors.get(8))
 
39
 
 
40
 
 
41
class TestAugmentDeferToThreadPool(MAASTestCase):
 
42
    """Tests for `augment_twisted_deferToThreadPool`."""
 
43
 
 
44
    run_tests_with = MAASTwistedRunTest.make_factory(timeout=5)
 
45
 
 
46
    def setUp(self):
 
47
        super(TestAugmentDeferToThreadPool, self).setUp()
 
48
        augment_twisted_deferToThreadPool()
 
49
 
 
50
    def test_functions_returning_Deferreds_from_threads_crash(self):
 
51
        return assert_fails_with(deferToThread(Deferred), TypeError)
 
52
 
 
53
    def test_functions_returning_other_from_threads_are_okay(self):
 
54
        return deferToThread(round, 12.34).addCallback(self.assertEqual, 12)