-
-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy paththread_test.py
More file actions
195 lines (158 loc) · 6.15 KB
/
thread_test.py
File metadata and controls
195 lines (158 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Project: MapServer
# Purpose: xUnit style Python mapscript test of multi-threading
# Author: Sean Gillies, sgillies@frii.com
#
# ===========================================================================
# Copyright (c) 2004, Sean Gillies
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ===========================================================================
import os
import threading
import unittest
import mapscript
from .testing import INCOMING, TESTMAPFILE
def draw_map(name, save=0):
# print("making map in thread %s" % (name))
mo = mapscript.mapObj(TESTMAPFILE)
im = mo.draw()
if save:
im.save("threadtest_%s.png" % (name))
def trigger_exception(name):
# print("triggering exception in thread %s" % (name))
mo = mapscript.mapObj(TESTMAPFILE)
try:
mo.setExtent(1, 50, -1, 51)
raise Exception("We expected a MapServer exception")
except mapscript.MapServerError:
pass
class MultipleThreadsTestCase(unittest.TestCase):
def testDrawMultiThreads(self):
"""map drawing with multiple threads"""
workers = []
for i in range(10):
name = "d%d" % (i)
thread = threading.Thread(target=draw_map, name=name, args=(name, 1))
workers.append(thread)
thread.start()
def testExceptionsMultiThreads(self):
"""mapserver exceptions behave with multiple threads"""
workers = []
for i in range(10):
name = "e%d" % (i)
thread = threading.Thread(target=trigger_exception, name=name, args=(name,))
workers.append(thread)
thread.start()
def testExceptionContainmentMultiThreads(self):
"""mapserver exceptions should be contained to a thread"""
num = 100
workers = []
# Trigger an exception in the first started thread
for i in range(0, 1):
name = "c%d" % (i)
thread = threading.Thread(target=trigger_exception, name=name, args=(name,))
workers.append(thread)
# Draw normally
for i in range(1, num):
name = "c%d" % (i)
thread = threading.Thread(target=draw_map, name=name, args=(name,))
workers.append(thread)
# Start all threads
for i in range(num):
workers[i].start()
def draw_map_wfs(name, save=0):
# print("making map in thread %s" % (name))
mo = mapscript.mapObj(TESTMAPFILE)
# WFS layer
lo = mapscript.layerObj()
lo.name = "cheapo_wfs"
lo.setProjection("+init=epsg:4326")
lo.connectiontype = mapscript.MS_WFS
lo.connection = "http://zcologia.com:9001/mapserver/members/features.rpy?"
lo.metadata.set("wfs_service", "WFS")
lo.metadata.set("wfs_typename", "users")
lo.metadata.set("wfs_version", "1.0.0")
lo.type = mapscript.MS_LAYER_POINT
lo.status = mapscript.MS_DEFAULT
lo.labelitem = "zco:mid"
so1 = mapscript.styleObj()
so1.color.setHex("#FFFFFF")
so1.size = 9
so1.symbol = 1 # mo.symbolset.index('circle')
so2 = mapscript.styleObj()
so2.color.setHex("#333333")
so2.size = 7
so2.symbol = 1 # mo.symbolset.index('circle')
co = mapscript.classObj()
co.label.type = mapscript.MS_BITMAP
co.label.size = mapscript.MS_SMALL
co.label.color.setHex("#000000")
co.label.outlinecolor.setHex("#FFFFFF")
co.label.position = mapscript.MS_AUTO
co.insertStyle(so1)
co.insertStyle(so2)
lo.insertClass(co)
mo.insertLayer(lo)
if not mo.web.imagepath:
mo.web.imagepath = os.environ.get("TEMP", None) or INCOMING
mo.debug = mapscript.MS_ON
im = mo.draw()
if save:
im.save("threadtest_wfs_%s.png" % (name))
def draw_map_wms(name, save=0):
# print("making map in thread %s" % (name))
mo = mapscript.mapObj(TESTMAPFILE)
# WMS layer
lo = mapscript.layerObj()
lo.name = "world_latlong"
lo.setProjection("+init=epsg:4326")
lo.connectiontype = mapscript.MS_WMS
lo.connection = "https://demo.mapserver.org/cgi-bin/msautotest?"
lo.metadata.set("wms_service", "WMS")
lo.metadata.set("wms_server_version", "1.3.0")
lo.metadata.set("wms_name", "world_latlong")
lo.metadata.set("wms_format", "image/jpeg")
lo.type = mapscript.MS_LAYER_RASTER
lo.status = mapscript.MS_DEFAULT
lo.debug = mapscript.MS_ON
mo.insertLayer(lo)
if not mo.web.imagepath:
mo.web.imagepath = os.environ.get("TEMP", None) or INCOMING
mo.debug = mapscript.MS_ON
mo.selectOutputFormat("image/jpeg")
im = mo.draw()
if save:
im.save("threadtest_wms_%s.jpg" % (name))
class OWSRequestTestCase(unittest.TestCase):
# def testDrawWFS(self):
# workers = []
# for i in range(10):
# name = 'd%d' % (i)
# thread = threading.Thread(target=draw_map_wfs, name=name, args=(name, 1))
# workers.append(thread)
# thread.start()
def testDrawWMS(self):
workers = []
for i in range(10):
name = "d%d" % (i)
thread = threading.Thread(target=draw_map_wms, name=name, args=(name, 1))
workers.append(thread)
thread.start()
if __name__ == "__main__":
unittest.main()