1 """
2 A special renderer for testish things requring the full server to be up
3 """
4
5
6
7
8
9
10
11 from twisted.internet import reactor
12
13 from nevow import inevow
14 from nevow import rend
15 from nevow import tags as T
16
17 from gavo import base
18 from gavo.svcs import streaming
19 from gavo.web import common
20
21
22 -class FooPage(rend.Page):
23 """is the most basic page conceivable.
24 """
25 docFactory = common.doctypedStan(T.html[
26 T.head[
27 T.title["A page"],
28 ],
29 T.body[
30 T.p["If you see this, you had better know why."]]])
31
32
33 -class StreamerPage(rend.Page):
34 """is a page that delivers senseless but possibly huge streams of
35 data through streaming.py
36 """
37 - def renderHTTP(self, ctx):
38 request = inevow.IRequest(ctx)
39 dataSize = int(request.args.get("size", [300])[0])
40 chunkSize = int(request.args.get("chunksize", [1000])[0])
41 def writeNoise(f):
42 for i in range(dataSize/chunkSize):
43 f.write("x"*chunkSize)
44 lastPayload = "1234567890"
45 toSend = dataSize%chunkSize
46 f.write(lastPayload*(toSend/10)+lastPayload[:toSend%10])
47 return streaming.streamOut(writeNoise, request)
48
49
51 """A page returning some data after some time has passed.
52 """
54 from twisted.internet import task
55 request = inevow.IRequest(ctx)
56 delay = int(request.args.get("seconds", [10])[0])
57 request.setHeader("content-type", "text/plain")
58 return task.deferLater(reactor, delay, lambda: "Hello world\n")
59
60
62 """A page blocking the entire server, including signals, for a while.
63 """
65 import os
66 request = inevow.IRequest(ctx)
67 request.setHeader("content-type", "text/plain")
68 os.system("sleep 20")
69 return "Living again\n"
70
71
72 -class StreamerCrashPage(rend.Page):
73 """is a page that starts streaming out data and then crashes.
74 """
75 - def renderHTTP(self, ctx):
76 request = inevow.IRequest(ctx)
77 request.setHeader("content-type", "text/plain")
78 def writeNoise(f):
79 f.buffer.chunkSize = 30
80 f.write("Here is some data. (and some more, just to cause a flush)\n")
81 raise Exception
82 return streaming.streamOut(writeNoise, request)
83
84
85 -class ExitPage(rend.Page):
86 """A page that lets the server exit (useful for profiling).
87 """
88 - def renderHTTP(self, ctx):
89 request = inevow.IRequest(ctx)
90 request.setHeader("content-type", "text/plain")
91 reactor.callLater(1, lambda: reactor.stop())
92 return "The server is now exiting in 1 second."
93
94
95 -class RenderCrashPage(rend.Page):
96 """A page that crashes during render.
97 """
98 - def render_crash(self, ctx, data):
99 try:
100 raise Exception("Wanton crash")
101 except:
102 import traceback
103 traceback.print_exc()
104 raise
105
106 docFactory = common.doctypedStan(T.html[
107 T.head[
108 T.title["A page"],
109 ],
110 T.body[
111 T.p(render=T.directive("crash"))["You should not see this"]]])
112
113
114 -class BadGatewayPage(rend.Page):
115 """A page that returns a 502 error.
116 """
117 - def renderHTTP(self, ctx):
118 request = inevow.IRequest(ctx)
119 request.setResponseCode(502, message="Bad Gateway")
120 request.setHeader("content-type", "text/plain")
121 return "Bad Gateway"
122
123
124 -class ServiceUnloadPage(rend.Page):
125 """a page that clears the services RD.
126 """
127 - def renderHTTP(self, ctx):
128 request = inevow.IRequest(ctx)
129 request.setHeader("content-type", "text/plain")
130 base.caches.clearForName("__system__/services")
131 return "Cleared the services RD"
132
133
134
151