Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 4
0
 def get_tld(container):
     current = threadlocal.default()
     current[key] = self.get_some_text()
     container.put(current._get_local_dict())
Ejemplo n.º 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)
Ejemplo n.º 6
0
 def test_default(self):
     self.assertIs(threadlocal.default(), threadlocal.CONTEXT)
Ejemplo n.º 7
0
 def setUp(self):
     # clear the threadlocal dict
     threadlocal.default().clear()
Ejemplo n.º 8
0
 def tearDown(self):
     self.patcher.stop()
     threadlocal.default().clear()
     super(TestContextMiddleware, self).tearDown()
Ejemplo n.º 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())
Ejemplo n.º 10
0
 def get_tld(container):
     current = threadlocal.default()
     current[key] = self.get_some_text()
     container.put(current._get_local_dict())
Ejemplo n.º 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)
Ejemplo n.º 12
0
 def test_default(self):
     self.assertIs(threadlocal.default(), threadlocal.CONTEXT)
Ejemplo n.º 13
0
 def setUp(self):
     # clear the threadlocal dict
     threadlocal.default().clear()
Ejemplo n.º 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())
Ejemplo n.º 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())
Ejemplo n.º 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())
Ejemplo n.º 17
0
 def test_request(self):
     env = {'REQUEST_METHOD': 'GET',
            'PATH_INFO': '/'}
     self.filter(env, self.start_response)
     self.assertEqual(threadlocal.default(), {})