def test_write_thread_storage_in_flush(self): request_cache.set('key', 'main') self.assertTrue(request_cache.has('key')) self.assertEqual('main', request_cache.get('key')) thread_output = {} def thread_func(thread_output): request_cache.set('key', 'thread') thread_output['value_of_get_before_flush'] = ( request_cache.get('key')) request_cache.flush() thread_output['value_of_get_after_flush'] = ( request_cache.get('key')) thread = threading.Thread(target=thread_func, args=[thread_output]) thread.start() thread.join() # The main thread should not see changes made by the second # thread. self.assertTrue(request_cache.has('key')) self.assertEqual('main', request_cache.get('key')) self.assertEqual('thread', thread_output['value_of_get_before_flush']) self.assertIsNone(thread_output['value_of_get_after_flush'])
def test_read_thread_storage_in_has(self): self.assertFalse(request_cache.has('key')) request_cache.set('key', 'main') self.assertTrue(request_cache.has('key')) thread_output = {} def thread_func(thread_output): thread_output['value_of_has'] = request_cache.has('key') thread = threading.Thread(target=thread_func, args=[thread_output]) thread.start() thread.join() # The second thread should see different values than the main thread. self.assertFalse(thread_output['value_of_has'])
def thread_func(thread_output): thread_output['value_of_has'] = request_cache.has('key')