{"action":"created","comment":{"url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/comments/27015601","id":27015601,"diff_hunk":"@@ -0,0 +1,363 @@\n+# -*- test-case-name: vumi_message_store.tests.test_riak_backend -*-\n+\n+\"\"\"\n+Fake in-memory Riak manager that implements enough for the message store.\n+\"\"\"\n+\n+# TODO: Sync and async.\n+\n+import base64\n+import json\n+\n+from twisted.internet import reactor\n+from twisted.internet.defer import Deferred, inlineCallbacks, returnValue\n+from vumi.persist.model import ModelProxy\n+from vumi.utils import flatten_generator\n+\n+\n+class FakeMemoryRiakManager(object):\n+    \"\"\"\n+    This is an in-memory implementation of a subset of the Riak manager's\n+    functionality.\n+\n+    NOTE: This is the minimal subset necessary for MessageStoreRiakBackend.\n+    \"\"\"\n+\n+    def __init__(self, state, store_versions=None):\n+        self._state = state\n+        self._is_sync = state._is_sync\n+        if self._is_sync:\n+            self.call_decorator = flatten_generator\n+        else:\n+            self.call_decorator = inlineCallbacks\n+        self.store_versions = store_versions or {}\n+        # Wrap the methods that need to handle async stuff.\n+        self.load = self.call_decorator(self._internal_load)\n+        self.store = self.call_decorator(self._internal_store)\n+\n+    def proxy(self, modelcls):\n+        \"\"\"\n+        Return a real proxy using the fake manager.\n+        \"\"\"\n+        return ModelProxy(self, modelcls)\n+\n+    def bucket_name(self, modelcls_or_obj):\n+        return modelcls_or_obj.bucket\n+\n+    def bucket_for_modelcls(self, modelcls):\n+        \"\"\"\n+        Return a fake bucket object for the given model class.\n+        \"\"\"\n+        bucket_name = self.bucket_name(modelcls)\n+        return FakeRiakBucket(self._state, bucket_name)\n+\n+    def riak_object(self, modelcls, key):\n+        \"\"\"\n+        Create and return a fake RiakObject.\n+        \"\"\"\n+        bucket_name = self.bucket_name(modelcls)\n+        riak_object = FakeRiakObject(self._state, bucket_name, key)\n+        riak_object.set_content_type(\"application/json\")\n+        riak_object.set_data({'$VERSION': modelcls.VERSION})\n+        return riak_object\n+\n+    def _internal_store(self, modelobj):\n+        \"\"\"\n+        This method gets wrapped in a sync or async decorator in __init__().\n+        \"\"\"\n+        riak_object = modelobj._riak_object\n+        modelcls = type(modelobj)\n+        model_name = \"%s.%s\" % (modelcls.__module__, modelcls.__name__)\n+        store_version = self.store_versions.get(model_name, modelcls.VERSION)\n+        # Run reverse migrators until we have the correct version of the data.\n+        data_version = riak_object.get_data().get('$VERSION', None)\n+        while data_version != store_version:\n+            migrator = modelcls.MIGRATOR(\n+                modelcls, self, data_version, reverse=True)\n+            riak_object = migrator(riak_object).get_riak_object()\n+            data_version = riak_object.get_data().get('$VERSION', None)\n+        yield riak_object.store()\n+        returnValue(modelobj)\n+\n+    def _internal_load(self, modelcls, key, result=None):\n+        \"\"\"\n+        This method gets wrapped in a sync or async decorator in __init__().\n+        \"\"\"\n+        assert result is None\n+        riak_object = self.riak_object(modelcls, key)\n+        yield riak_object.reload()\n+        was_migrated = False\n+\n+        # Run migrators until we have the correct version of the data.\n+        while riak_object.get_data() is not None:\n+            data_version = riak_object.get_data().get('$VERSION', None)\n+            if data_version == modelcls.VERSION:\n+                obj = modelcls(self, key, _riak_object=riak_object)\n+                obj.was_migrated = was_migrated\n+                returnValue(obj)\n+            migrator = modelcls.MIGRATOR(modelcls, self, data_version)\n+            riak_object = migrator(riak_object).get_riak_object()\n+            was_migrated = True\n+        returnValue(None)\n+\n+    def index_keys(self, model, index_name, start_value, end_value=None,\n+                   return_terms=None):\n+        bucket = self.bucket_for_modelcls(model)\n+        return bucket.get_index(\n+            index_name, start_value, end_value, return_terms=return_terms)\n+\n+    def index_keys_page(self, model, index_name, start_value, end_value=None,\n+                        return_terms=None, max_results=None,\n+                        continuation=None):\n+        bucket = self.bucket_for_modelcls(model)\n+        return bucket.get_index_page(\n+            index_name, start_value, end_value, return_terms=return_terms,\n+            max_results=max_results, continuation=continuation)\n+\n+\n+class FakeRiakState(object):\n+    \"\"\"\n+    An object to hold fake Riak state.\n+    \"\"\"\n+\n+    def __init__(self, is_sync):\n+        self._buckets = {}\n+        self._is_sync = is_sync\n+        self._delayed_calls = []\n+\n+    def teardown(self):\n+        for delayed in self._delayed_calls:\n+            if not (delayed.cancelled or delayed.called):\n+                delayed.cancel()\n+\n+    def delayed_result(self, result):\n+        \"\"\"\n+        If in async mode, return the result with some delay to catch code that\n+        doesn't properly wait for the deferred to fire.\n+        \"\"\"\n+        if self._is_sync:\n+            return result\n+\n+        # Add some latency to catch things that don't wait on deferreds. We\n+        # can't use deferLater() here because we want to keep track of the\n+        # delayed call object.\n+        d = Deferred()\n+        delayed = reactor.callLater(0.002, d.callback, result)","path":"vumi_message_store/memory_backend_manager.py","position":145,"original_position":145,"commit_id":"b1194400259144ff55cf3bb0d5d1972a5526530d","original_commit_id":"b1194400259144ff55cf3bb0d5d1972a5526530d","user":{"login":"justinvdm","id":1731223,"avatar_url":"https://avatars.githubusercontent.com/u/1731223?v=3","gravatar_id":"","url":"https://api.github.com/users/justinvdm","html_url":"https://github.com/justinvdm","followers_url":"https://api.github.com/users/justinvdm/followers","following_url":"https://api.github.com/users/justinvdm/following{/other_user}","gists_url":"https://api.github.com/users/justinvdm/gists{/gist_id}","starred_url":"https://api.github.com/users/justinvdm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinvdm/subscriptions","organizations_url":"https://api.github.com/users/justinvdm/orgs","repos_url":"https://api.github.com/users/justinvdm/repos","events_url":"https://api.github.com/users/justinvdm/events{/privacy}","received_events_url":"https://api.github.com/users/justinvdm/received_events","type":"User","site_admin":false},"body":"s/enough// :)","created_at":"2015-03-24T10:21:58Z","updated_at":"2015-03-24T10:21:58Z","html_url":"https://github.com/praekelt/vumi-message-store/pull/7#discussion_r27015601","pull_request_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/comments/27015601"},"html":{"href":"https://github.com/praekelt/vumi-message-store/pull/7#discussion_r27015601"},"pull_request":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7","id":30577660,"html_url":"https://github.com/praekelt/vumi-message-store/pull/7","diff_url":"https://github.com/praekelt/vumi-message-store/pull/7.diff","patch_url":"https://github.com/praekelt/vumi-message-store/pull/7.patch","issue_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/7","number":7,"state":"open","locked":false,"title":"In-memory message store backend","user":{"login":"jerith","id":43731,"avatar_url":"https://avatars.githubusercontent.com/u/43731?v=3","gravatar_id":"","url":"https://api.github.com/users/jerith","html_url":"https://github.com/jerith","followers_url":"https://api.github.com/users/jerith/followers","following_url":"https://api.github.com/users/jerith/following{/other_user}","gists_url":"https://api.github.com/users/jerith/gists{/gist_id}","starred_url":"https://api.github.com/users/jerith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerith/subscriptions","organizations_url":"https://api.github.com/users/jerith/orgs","repos_url":"https://api.github.com/users/jerith/repos","events_url":"https://api.github.com/users/jerith/events{/privacy}","received_events_url":"https://api.github.com/users/jerith/received_events","type":"User","site_admin":false},"body":"Really useful for tests and such. :-)","created_at":"2015-03-05T17:31:22Z","updated_at":"2015-03-24T10:21:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3cba8185dbfae33ffa38d5897eb4060673430be8","assignee":{"login":"jerith","id":43731,"avatar_url":"https://avatars.githubusercontent.com/u/43731?v=3","gravatar_id":"","url":"https://api.github.com/users/jerith","html_url":"https://github.com/jerith","followers_url":"https://api.github.com/users/jerith/followers","following_url":"https://api.github.com/users/jerith/following{/other_user}","gists_url":"https://api.github.com/users/jerith/gists{/gist_id}","starred_url":"https://api.github.com/users/jerith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerith/subscriptions","organizations_url":"https://api.github.com/users/jerith/orgs","repos_url":"https://api.github.com/users/jerith/repos","events_url":"https://api.github.com/users/jerith/events{/privacy}","received_events_url":"https://api.github.com/users/jerith/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7/commits","review_comments_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7/comments","review_comment_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/comments{/number}","comments_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/7/comments","statuses_url":"https://api.github.com/repos/praekelt/vumi-message-store/statuses/b1194400259144ff55cf3bb0d5d1972a5526530d","head":{"label":"praekelt:feature/issue-7-in-memory-backend","ref":"feature/issue-7-in-memory-backend","sha":"b1194400259144ff55cf3bb0d5d1972a5526530d","user":{"login":"praekelt","id":81810,"avatar_url":"https://avatars.githubusercontent.com/u/81810?v=3","gravatar_id":"","url":"https://api.github.com/users/praekelt","html_url":"https://github.com/praekelt","followers_url":"https://api.github.com/users/praekelt/followers","following_url":"https://api.github.com/users/praekelt/following{/other_user}","gists_url":"https://api.github.com/users/praekelt/gists{/gist_id}","starred_url":"https://api.github.com/users/praekelt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/praekelt/subscriptions","organizations_url":"https://api.github.com/users/praekelt/orgs","repos_url":"https://api.github.com/users/praekelt/repos","events_url":"https://api.github.com/users/praekelt/events{/privacy}","received_events_url":"https://api.github.com/users/praekelt/received_events","type":"Organization","site_admin":false},"repo":{"id":30292357,"name":"vumi-message-store","full_name":"praekelt/vumi-message-store","owner":{"login":"praekelt","id":81810,"avatar_url":"https://avatars.githubusercontent.com/u/81810?v=3","gravatar_id":"","url":"https://api.github.com/users/praekelt","html_url":"https://github.com/praekelt","followers_url":"https://api.github.com/users/praekelt/followers","following_url":"https://api.github.com/users/praekelt/following{/other_user}","gists_url":"https://api.github.com/users/praekelt/gists{/gist_id}","starred_url":"https://api.github.com/users/praekelt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/praekelt/subscriptions","organizations_url":"https://api.github.com/users/praekelt/orgs","repos_url":"https://api.github.com/users/praekelt/repos","events_url":"https://api.github.com/users/praekelt/events{/privacy}","received_events_url":"https://api.github.com/users/praekelt/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/praekelt/vumi-message-store","description":"A persistent store for vumi messages.","fork":false,"url":"https://api.github.com/repos/praekelt/vumi-message-store","forks_url":"https://api.github.com/repos/praekelt/vumi-message-store/forks","keys_url":"https://api.github.com/repos/praekelt/vumi-message-store/keys{/key_id}","collaborators_url":"https://api.github.com/repos/praekelt/vumi-message-store/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/praekelt/vumi-message-store/teams","hooks_url":"https://api.github.com/repos/praekelt/vumi-message-store/hooks","issue_events_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/events{/number}","events_url":"https://api.github.com/repos/praekelt/vumi-message-store/events","assignees_url":"https://api.github.com/repos/praekelt/vumi-message-store/assignees{/user}","branches_url":"https://api.github.com/repos/praekelt/vumi-message-store/branches{/branch}","tags_url":"https://api.github.com/repos/praekelt/vumi-message-store/tags","blobs_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/refs{/sha}","trees_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/trees{/sha}","statuses_url":"https://api.github.com/repos/praekelt/vumi-message-store/statuses/{sha}","languages_url":"https://api.github.com/repos/praekelt/vumi-message-store/languages","stargazers_url":"https://api.github.com/repos/praekelt/vumi-message-store/stargazers","contributors_url":"https://api.github.com/repos/praekelt/vumi-message-store/contributors","subscribers_url":"https://api.github.com/repos/praekelt/vumi-message-store/subscribers","subscription_url":"https://api.github.com/repos/praekelt/vumi-message-store/subscription","commits_url":"https://api.github.com/repos/praekelt/vumi-message-store/commits{/sha}","git_commits_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/commits{/sha}","comments_url":"https://api.github.com/repos/praekelt/vumi-message-store/comments{/number}","issue_comment_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/comments{/number}","contents_url":"https://api.github.com/repos/praekelt/vumi-message-store/contents/{+path}","compare_url":"https://api.github.com/repos/praekelt/vumi-message-store/compare/{base}...{head}","merges_url":"https://api.github.com/repos/praekelt/vumi-message-store/merges","archive_url":"https://api.github.com/repos/praekelt/vumi-message-store/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/praekelt/vumi-message-store/downloads","issues_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues{/number}","pulls_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls{/number}","milestones_url":"https://api.github.com/repos/praekelt/vumi-message-store/milestones{/number}","notifications_url":"https://api.github.com/repos/praekelt/vumi-message-store/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/praekelt/vumi-message-store/labels{/name}","releases_url":"https://api.github.com/repos/praekelt/vumi-message-store/releases{/id}","created_at":"2015-02-04T09:56:40Z","updated_at":"2015-03-23T12:07:57Z","pushed_at":"2015-03-23T12:08:13Z","git_url":"git://github.com/praekelt/vumi-message-store.git","ssh_url":"git@github.com:praekelt/vumi-message-store.git","clone_url":"https://github.com/praekelt/vumi-message-store.git","svn_url":"https://github.com/praekelt/vumi-message-store","homepage":null,"size":280,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":0,"default_branch":"develop"}},"base":{"label":"praekelt:develop","ref":"develop","sha":"8001c8ccf69cd46749cd25e447a76e6c2786572a","user":{"login":"praekelt","id":81810,"avatar_url":"https://avatars.githubusercontent.com/u/81810?v=3","gravatar_id":"","url":"https://api.github.com/users/praekelt","html_url":"https://github.com/praekelt","followers_url":"https://api.github.com/users/praekelt/followers","following_url":"https://api.github.com/users/praekelt/following{/other_user}","gists_url":"https://api.github.com/users/praekelt/gists{/gist_id}","starred_url":"https://api.github.com/users/praekelt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/praekelt/subscriptions","organizations_url":"https://api.github.com/users/praekelt/orgs","repos_url":"https://api.github.com/users/praekelt/repos","events_url":"https://api.github.com/users/praekelt/events{/privacy}","received_events_url":"https://api.github.com/users/praekelt/received_events","type":"Organization","site_admin":false},"repo":{"id":30292357,"name":"vumi-message-store","full_name":"praekelt/vumi-message-store","owner":{"login":"praekelt","id":81810,"avatar_url":"https://avatars.githubusercontent.com/u/81810?v=3","gravatar_id":"","url":"https://api.github.com/users/praekelt","html_url":"https://github.com/praekelt","followers_url":"https://api.github.com/users/praekelt/followers","following_url":"https://api.github.com/users/praekelt/following{/other_user}","gists_url":"https://api.github.com/users/praekelt/gists{/gist_id}","starred_url":"https://api.github.com/users/praekelt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/praekelt/subscriptions","organizations_url":"https://api.github.com/users/praekelt/orgs","repos_url":"https://api.github.com/users/praekelt/repos","events_url":"https://api.github.com/users/praekelt/events{/privacy}","received_events_url":"https://api.github.com/users/praekelt/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/praekelt/vumi-message-store","description":"A persistent store for vumi messages.","fork":false,"url":"https://api.github.com/repos/praekelt/vumi-message-store","forks_url":"https://api.github.com/repos/praekelt/vumi-message-store/forks","keys_url":"https://api.github.com/repos/praekelt/vumi-message-store/keys{/key_id}","collaborators_url":"https://api.github.com/repos/praekelt/vumi-message-store/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/praekelt/vumi-message-store/teams","hooks_url":"https://api.github.com/repos/praekelt/vumi-message-store/hooks","issue_events_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/events{/number}","events_url":"https://api.github.com/repos/praekelt/vumi-message-store/events","assignees_url":"https://api.github.com/repos/praekelt/vumi-message-store/assignees{/user}","branches_url":"https://api.github.com/repos/praekelt/vumi-message-store/branches{/branch}","tags_url":"https://api.github.com/repos/praekelt/vumi-message-store/tags","blobs_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/refs{/sha}","trees_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/trees{/sha}","statuses_url":"https://api.github.com/repos/praekelt/vumi-message-store/statuses/{sha}","languages_url":"https://api.github.com/repos/praekelt/vumi-message-store/languages","stargazers_url":"https://api.github.com/repos/praekelt/vumi-message-store/stargazers","contributors_url":"https://api.github.com/repos/praekelt/vumi-message-store/contributors","subscribers_url":"https://api.github.com/repos/praekelt/vumi-message-store/subscribers","subscription_url":"https://api.github.com/repos/praekelt/vumi-message-store/subscription","commits_url":"https://api.github.com/repos/praekelt/vumi-message-store/commits{/sha}","git_commits_url":"https://api.github.com/repos/praekelt/vumi-message-store/git/commits{/sha}","comments_url":"https://api.github.com/repos/praekelt/vumi-message-store/comments{/number}","issue_comment_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues/comments{/number}","contents_url":"https://api.github.com/repos/praekelt/vumi-message-store/contents/{+path}","compare_url":"https://api.github.com/repos/praekelt/vumi-message-store/compare/{base}...{head}","merges_url":"https://api.github.com/repos/praekelt/vumi-message-store/merges","archive_url":"https://api.github.com/repos/praekelt/vumi-message-store/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/praekelt/vumi-message-store/downloads","issues_url":"https://api.github.com/repos/praekelt/vumi-message-store/issues{/number}","pulls_url":"https://api.github.com/repos/praekelt/vumi-message-store/pulls{/number}","milestones_url":"https://api.github.com/repos/praekelt/vumi-message-store/milestones{/number}","notifications_url":"https://api.github.com/repos/praekelt/vumi-message-store/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/praekelt/vumi-message-store/labels{/name}","releases_url":"https://api.github.com/repos/praekelt/vumi-message-store/releases{/id}","created_at":"2015-02-04T09:56:40Z","updated_at":"2015-03-23T12:07:57Z","pushed_at":"2015-03-23T12:08:13Z","git_url":"git://github.com/praekelt/vumi-message-store.git","ssh_url":"git@github.com:praekelt/vumi-message-store.git","clone_url":"https://github.com/praekelt/vumi-message-store.git","svn_url":"https://github.com/praekelt/vumi-message-store","homepage":null,"size":280,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":0,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7"},"html":{"href":"https://github.com/praekelt/vumi-message-store/pull/7"},"issue":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/issues/7"},"comments":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/praekelt/vumi-message-store/statuses/b1194400259144ff55cf3bb0d5d1972a5526530d"}}}}