Created
June 14, 2013 20:29
-
-
Save emmettbutler/5785018 to your computer and use it in GitHub Desktop.
Tornado TestCase subclass that caches responses in memory. Useful when you need multiple test methods testing a single response and that response takes a long time to generate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime as dt | |
from tornado.testing import AsyncHTTPTestCase | |
class CachedTestCase(AsyncHTTPTestCase): | |
responses = {} | |
def cache_response(self, key, response): | |
self.responses[key] = {} | |
self.responses[key]['response'] = response | |
self.responses[key]['time'] = dt.datetime.now() | |
def get_cached_url(self, path): | |
if path not in self.responses.keys(): | |
self.http_client.fetch(self.get_url(path), self.stop) | |
self.cache_response(path, self.wait(timeout=30)) | |
return self.responses[path] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment