示例#1
0
    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)
示例#2
0
    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)
示例#3
0
 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
示例#4
0
 def get_tld(container):
     current = threadlocal.default()
     current[key] = self.get_some_text()
     container.put(current._get_local_dict())
示例#5
0
 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)
示例#6
0
 def test_default(self):
     self.assertIs(threadlocal.default(), threadlocal.CONTEXT)
示例#7
0
 def setUp(self):
     # clear the threadlocal dict
     threadlocal.default().clear()
 def tearDown(self):
     self.patcher.stop()
     threadlocal.default().clear()
     super(TestContextMiddleware, self).tearDown()
示例#9
0
 def test_non_default_namespace(self):
     tld = threadlocal.default()
     another = threadlocal.ThreadLocalDict('custom_namespace')
     self.assertIsNot(tld._get_local_dict(), another._get_local_dict())
示例#10
0
 def get_tld(container):
     current = threadlocal.default()
     current[key] = self.get_some_text()
     container.put(current._get_local_dict())
示例#11
0
 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)
示例#12
0
 def test_default(self):
     self.assertIs(threadlocal.default(), threadlocal.CONTEXT)
示例#13
0
 def setUp(self):
     # clear the threadlocal dict
     threadlocal.default().clear()
示例#14
0
 def test_non_default_namespace(self):
     tld = threadlocal.default()
     another = threadlocal.ThreadLocalDict('custom_namespace')
     self.assertIsNot(tld._get_local_dict(), another._get_local_dict())
示例#15
0
 def test_default_namespace(self):
     tld = threadlocal.default()
     another = threadlocal.ThreadLocalDict(threadlocal.DEFAULT_NAMESPACE)
     self.assertIs(tld._get_local_dict(), another._get_local_dict())
示例#16
0
 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(), {})