~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/test/test_app.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
 
5
 
"""
6
 
Test cases for twisted.internet.app.
7
 
"""
8
 
 
9
 
from twisted.trial import unittest, util
10
 
from twisted.internet import app, protocol, error
11
 
from twisted.internet.defer import succeed, fail, SUCCESS, FAILURE
12
 
from twisted.python import log
13
 
import warnings
14
 
 
15
 
class AppTestCase(unittest.TestCase):
16
 
    suppress = [util.suppress(message='twisted.internet.app is deprecated',
17
 
                              category=DeprecationWarning)]
18
 
 
19
 
    def testListenUnlistenTCP(self):
20
 
        a = app.Application("foo")
21
 
        f = protocol.ServerFactory()
22
 
        a.listenTCP(9999, f)
23
 
        a.listenTCP(9998, f)
24
 
        self.assertEquals(len(a.tcpPorts), 2)
25
 
        a.unlistenTCP(9999)
26
 
        self.assertEquals(len(a.tcpPorts), 1)
27
 
        a.listenTCP(9999, f, interface='127.0.0.1')
28
 
        self.assertEquals(len(a.tcpPorts), 2)
29
 
        a.unlistenTCP(9999, '127.0.0.1')
30
 
        self.assertEquals(len(a.tcpPorts), 1)
31
 
        a.unlistenTCP(9998)
32
 
        self.assertEquals(len(a.tcpPorts), 0)
33
 
 
34
 
    def testListenUnlistenUDP(self):
35
 
        a = app.Application("foo")
36
 
        f = protocol.DatagramProtocol()
37
 
        a.listenUDP(9999, f)
38
 
        a.listenUDP(9998, f)
39
 
        self.assertEquals(len(a.udpPorts), 2)
40
 
        a.unlistenUDP(9999)
41
 
        self.assertEquals(len(a.udpPorts), 1)
42
 
        a.listenUDP(9999, f, interface='127.0.0.1')
43
 
        self.assertEquals(len(a.udpPorts), 2)
44
 
        a.unlistenUDP(9999, '127.0.0.1')
45
 
        self.assertEquals(len(a.udpPorts), 1)
46
 
        a.unlistenUDP(9998)
47
 
        self.assertEquals(len(a.udpPorts), 0)
48
 
 
49
 
    def testListenUnlistenUNIX(self):
50
 
        a = app.Application("foo")
51
 
        f = protocol.ServerFactory()
52
 
        a.listenUNIX("xxx", f)
53
 
        self.assertEquals(len(a.unixPorts), 1)
54
 
        a.unlistenUNIX("xxx")
55
 
        self.assertEquals(len(a.unixPorts), 0)
56
 
 
57
 
    def testIllegalUnlistens(self):
58
 
        a = app.Application("foo")
59
 
 
60
 
        self.assertRaises(error.NotListeningError, a.unlistenTCP, 1010)
61
 
        self.assertRaises(error.NotListeningError, a.unlistenUNIX, '1010')
62
 
        self.assertRaises(error.NotListeningError, a.unlistenSSL, 1010)
63
 
        self.assertRaises(error.NotListeningError, a.unlistenUDP, 1010)
64
 
 
65
 
class ServiceTestCase(unittest.TestCase):
66
 
 
67
 
    def testRegisterService(self):
68
 
        a = app.Application("foo")
69
 
        svc = app.ApplicationService("service", a)
70
 
        self.assertEquals(a.getServiceNamed("service"), svc)
71
 
        self.assertEquals(a, svc.serviceParent)
72
 
    testRegisterService.suppress = [util.suppress(message='twisted.internet.app is deprecated',
73
 
                                                  category=DeprecationWarning)]
74
 
 
75
 
class StopError(Exception): pass
76
 
 
77
 
class StoppingService(app.ApplicationService):
78
 
 
79
 
    def __init__(self, name, succeed):
80
 
        app.ApplicationService.__init__(self, name)
81
 
        self.succeed = succeed
82
 
 
83
 
    def stopService(self):
84
 
        if self.succeed:
85
 
            return succeed("yay!")
86
 
        else:
87
 
            return fail(StopError('boo'))
88
 
 
89
 
class StoppingServiceII(app.ApplicationService):
90
 
    def stopService(self):
91
 
        # The default stopService returns None.
92
 
        return None # return app.ApplicationService.stopService(self)
93
 
 
94
 
class MultiServiceTestCase(unittest.TestCase):
95
 
    def setUp(self):
96
 
        self.callbackRan = 0
97
 
 
98
 
    def testDeferredStopService(self):
99
 
        ms = app.MultiService("MultiService")
100
 
        self.s1 = StoppingService("testService", 0)
101
 
        self.s2 = StoppingService("testService2", 1)
102
 
        ms.addService(self.s1)
103
 
        ms.addService(self.s2)
104
 
        ms.stopService().addCallback(self.woohoo)
105
 
        log.flushErrors (StopError)
106
 
 
107
 
    def woohoo(self, res):
108
 
        self.callbackRan = 1
109
 
        self.assertEqual(res[self.s1][0], 0)
110
 
        self.assertEqual(res[self.s2][0], 1)
111
 
 
112
 
    def testStopServiceNone(self):
113
 
        """MultiService.stopService returns Deferred when service returns None.
114
 
        """
115
 
        ms = app.MultiService("MultiService")
116
 
        self.s1 = StoppingServiceII("testService")
117
 
        ms.addService(self.s1)
118
 
        d = ms.stopService()
119
 
        d.addCallback(self.cb_nonetest)
120
 
        log.flushErrors (StopError)
121
 
 
122
 
    def cb_nonetest(self, res):
123
 
        self.callbackRan = 1
124
 
        self.assertEqual((SUCCESS, None), res[self.s1])
125
 
 
126
 
    def testEmptyStopService(self):
127
 
        """MutliService.stopService returns Deferred when empty."""
128
 
        ms = app.MultiService("MultiService")
129
 
        d = ms.stopService()
130
 
        d.addCallback(self.cb_emptytest)
131
 
 
132
 
    def cb_emptytest(self, res):
133
 
        self.callbackRan = 1
134
 
        self.assertEqual(len(res), 0)
135
 
 
136
 
    def tearDown(self):
137
 
        log.flushErrors (StopError)
138
 
        self.failUnless(self.callbackRan, "Callback was never run.")