def test_correct_threadlocal_dicts(self): key = 'value' num_threads = 5 def get_tld(container): current = threadlocal.default() current[key] = self.get_some_text() container.put(current._get_local_dict()) local_dicts = queue.Queue() threads = [] for _ in xrange(num_threads): t = threading.Thread(target=get_tld, args=(local_dicts,)) t.start() threads.append(t) # and one to grow on threadlocal.default()[key] = self.get_some_text() local_dicts.put(threadlocal.default()._get_local_dict()) for thread in threads: thread.join() values = set() while not local_dicts.empty(): tld = local_dicts.get(block=False) values.add(tld[key]) # assert 6 dicts were created with 6 different values self.assertEqual(len(values), num_threads+1)
def test_correct_threadlocal_dicts(self): key = 'value' num_threads = 5 def get_tld(container): current = threadlocal.default() current[key] = self.get_some_text() container.put(current._get_local_dict()) local_dicts = queue.Queue() threads = [] for _ in xrange(num_threads): t = threading.Thread(target=get_tld, args=(local_dicts, )) t.start() threads.append(t) # and one to grow on threadlocal.default()[key] = self.get_some_text() local_dicts.put(threadlocal.default()._get_local_dict()) for thread in threads: thread.join() values = set() while not local_dicts.empty(): tld = local_dicts.get(block=False) values.add(tld[key]) # assert 6 dicts were created with 6 different values self.assertEqual(len(values), num_threads + 1)
def __call__(self, environ, start_response): """Handle WSGI Request.""" with clear(threadlocal.default()) as context: assert context == {}, "New thread context was not empty" self.populate_context(context, environ) environ['context'] = context resp = self.app( environ, self.start_response_callback(start_response, context['transaction_id'])) return resp
def get_tld(container): current = threadlocal.default() current[key] = self.get_some_text() container.put(current._get_local_dict())
def test_threadlocal_dict_repr(self): tld = threadlocal.default() tld['good'] = 'glob' repr_str = repr(tld) expected = "<ThreadLocalDict {'good': 'glob'}>" self.assertEqual(repr_str, expected)
def test_default(self): self.assertIs(threadlocal.default(), threadlocal.CONTEXT)
def setUp(self): # clear the threadlocal dict threadlocal.default().clear()
def tearDown(self): self.patcher.stop() threadlocal.default().clear() super(TestContextMiddleware, self).tearDown()
def test_non_default_namespace(self): tld = threadlocal.default() another = threadlocal.ThreadLocalDict('custom_namespace') self.assertIsNot(tld._get_local_dict(), another._get_local_dict())
def test_default_namespace(self): tld = threadlocal.default() another = threadlocal.ThreadLocalDict(threadlocal.DEFAULT_NAMESPACE) self.assertIs(tld._get_local_dict(), another._get_local_dict())
def test_request(self): env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} self.filter(env, self.start_response) self.assertEqual(threadlocal.default(), {})