Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
d7e92149
Commit
d7e92149
authored
May 15, 2010
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update test__server*.py
parent
c666e418
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
105 additions
and
48 deletions
+105
-48
greentest/test__server.py
greentest/test__server.py
+37
-25
greentest/test__server_close.py
greentest/test__server_close.py
+1
-1
greentest/test__server_http.py
greentest/test__server_http.py
+15
-14
greentest/test__server_pywsgi.py
greentest/test__server_pywsgi.py
+38
-4
greentest/test__server_wsgi.py
greentest/test__server_wsgi.py
+14
-4
No files found.
greentest/test__server.py
View file @
d7e92149
...
...
@@ -20,7 +20,7 @@ class SimpleStreamServer(StreamServer):
raise
if
path
==
'/ping'
:
client_socket
.
sendall
(
'HTTP/1.0 200 OK
\
r
\
n
\
r
\
n
PONG'
)
elif
path
==
'/long'
:
elif
path
in
[
'/long'
,
'/short'
]
:
client_socket
.
sendall
(
'hello'
)
while
True
:
data
=
client_socket
.
recv
(
1
)
...
...
@@ -34,13 +34,16 @@ class Settings:
ServerClass
=
StreamServer
ServerSubClass
=
SimpleStreamServer
restartable
=
True
close_socket_detected
=
True
@
staticmethod
def
assert
500
(
self
):
def
assert
AcceptedConnectionError
(
self
):
conn
=
self
.
makefile
()
result
=
conn
.
read
()
assert
not
result
,
repr
(
result
)
assert500
=
assertAcceptedConnectionError
@
staticmethod
def
assert503
(
self
):
# regular reads timeout
...
...
@@ -52,6 +55,10 @@ class Settings:
if
ex
[
0
]
!=
errno
.
ECONNRESET
:
raise
@
staticmethod
def
assertPoolFull
(
self
):
self
.
assertRaises
(
socket
.
timeout
,
self
.
assertRequestSucceeded
,
timeout
=
0.01
)
class
TestCase
(
greentest
.
TestCase
):
...
...
@@ -91,6 +98,12 @@ class TestCase(greentest.TestCase):
def
assert503
(
self
):
Settings
.
assert503
(
self
)
def
assertAcceptedConnectionError
(
self
):
Settings
.
assertAcceptedConnectionError
(
self
)
def
assertPoolFull
(
self
):
Settings
.
assertPoolFull
(
self
)
def
assertNotAccepted
(
self
):
conn
=
self
.
makefile
()
conn
.
write
(
'GET / HTTP/1.0
\
r
\
n
\
r
\
n
'
)
...
...
@@ -107,16 +120,16 @@ class TestCase(greentest.TestCase):
return
assert
result
.
startswith
(
'HTTP/1.0 500 Internal Server Error'
),
repr
(
result
)
def
assert
ConnectionSucceed
(
self
):
conn
=
self
.
makefile
()
def
assert
RequestSucceeded
(
self
,
timeout
=
0.1
):
conn
=
self
.
makefile
(
timeout
=
timeout
)
conn
.
write
(
'GET /ping HTTP/1.0
\
r
\
n
\
r
\
n
'
)
result
=
conn
.
read
()
assert
result
.
endswith
(
'
\
r
\
n
\
r
\
n
PONG'
),
repr
(
result
)
def
start_server
(
self
):
self
.
server
.
start
()
self
.
assert
ConnectionSucce
ed
()
self
.
assert
ConnectionSucce
ed
()
self
.
assert
RequestSucceed
ed
()
self
.
assert
RequestSucceed
ed
()
def
stop_server
(
self
):
self
.
server
.
stop
()
...
...
@@ -138,9 +151,9 @@ class TestCase(greentest.TestCase):
return
self
.
server
.
socket
def
_test_invalid_callback
(
self
):
self
.
hook_stderr
()
self
.
server
=
self
.
ServerClass
((
'127.0.0.1'
,
0
),
lambda
:
None
)
self
.
server
.
start
()
self
.
hook_stderr
()
self
.
assert500
()
self
.
assert_stderr_traceback
(
'TypeError'
)
self
.
assert_stderr
(
self
.
invalid_callback_message
)
...
...
@@ -171,7 +184,7 @@ class TestDefaultSpawn(TestCase):
self
.
assertNotAccepted
()
self
.
server
.
start_accepting
()
self
.
report_netstat
(
'after start_accepting'
)
self
.
assert
ConnectionSucce
ed
()
self
.
assert
RequestSucceed
ed
()
else
:
self
.
assertRaises
(
Exception
,
self
.
server
.
start
)
# XXX which exception exactly?
self
.
stop_server
()
...
...
@@ -203,7 +216,7 @@ class TestDefaultSpawn(TestCase):
g
=
gevent
.
spawn_link_exception
(
self
.
server
.
serve_forever
)
try
:
gevent
.
sleep
(
0.01
)
self
.
assert
ConnectionSucce
ed
()
self
.
assert
RequestSucceed
ed
()
self
.
server
.
stop
()
assert
not
self
.
server
.
started
self
.
assertConnectionRefused
()
...
...
@@ -233,7 +246,8 @@ class TestDefaultSpawn(TestCase):
try
:
try
:
result
=
conn
.
read
()
assert
result
.
startswith
(
'HTTP/1.0 500 Internal Server Error'
),
repr
(
result
)
if
result
:
assert
result
.
startswith
(
'HTTP/1.0 500 Internal Server Error'
),
repr
(
result
)
except
socket
.
error
,
ex
:
if
ex
[
0
]
!=
errno
.
ECONNRESET
:
raise
...
...
@@ -256,10 +270,10 @@ class TestDefaultSpawn(TestCase):
self
.
hook_stderr
()
error
=
ExpectedError
(
'test_error_in_spawn'
)
self
.
server
.
spawn
=
lambda
*
args
:
gevent
.
getcurrent
().
throw
(
error
)
self
.
assert
500
()
self
.
assert
AcceptedConnectionError
()
self
.
assert_stderr_traceback
(
error
)
#self.assert_stderr('^WARNING: <SimpleStreamServer .*?>: ignoring test_error_in_spawn \\(sleeping \d.\d+ seconds\\)\n$')
self
.
assert_stderr
(
'Failed to handle...'
)
self
.
assert_stderr
(
'
<.*?>:
Failed to handle...'
)
return
if
Settings
.
restartable
:
assert
not
self
.
server
.
started
...
...
@@ -282,26 +296,24 @@ class TestPoolSpawn(TestDefaultSpawn):
def
get_spawn
(
self
):
return
2
def
test_p
ul
l_full
(
self
):
def
test_p
oo
l_full
(
self
):
self
.
init_server
()
pool
=
[]
for
_
in
xrange
(
2
):
pool
.
append
(
self
.
send_request
(
'/long'
))
short_request
=
self
.
send_request
(
'/short'
)
long_request
=
self
.
send_request
(
'/long'
)
gevent
.
sleep
(
0.01
)
print
len
(
self
.
server
.
pool
)
self
.
assert
503
()
self
.
assert
503
()
s
elf
.
assert503
()
pool
[
0
].
_sock
.
close
()
pool
[
0
].
close
()
self
.
assertPoolFull
(
)
self
.
assert
PoolFull
()
self
.
assert
PoolFull
()
s
hort_request
.
_sock
.
close
()
# gevent.http and gevent.wsgi cannot detect socket close, so sleep a little
# to let /short request finish
gevent
.
sleep
(
0.1
)
print
len
(
self
.
server
.
pool
)
self
.
assertConnectionSucceed
()
self
.
assertRequestSucceeded
()
class
TestNoneSpawn
(
TestCase
):
invalid_callback_message
=
'Failed to handle'
invalid_callback_message
=
'
<.*?>:
Failed to handle'
def
get_spawn
(
self
):
return
None
...
...
greentest/test__server_close.py
View file @
d7e92149
...
...
@@ -23,7 +23,7 @@ class Test(unittest.TestCase):
except
socket
.
error
,
ex
:
self
.
assertEqual
(
ex
[
0
],
errno
.
ECONNREFUSED
)
def
assert
ConnectionSucce
ed
(
self
):
def
assert
RequestSucceed
ed
(
self
):
conn
=
self
.
makefile
()
conn
.
write
(
'GET /ping HTTP/1.0
\
r
\
n
\
r
\
n
'
)
result
=
conn
.
read
()
...
...
greentest/test__server_http.py
View file @
d7e92149
...
...
@@ -4,13 +4,8 @@ from gevent import http
import
test__server
from
test__server
import
*
internal_error
=
'''HTTP/1.0 500 Internal Server Error
Connection: close
Content-type: text/plain
Content-length: 21
Internal Server Error'''
.
replace
(
'
\
n
'
,
'
\
r
\
n
'
)
internal_error_start
=
'HTTP/1.0 500 Internal Server Error
\
n
'
.
replace
(
'
\
n
'
,
'
\
r
\
n
'
)
internal_error_end
=
'
\
n
\
n
Internal Server Error'
.
replace
(
'
\
n
'
,
'
\
r
\
n
'
)
internal_error503
=
'''HTTP/1.0 503 Service Unavailable
Connection: close
...
...
@@ -24,27 +19,31 @@ class SimpleHTTPServer(http.HTTPServer):
def
handle
(
self
,
request
):
if
request
.
uri
==
'/ping'
:
request
.
send_reply
(
200
,
"OK"
,
"PONG"
)
elif
request
.
uri
==
'/short'
:
gevent
.
sleep
(
0.1
)
request
.
send_reply
(
200
,
"OK"
,
'hello'
)
elif
request
.
uri
==
'/long'
:
request
.
send_reply_start
(
200
,
"OK"
)
request
.
send_reply_chunk
(
"hello"
)
while
request
:
print
request
gevent
.
sleep
(
0.01
)
gevent
.
sleep
(
10
)
request
.
send_reply
(
200
,
"OK"
,
'hello'
)
else
:
request
.
send_reply
(
404
,
"
what?
"
,
""
)
request
.
send_reply
(
404
,
"
gevent.http
"
,
""
)
class
Settings
:
ServerClass
=
http
.
HTTPServer
ServerSubClass
=
SimpleHTTPServer
restartable
=
False
close_socket_detected
=
False
@
staticmethod
def
assert500
(
self
):
conn
=
self
.
makefile
()
conn
.
write
(
'GET / HTTP/1.0
\
r
\
n
\
r
\
n
'
)
result
=
conn
.
read
()
assert
result
==
internal_error
,
(
result
,
internal_error
)
assert
result
.
startswith
(
internal_error_start
),
(
result
,
internal_error_start
)
assert
result
.
endswith
(
internal_error_end
),
(
result
,
internal_error_end
)
assertAcceptedConnectionError
=
assert500
@
staticmethod
def
assert503
(
self
):
...
...
@@ -53,6 +52,8 @@ class Settings:
result
=
conn
.
read
()
assert
result
==
internal_error503
,
(
result
,
internal_error503
)
assertPoolFull
=
assert503
test__server
.
Settings
=
Settings
...
...
greentest/test__server_pywsgi.py
View file @
d7e92149
...
...
@@ -3,15 +3,27 @@ import gevent
from
gevent
import
pywsgi
import
test__server
from
test__server
import
*
from
test__server
import
Settings
as
server_Settings
from
test__server_http
import
Settings
as
http_Settings
import
gevent.wsgi
;
gevent
.
wsgi
.
WSGIServer
.
__init__
=
None
def
application
(
self
,
environ
,
start_response
):
if
environ
[
'PATH_INFO'
]
==
'/'
:
start_response
(
"200 OK"
,
[])
return
[
"PONG"
]
if
environ
[
'PATH_INFO'
]
==
'/ping'
:
start_response
(
"200 OK"
,
[])
return
[
"PONG"
]
elif
environ
[
'PATH_INFO'
]
==
'/short'
:
gevent
.
sleep
(
0.5
)
start_response
(
"200 OK"
,
[])
return
[]
elif
environ
[
'PATH_INFO'
]
==
'/long'
:
gevent
.
sleep
(
10
)
start_response
(
"200 OK"
,
[])
return
[]
else
:
start_response
(
"404 pywsgi WTF?"
,
[])
return
[]
...
...
@@ -24,14 +36,36 @@ class SimpleWSGIServer(pywsgi.WSGIServer):
class
Settings
(
http_Settings
):
ServerClass
=
pywsgi
.
WSGIServer
ServerSubClass
=
SimpleWSGIServer
close_socket_detected
=
True
@
staticmethod
def
assertPoolFull
(
self
):
self
.
assertRaises
(
socket
.
timeout
,
self
.
assertRequestSucceeded
)
@
staticmethod
def
assertAcceptedConnectionError
(
self
):
conn
=
self
.
makefile
()
result
=
conn
.
read
()
assert
not
result
,
repr
(
result
)
# @staticmethod
# def assert500(self):
# conn = self.makefile()
# result = conn.read()
# assert not result, repr(result)
test__server
.
Settings
=
Settings
TestNoneSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestRawSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestPoolSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestDefaultSpawn
.
invalid_callback_message
=
'Failed to handle...'
msg
=
'<.*?>: Failed to handle...'
TestNoneSpawn
.
invalid_callback_message
=
msg
TestRawSpawn
.
invalid_callback_message
=
msg
TestPoolSpawn
.
invalid_callback_message
=
msg
TestDefaultSpawn
.
invalid_callback_message
=
msg
del
TestNoneSpawn
if
__name__
==
'__main__'
:
unittest
.
main
()
...
...
greentest/test__server_wsgi.py
View file @
d7e92149
...
...
@@ -12,6 +12,14 @@ def application(self, environ, start_response):
if
environ
[
'PATH_INFO'
]
==
'/ping'
:
start_response
(
"200 OK"
,
[])
return
[
"PONG"
]
elif
environ
[
'PATH_INFO'
]
==
'/short'
:
gevent
.
sleep
(
0.1
)
start_response
(
"200 after 0.1 seconds"
,
[])
return
[
"hello"
]
elif
environ
[
'PATH_INFO'
]
==
'/long'
:
gevent
.
sleep
(
10
)
start_response
(
"200 after 10 seconds"
,
[])
return
[
"hello"
]
else
:
start_response
(
"404 wsgi WTF?"
,
[])
return
[]
...
...
@@ -28,10 +36,12 @@ class Settings(http_Settings):
test__server
.
Settings
=
Settings
TestNoneSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestRawSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestPoolSpawn
.
invalid_callback_message
=
'Failed to handle...'
TestDefaultSpawn
.
invalid_callback_message
=
'Failed to handle...'
msg
=
'<.*?>: Failed to handle...'
TestNoneSpawn
.
invalid_callback_message
=
msg
TestRawSpawn
.
invalid_callback_message
=
msg
TestPoolSpawn
.
invalid_callback_message
=
msg
TestDefaultSpawn
.
invalid_callback_message
=
msg
if
__name__
==
'__main__'
:
unittest
.
main
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment