def test_after_exception(self):
        """Test setting, getting state after exception in threads
        """
        state['a'] = 1
        buf_out = StringIO()
        buf_err = StringIO()
        try:
            with stdout_err_redirector(buf_out, buf_err):
                with thread_state_context():

                    def worker():
                        """thread worker function"""
                        raise RuntimeError

                    threads = []
                    for _ in range(20):
                        thread = threading.Thread(target=worker)
                        thread.start()
                        threads.append(thread)

                    for thread in threads:
                        thread.join()
                    raise RuntimeError
        except RuntimeError:
            pass
        self.assertEqual(state['a'], 1)
    def test_after_exception(self):
        """Test setting, getting state after exception in threads
        """
        state['a'] = 1
        buf_out = StringIO()
        buf_err = StringIO()
        try:
            with stdout_err_redirector(buf_out, buf_err):
                with thread_state_context():
                    def worker():
                        """thread worker function"""
                        raise RuntimeError

                    threads = []
                    for _ in range(20):
                        thread = threading.Thread(target=worker)
                        thread.start()
                        threads.append(thread)

                    for thread in threads:
                        thread.join()
                    raise RuntimeError
        except RuntimeError:
            pass
        self.assertEqual(state['a'], 1)
    def test_delete_nonexisting(self):
        """Test deleting non-existing sub-state in threads
        """
        with thread_state_context():
            def worker():
                """thread worker function"""
                def dell():
                    """Test function"""
                    del state['foo']
                self.assertRaises(KeyError, dell)

            threads = []
            for _ in range(20):
                thread = threading.Thread(target=worker)
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()
    def test_basic_functionality(self):
        """Test setting, getting sub-state in 20 threads
        """
        with thread_state_context():
            def worker(i):
                """thread worker function"""
                state[str(i)] = i
                self.assertEqual(state[str(i)], i)

            threads = []
            for i in range(20):
                thread = threading.Thread(target=worker, args=(i,))
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()

            for i in range(len(threads)):
                self.assertEqual(state[str(i)], i)
    def test_delete_nonexisting(self):
        """Test deleting non-existing sub-state in threads
        """
        with thread_state_context():

            def worker():
                """thread worker function"""
                def dell():
                    """Test function"""
                    del state['foo']

                self.assertRaises(KeyError, dell)

            threads = []
            for _ in range(20):
                thread = threading.Thread(target=worker)
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()
    def test_basic_functionality(self):
        """Test setting, getting sub-state in 20 threads
        """
        with thread_state_context():

            def worker(i):
                """thread worker function"""
                state[str(i)] = i
                self.assertEqual(state[str(i)], i)

            threads = []
            for i in range(20):
                thread = threading.Thread(target=worker, args=(i, ))
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()

            for i in range(len(threads)):
                self.assertEqual(state[str(i)], i)