OLD | NEW |
| (Empty) |
1 # Copyright (c) 2008 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 """ | |
5 Tests for L{twisted.web.distrib}. | |
6 """ | |
7 | |
8 from twisted.trial import unittest | |
9 from twisted.web import http, distrib, client, resource, static, server | |
10 from twisted.internet import reactor, defer | |
11 from twisted.spread import pb | |
12 from twisted.python import log | |
13 | |
14 class MySite(server.Site): | |
15 def stopFactory(self): | |
16 if hasattr(self, "logFile"): | |
17 if self.logFile != log.logfile: | |
18 self.logFile.close() | |
19 del self.logFile | |
20 | |
21 | |
22 class PBServerFactory(pb.PBServerFactory): | |
23 def buildProtocol(self, addr): | |
24 self.proto = pb.PBServerFactory.buildProtocol(self, addr) | |
25 return self.proto | |
26 | |
27 | |
28 class DistribTest(unittest.TestCase): | |
29 port1 = None | |
30 port2 = None | |
31 sub = None | |
32 | |
33 def tearDown(self): | |
34 dl = [defer.Deferred(), defer.Deferred()] | |
35 self.f1.proto.notifyOnDisconnect(lambda: dl[0].callback(None)) | |
36 if self.sub is not None: | |
37 self.sub.publisher.broker.notifyOnDisconnect( | |
38 lambda: dl[1].callback(None)) | |
39 self.sub.publisher.broker.transport.loseConnection() | |
40 http._logDateTimeStop() | |
41 if self.port1 is not None: | |
42 dl.append(self.port1.stopListening()) | |
43 if self.port2 is not None: | |
44 dl.append(self.port2.stopListening()) | |
45 return defer.gatherResults(dl) | |
46 | |
47 def testDistrib(self): | |
48 # site1 is the publisher | |
49 r1 = resource.Resource() | |
50 r1.putChild("there", static.Data("root", "text/plain")) | |
51 site1 = server.Site(r1) | |
52 self.f1 = PBServerFactory(distrib.ResourcePublisher(site1)) | |
53 self.port1 = reactor.listenTCP(0, self.f1) | |
54 self.sub = distrib.ResourceSubscription("127.0.0.1", | |
55 self.port1.getHost().port) | |
56 r2 = resource.Resource() | |
57 r2.putChild("here", self.sub) | |
58 f2 = MySite(r2) | |
59 self.port2 = reactor.listenTCP(0, f2) | |
60 d = client.getPage("http://127.0.0.1:%d/here/there" % \ | |
61 self.port2.getHost().port) | |
62 d.addCallback(self.failUnlessEqual, 'root') | |
63 return d | |
64 | |
OLD | NEW |