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
a51e0937
Commit
a51e0937
authored
Jun 04, 2020
by
Jason Madden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More selector tests
parent
8d001495
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
37 deletions
+99
-37
src/gevent/tests/test__monkey_selectors.py
src/gevent/tests/test__monkey_selectors.py
+3
-36
src/gevent/tests/test__selectors.py
src/gevent/tests/test__selectors.py
+95
-0
src/gevent/tests/test__threadpool.py
src/gevent/tests/test__threadpool.py
+1
-1
No files found.
src/gevent/tests/test__monkey_selectors.py
View file @
a51e0937
...
...
@@ -5,9 +5,6 @@ try:
import
selectors
except
ImportError
:
import
selectors2
as
selectors
import
socket
import
gevent
from
gevent.monkey
import
patch_all
import
gevent.testing
as
greentest
...
...
@@ -16,9 +13,9 @@ patch_all()
from
gevent.selectors
import
DefaultSelector
from
gevent.selectors
import
GeventSelector
from
gevent.tests.test__selectors
import
SelectorTestMixin
class
TestSelectors
(
greentest
.
TestCase
):
class
TestSelectors
(
SelectorTestMixin
,
greentest
.
TestCase
):
@
greentest
.
skipOnPy2
(
'selectors2 backport does not use _select'
...
...
@@ -39,40 +36,10 @@ class TestSelectors(greentest.TestCase):
self
.
assertIs
(
selectors
.
DefaultSelector
,
GeventSelector
)
def
test_import_selectors
(
self
):
# selectors can always be imported. On Python 2,
# selectors can always be imported
once monkey-patched
. On Python 2,
# this is an alias for gevent.selectors.
__import__
(
'selectors'
)
def
_check_selector
(
self
,
sel
):
def
read
(
conn
,
_mask
):
data
=
conn
.
recv
(
100
)
# Should be ready
if
data
:
conn
.
send
(
data
)
# Hope it won't block
sel
.
unregister
(
conn
)
conn
.
close
()
def
run_selector_once
():
events
=
sel
.
select
()
for
key
,
mask
in
events
:
key
.
data
(
key
.
fileobj
,
mask
)
sock1
,
sock2
=
socket
.
socketpair
()
try
:
sel
.
register
(
sock1
,
selectors
.
EVENT_READ
,
read
)
glet
=
gevent
.
spawn
(
run_selector_once
)
DATA
=
b'abcdef'
sock2
.
send
(
DATA
)
data
=
sock2
.
recv
(
50
)
self
.
assertEqual
(
data
,
DATA
)
finally
:
sel
.
close
()
sock1
.
close
()
sock2
.
close
()
glet
.
join
(
10
)
self
.
assertTrue
(
glet
.
ready
())
def
_make_test
(
name
,
kind
):
# pylint:disable=no-self-argument
if
kind
is
None
:
def
m
(
self
):
...
...
src/gevent/tests/test__selectors.py
0 → 100644
View file @
a51e0937
# Tests for gevent.selectors in its native form, without
# monkey-patching.
import
gevent
from
gevent
import
socket
from
gevent
import
selectors
import
gevent.testing
as
greentest
class
SelectorTestMixin
(
object
):
@
staticmethod
def
run_selector_once
(
sel
):
# Run in a background greenlet, leaving the main
# greenlet free to send data.
events
=
sel
.
select
(
timeout
=
3
)
for
key
,
mask
in
events
:
key
.
data
(
sel
,
key
.
fileobj
,
mask
)
gevent
.
sleep
()
unregister_after_send
=
True
def
read_from_ready_socket_and_reply
(
self
,
selector
,
conn
,
_events
):
data
=
conn
.
recv
(
100
)
# Should be ready
if
data
:
conn
.
send
(
data
)
# Hope it won't block
# Must unregister before we close.
if
self
.
unregister_after_send
:
selector
.
unregister
(
conn
)
conn
.
close
()
def
_check_selector
(
self
,
sel
):
server
,
client
=
socket
.
socketpair
()
try
:
sel
.
register
(
server
,
selectors
.
EVENT_READ
,
self
.
read_from_ready_socket_and_reply
)
glet
=
gevent
.
spawn
(
self
.
run_selector_once
,
sel
)
DATA
=
b'abcdef'
client
.
send
(
DATA
)
data
=
client
.
recv
(
50
)
# here is probably where we yield to the event loop
self
.
assertEqual
(
data
,
DATA
)
finally
:
sel
.
close
()
server
.
close
()
client
.
close
()
glet
.
join
(
10
)
self
.
assertTrue
(
glet
.
ready
())
class
GeventSelectorTest
(
SelectorTestMixin
,
greentest
.
TestCase
):
def
test_select_using_socketpair
(
self
):
# Basic test.
with
selectors
.
GeventSelector
()
as
sel
:
self
.
_check_selector
(
sel
)
def
test_select_many_sockets
(
self
):
pairs
=
[
socket
.
socketpair
()
for
_
in
range
(
10
)]
clients
=
[
s
[
1
]
for
s
in
pairs
]
try
:
server_sel
=
selectors
.
GeventSelector
()
client_sel
=
selectors
.
GeventSelector
()
for
i
,
pair
in
enumerate
(
pairs
):
server
,
client
=
pair
server_sel
.
register
(
server
,
selectors
.
EVENT_READ
,
self
.
read_from_ready_socket_and_reply
)
client_sel
.
register
(
client
,
selectors
.
EVENT_READ
,
i
)
# Prime them all to be ready at once.
for
i
,
client
in
enumerate
(
clients
):
data
=
str
(
i
).
encode
(
'ascii'
)
client
.
send
(
data
)
# Read and reply to all the clients
self
.
run_selector_once
(
server_sel
)
found
=
0
for
key
,
_
in
client_sel
.
select
(
timeout
=
3
):
expected
=
str
(
key
.
data
).
encode
(
'ascii'
)
data
=
key
.
fileobj
.
recv
(
50
)
self
.
assertEqual
(
data
,
expected
)
found
+=
1
self
.
assertEqual
(
found
,
len
(
pairs
))
finally
:
server_sel
.
close
()
client_sel
.
close
()
for
pair
in
pairs
:
for
s
in
pair
:
s
.
close
()
if
__name__
==
'__main__'
:
greentest
.
main
()
src/gevent/tests/test__threadpool.py
View file @
a51e0937
...
...
@@ -438,7 +438,7 @@ class TestMaxsize(TestCase):
pool
.
spawn
(
sleep
,
0.2
)
pool
.
spawn
(
sleep
,
0.3
)
gevent
.
sleep
(
0.2
)
self
.
assert
Equal
(
pool
.
size
,
3
)
self
.
assert
GreaterEqual
(
pool
.
size
,
2
)
pool
.
maxsize
=
0
gevent
.
sleep
(
0.2
)
self
.
assertEqualFlakyRaceCondition
(
pool
.
size
,
0
)
...
...
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