Commit b026147a authored by Jason Madden's avatar Jason Madden

Port contextvars test to all versions of python.

parent 9e078011
# gevent: ed from 3.7 to test our monkey-patch. # gevent: copied from 3.7 to test our monkey-patch.
# XXX: We need to move this to src/gevent/tests and # Modified to work on all versions of Python.
# make it run on all supported Python versions. import gevent.monkey; gevent.monkey.patch_all()
import sys
import concurrent.futures import concurrent.futures
import contextvars try:
import contextvars
except ImportError:
from gevent import contextvars
import functools import functools
import gc import gc
import random import random
...@@ -10,11 +15,11 @@ import time ...@@ -10,11 +15,11 @@ import time
import unittest import unittest
import weakref import weakref
try: # try:
from _testcapi import hamt # from _testcapi import hamt
except ImportError: # except ImportError:
hamt = None # hamt = None
hamt = None
def isolated_context(func): def isolated_context(func):
"""Needed to make reftracking test mode work.""" """Needed to make reftracking test mode work."""
...@@ -26,12 +31,17 @@ def isolated_context(func): ...@@ -26,12 +31,17 @@ def isolated_context(func):
class ContextTest(unittest.TestCase): class ContextTest(unittest.TestCase):
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
def test_context_var_new_1(self): def test_context_var_new_1(self):
with self.assertRaisesRegex(TypeError, 'takes exactly 1'): with self.assertRaises(TypeError):
contextvars.ContextVar() contextvars.ContextVar()
with self.assertRaisesRegex(TypeError, 'must be a str'): # gevent: Doesn't raise
contextvars.ContextVar(1) # with self.assertRaisesRegex(TypeError, 'must be a str'):
# contextvars.ContextVar(1)
c = contextvars.ContextVar('aaa') c = contextvars.ContextVar('aaa')
self.assertEqual(c.name, 'aaa') self.assertEqual(c.name, 'aaa')
...@@ -61,26 +71,27 @@ class ContextTest(unittest.TestCase): ...@@ -61,26 +71,27 @@ class ContextTest(unittest.TestCase):
c.reset(t) c.reset(t)
self.assertIn(' used ', repr(t)) self.assertIn(' used ', repr(t))
def test_context_subclassing_1(self): # gevent: Doesn't raise
with self.assertRaisesRegex(TypeError, 'not an acceptable base type'): # def test_context_subclassing_1(self):
class MyContextVar(contextvars.ContextVar): # with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
# Potentially we might want ContextVars to be subclassable. # class MyContextVar(contextvars.ContextVar):
pass # # Potentially we might want ContextVars to be subclassable.
# pass
with self.assertRaisesRegex(TypeError, 'not an acceptable base type'): # with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
class MyContext(contextvars.Context): # class MyContext(contextvars.Context):
pass # pass
with self.assertRaisesRegex(TypeError, 'not an acceptable base type'): # with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
class MyToken(contextvars.Token): # class MyToken(contextvars.Token):
pass # pass
def test_context_new_1(self): def test_context_new_1(self):
with self.assertRaisesRegex(TypeError, 'any arguments'): with self.assertRaises(TypeError):
contextvars.Context(1) contextvars.Context(1)
with self.assertRaisesRegex(TypeError, 'any arguments'): with self.assertRaises(TypeError):
contextvars.Context(1, a=1) contextvars.Context(1, a=1)
with self.assertRaisesRegex(TypeError, 'any arguments'): with self.assertRaises(TypeError):
contextvars.Context(a=1) contextvars.Context(a=1)
contextvars.Context(**{}) contextvars.Context(**{})
...@@ -98,11 +109,12 @@ class ContextTest(unittest.TestCase): ...@@ -98,11 +109,12 @@ class ContextTest(unittest.TestCase):
ctx = contextvars.copy_context() ctx = contextvars.copy_context()
self.assertIsInstance(ctx, contextvars.Context) self.assertIsInstance(ctx, contextvars.Context)
def test_context_run_1(self): # gevent: This doesn't raise
ctx = contextvars.Context() # def test_context_run_1(self):
# ctx = contextvars.Context()
with self.assertRaisesRegex(TypeError, 'missing 1 required'): # with self.assertRaisesRegex(TypeError, 'missing 1 required'):
ctx.run() # ctx.run()
def test_context_run_2(self): def test_context_run_2(self):
ctx = contextvars.Context() ctx = contextvars.Context()
...@@ -373,14 +385,14 @@ class ContextTest(unittest.TestCase): ...@@ -373,14 +385,14 @@ class ContextTest(unittest.TestCase):
class HashKey: class HashKey:
_crasher = None _crasher = None
def __init__(self, hash, name, *, error_on_eq_to=None): def __init__(self, hash, name, error_on_eq_to=None):
assert hash != -1 assert hash != -1
self.name = name self.name = name
self.hash = hash self.hash = hash
self.error_on_eq_to = error_on_eq_to self.error_on_eq_to = error_on_eq_to
def __repr__(self): # def __repr__(self):
return f'<Key name:{self.name} hash:{self.hash}>' # return f'<Key name:{self.name} hash:{self.hash}>'
def __hash__(self): def __hash__(self):
if self._crasher is not None and self._crasher.error_on_hash: if self._crasher is not None and self._crasher.error_on_hash:
...@@ -396,9 +408,9 @@ class HashKey: ...@@ -396,9 +408,9 @@ class HashKey:
raise EqError raise EqError
if self.error_on_eq_to is not None and self.error_on_eq_to is other: if self.error_on_eq_to is not None and self.error_on_eq_to is other:
raise ValueError(f'cannot compare {self!r} to {other!r}') raise ValueError#(f'cannot compare {self!r} to {other!r}')
if other.error_on_eq_to is not None and other.error_on_eq_to is self: if other.error_on_eq_to is not None and other.error_on_eq_to is self:
raise ValueError(f'cannot compare {other!r} to {self!r}') raise ValueError#(f'cannot compare {other!r} to {self!r}')
return (self.name, self.hash) == (other.name, other.hash) return (self.name, self.hash) == (other.name, other.hash)
...@@ -416,7 +428,7 @@ class KeyStr(str): ...@@ -416,7 +428,7 @@ class KeyStr(str):
class HaskKeyCrasher: class HaskKeyCrasher:
def __init__(self, *, error_on_hash=False, error_on_eq=False): def __init__(self, error_on_hash=False, error_on_eq=False):
self.error_on_hash = error_on_hash self.error_on_hash = error_on_hash
self.error_on_eq = error_on_eq self.error_on_eq = error_on_eq
...@@ -812,7 +824,7 @@ class HamtTest(unittest.TestCase): ...@@ -812,7 +824,7 @@ class HamtTest(unittest.TestCase):
for i in range(17): for i in range(17):
key = HashKey(i, str(i)) key = HashKey(i, str(i))
keys.append(key) keys.append(key)
h = h.set(key, f'val-{i}') h = h.set(key, 'val-{i}'.format(i=i))
collision_key16 = HashKey(16, '18') collision_key16 = HashKey(16, '18')
h = h.set(collision_key16, 'collision') h = h.set(collision_key16, 'collision')
...@@ -1071,4 +1083,5 @@ class HamtTest(unittest.TestCase): ...@@ -1071,4 +1083,5 @@ class HamtTest(unittest.TestCase):
if __name__ == "__main__": if __name__ == "__main__":
if not gevent.monkey.PY37:
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment