Debian Codegraph @

gavo/web/webtests.py

raw
1 """
2 A special renderer for testish things requiring the full server to be up
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10 import time
11
12 from twisted.internet import reactor
13 from twisted.web import resource
14 from twisted.web import template
15 from twisted.web.template import tags as T
16
17 from gavo import base
18 from gavo.formal import nevowc
19 from gavo.svcs import streaming
20 from gavo.web import common
21
22
23 class FooPage(resource.Resource):
24 """is the most basic page conceivable.
25 """
26 loader = common.doctypedStan(T.html[
27 T.head[
28 T.title["A page"],
29 ],
30 T.body[
31 T.p["If you see this, you had better know why."]]])
32
33
34 class StreamerPage(resource.Resource):
35 """is a page that delivers senseless but possibly huge streams of
36 data through streaming.py
37 """
38 def render(self, request):
39 dataSize = int(request.strargs.get("size", [300])[0])
40 chunkSize = int(request.strargs.get("chunksize", [1000])[0])
41 def writeNoise(f):
42 for i in range(dataSize//chunkSize):
43 f.write(b"x"*chunkSize)
44 time.sleep(0.01)
45 lastPayload = b"1234567890"
46 toSend = dataSize%chunkSize
47 f.write(lastPayload*(toSend//10)+lastPayload[:toSend%10])
48 return streaming.streamOut(writeNoise, request)
49
50
51 class Delay(resource.Resource):
52 """A page returning some data after some time has passed.
53 """
54 def render(self, request):
55 from twisted.internet import task
56 delay = int(request.strargs.get("seconds", [10])[0])
57 request.setHeader("content-type", "text/plain")
58 return task.deferLater(reactor, delay, lambda: b"Hello world\n")
59
60
61 class Block(resource.Resource):
62 """A page blocking the entire server, including signals, for a while.
63 """
64 def render(self, request):
65 import os
66 request.setHeader("content-type", "text/plain")
67 os.system("sleep 20")
68 return b"Living again\n"
69
70
71 class StreamerCrashPage(resource.Resource):
72 """is a page that starts streaming out data and then crashes.
73 """
74 def render(self, request):
75 request.setHeader("content-type", "text/plain")
76 def writeNoise(f):
77 f.buffer.chunkSize = 30
78 f.write("Here is some data. (and some more, just to cause a flush)\n")
79 raise Exception
80 return streaming.streamOut(writeNoise, request)
81
82
83 class ExitPage(resource.Resource):
84 """A page that lets the server exit (useful for profiling).
85 """
86 def render(self, request):
87 request.setHeader("content-type", "text/plain")
88 reactor.callLater(1, lambda: reactor.stop())
89 return b"The server will exit in 1 second."
90
91
92 class RenderCrashPage(resource.Resource):
93 """A page that crashes during render.
94 """
95 @template.renderer
96 def crash(self, ctx, data):
97 try:
98 raise Exception("Wanton crash")
99 except:
100 import traceback
101 traceback.print_exc()
102 raise
103
104 loader = common.doctypedStan(T.html[
105 T.head[
106 T.title["A page"],
107 ],
108 T.body[
109 T.p(render="crash")["You should not see this"]]])
110
111
112 class BadGatewayPage(resource.Resource):
113 """A page that returns a 502 error.
114 """
115 def render(self, request):
116 request.setResponseCode(502, message="Bad Gateway")
117 request.setHeader("content-type", "text/plain")
118 return b"Bad Gateway"
119
120
121 class ServiceUnloadPage(resource.Resource):
122 """a page that clears the services RD.
123 """
124 def render(self, request):
125 request.setHeader("content-type", "text/plain")
126 base.caches.clearForName("__system__/services")
127 return b"Cleared the services RD"
128
129
130 class _UnrenderableException(Exception):
131 def __str__(self):
132 ddt #noflake: intended f'up.
133
134
135 class BlowUpErrorPage(resource.Resource):
136 """a page that produces an unrenderable error when rendering.
137 """
138 def render_GET(self, request):
139 raise _UnrenderableException()
140
141
142 class Tests(nevowc.TemplatedPage):
143 def __init__(self):
144 resource.Resource.__init__(self)
145 self.putChild(b"foo", FooPage())
146 self.putChild(b"stream", StreamerPage())
147 self.putChild(b"streamcrash", StreamerCrashPage())
148 self.putChild(b"rendercrash", RenderCrashPage())
149 self.putChild(b"badgateway", BadGatewayPage())
150 self.putChild(b"block", Block())
151 self.putChild(b"exit", ExitPage())
152 self.putChild(b"clearservice", ServiceUnloadPage())
153 self.putChild(b"delay", Delay())
154 self.putChild(b"blowuperror", BlowUpErrorPage())
155
156 loader = common.doctypedStan(T.html[
157 T.head[
158 T.title["Wrong way"],
159 ],
160 T.body[
161 T.p["There is nothing here. Trust me."]]])