File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#!/usr/bin/env python
2- from concurrent .futures .thread import ThreadPoolExecutor
32import re
3+ import random
4+ import string
5+ import threading
46from tests import unittest , OrderedDict
57
68from jmespath import parser
@@ -322,30 +324,29 @@ def test_cache_purge(self):
322324 self .assertEqual (first .parsed ,
323325 cached .parsed )
324326
325- def test_free_cache_entries (self ):
326- """
327- Test for a race that occurs during cache eviction. The parser cache is
328- shared by all threads in a process so if two threads randomly select to
329- evict the same entry, one of them would raise a KeyError if they used
330- `del` to evict it.
331- """
332- num_workers = 32
333- num_repetitions = 4096
334-
327+ def test_thread_safety_of_cache (self ):
328+ errors = []
329+ expressions = [
330+ '' .join (random .choice (string .ascii_letters ) for _ in range (3 ))
331+ for _ in range (2000 )
332+ ]
335333 def worker ():
336- for _ in range (num_repetitions ):
337- p = parser .Parser ()
338- p .parse ('foo' )
339- p .parse ('bar' )
340- p .parse ('fubaz' )
341-
342- parser .Parser ._MAX_SIZE , original_size = 2 , parser .Parser ._MAX_SIZE ,
343- try :
344- with ThreadPoolExecutor (max_workers = num_workers ) as tpe :
345- futures = [tpe .submit (worker ) for _ in range (num_workers )]
346- self .assertTrue (all (f .result () is None for f in futures ))
347- finally :
348- parser .Parser ._MAX_SIZE = original_size
334+ p = parser .Parser ()
335+ for expression in expressions :
336+ try :
337+ p .parse (expression )
338+ except Exception as e :
339+ errors .append (e )
340+
341+ threads = []
342+ for i in range (10 ):
343+ threads .append (threading .Thread (target = worker ))
344+ for thread in threads :
345+ thread .start ()
346+ for thread in threads :
347+ thread .join ()
348+
349+ self .assertEqual (errors , [])
349350
350351
351352class TestParserAddsExpressionAttribute (unittest .TestCase ):
You can’t perform that action at this time.
0 commit comments