{"action":"created","comment":{"url":"https://api.github.com/repos/uber/tchannel/pulls/comments/31099173","id":31099173,"diff_hunk":"@@ -0,0 +1,216 @@\n+# Copyright (c) 2015 Uber Technologies, Inc.\n+#\n+# Permission is hereby granted, free of charge, to any person obtaining a copy\n+# of this software and associated documentation files (the \"Software\"), to deal\n+# in the Software without restriction, including without limitation the rights\n+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n+# copies of the Software, and to permit persons to whom the Software is\n+# furnished to do so, subject to the following conditions:\n+#\n+# The above copyright notice and this permission notice shall be included in\n+# all copies or substantial portions of the Software.\n+#\n+# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n+# THE SOFTWARE.\n+\n+from __future__ import absolute_import\n+\n+import pytest\n+import tornado\n+import tornado.gen\n+from mock import patch\n+\n+from tchannel.errors import ProtocolError\n+from tchannel.errors import TChannelError\n+from tchannel.errors import TimeoutError\n+from tchannel.messages import ErrorCode\n+from tchannel.transport_header import RetryType\n+from tchannel.tornado import Request\n+from tchannel.tornado import TChannel\n+from tchannel.tornado.peer import should_retry_on_error\n+from tchannel.tornado.stream import InMemStream\n+\n+\n+@pytest.mark.gen_test(timeout=10)\n+def test_retry_timeout():\n+    endpoint = b'tchannelretrytest'\n+\n+    @tornado.gen.coroutine\n+    def handler_error(request, response, proxy):\n+        yield tornado.gen.sleep(2)\n+        response.connection.send_error(\n+            ErrorCode.busy,\n+            \"retry\",\n+            response.id,\n+        )\n+        # stop normal response streams\n+        response.set_exception(TChannelError(\"stop stream\"))\n+\n+    tchannel = TChannel()\n+    tchannel_server = None\n+    for i in range(3):\n+        tchannel_server = TChannel()\n+        tchannel_server.register(endpoint, 'raw', handler_error)\n+        tchannel_server.listen()\n+        tchannel.peers.get(tchannel_server.hostport)\n+\n+    hostport = tchannel_server.hostport\n+    with (\n+        patch(\n+            'tchannel.tornado.peer.should_retry_on_error',\n+            autospec=True)\n+    ) as mock_should_retry_on_error:\n+\n+        mock_should_retry_on_error.return_value = True\n+        with pytest.raises(TimeoutError):\n+            yield tchannel.request(\n+                hostport,\n+                score_threshold=0,\n+            ).send(\n+                endpoint,\n+                \"test\",\n+                \"test\",\n+                headers={\n+                    're': RetryType.CONNECTION_ERROR_AND_TIMEOUT\n+                },\n+                timeout_per_req=0.01,\n+                retry_times=3,\n+                retry_delay=0.01,\n+            )\n+\n+\n+@pytest.mark.gen_test\n+def test_retry_on_error_fail():\n+    endpoint = b'tchannelretrytest'\n+\n+    @tornado.gen.coroutine\n+    def handler_error(request, response, proxy):\n+        response.connection.send_error(\n+            ErrorCode.busy,\n+            \"retry\",\n+            response.id,\n+        )\n+\n+        # stop normal response streams\n+        response.set_exception(TChannelError(\"stop stream\"))\n+\n+    tchannel = TChannel()\n+    tchannel_server = None\n+    for i in range(3):\n+        tchannel_server = TChannel()\n+        tchannel_server.register(endpoint, 'raw', handler_error)\n+        tchannel_server.listen()\n+        tchannel.peers.get(tchannel_server.hostport)\n+\n+    with (\n+        patch(\n+            'tchannel.tornado.peer.should_retry_on_error',\n+            autospec=True)\n+    ) as mock_should_retry_on_error:\n+        mock_should_retry_on_error.return_value = True\n+        with pytest.raises(ProtocolError) as e:\n+            yield tchannel.request(\n+                score_threshold=0\n+            ).send(\n+                endpoint,\n+                \"test\",\n+                \"test\",\n+                headers={\n+                    're': RetryType.TIMEOUT_AND_CONNECTION_ERROR\n+                },\n+                timeout_per_req=0.01,\n+                retry_times=3,\n+                retry_delay=0.01,\n+            )\n+\n+        assert mock_should_retry_on_error.called\n+        assert mock_should_retry_on_error.call_count == Request.MAX_RETRY_TIMES\n+        assert e.value.code == ErrorCode.busy\n+\n+\n+@pytest.mark.gen_test\n+def test_retry_on_error_success():\n+\n+    endpoint = b'tchannelretrytest'\n+\n+    @tornado.gen.coroutine\n+    def handler_success(request, response, proxy):\n+        response.set_body_s(InMemStream(\"success\"))\n+\n+    @tornado.gen.coroutine\n+    def handler_error(request, response, proxy):\n+        response.connection.send_error(\n+            ErrorCode.busy,\n+            \"retry\",\n+            response.id,\n+        )\n+\n+        # stop normal response streams\n+        response.set_exception(TChannelError(\"stop stream\"))\n+\n+    tchannel = TChannel()\n+    for i in range(2):\n+        tchannel_server = TChannel()\n+        tchannel_server.register(endpoint, 'raw', handler_error)\n+        tchannel_server.listen()\n+        tchannel.peers.get(tchannel_server.hostport)\n+\n+    tchannel_success = TChannel()\n+    tchannel_success.register(endpoint, 'raw', handler_success)\n+    tchannel_success.listen()\n+    tchannel.peers.get(tchannel_success.hostport)\n+\n+    with (\n+        patch(\n+            'tchannel.tornado.peer.should_retry_on_error',\n+            autospec=True)\n+    ) as mock_should_retry_on_error:\n+        mock_should_retry_on_error.return_value = True\n+        response = yield tchannel.request(\n+            score_threshold=0\n+        ).send(\n+            endpoint,\n+            \"test\",\n+            \"test\",\n+            headers={\n+                're': RetryType.CONNECTION_ERROR_AND_TIMEOUT,\n+            },\n+            timeout_per_req=0.01,\n+            retry_times=3,\n+            retry_delay=0.01,\n+        )\n+\n+        header = yield response.get_header()\n+        body = yield response.get_body()\n+        assert body == \"success\"\n+        assert header == \"\"\n+\n+\n+@pytest.mark.gen_test\n+@pytest.mark.parametrize('retry_flag, error_code, result', [","path":"python/tests/integration/test_retry.py","position":195,"original_position":195,"commit_id":"a610ba5d2f43413012899a1cb25ae27f001bcab5","original_commit_id":"a610ba5d2f43413012899a1cb25ae27f001bcab5","user":{"login":"blampe","id":848843,"avatar_url":"https://avatars.githubusercontent.com/u/848843?v=3","gravatar_id":"","url":"https://api.github.com/users/blampe","html_url":"https://github.com/blampe","followers_url":"https://api.github.com/users/blampe/followers","following_url":"https://api.github.com/users/blampe/following{/other_user}","gists_url":"https://api.github.com/users/blampe/gists{/gist_id}","starred_url":"https://api.github.com/users/blampe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blampe/subscriptions","organizations_url":"https://api.github.com/users/blampe/orgs","repos_url":"https://api.github.com/users/blampe/repos","events_url":"https://api.github.com/users/blampe/events{/privacy}","received_events_url":"https://api.github.com/users/blampe/received_events","type":"User","site_admin":false},"body":"Nice!","created_at":"2015-05-27T02:40:44Z","updated_at":"2015-05-27T02:40:44Z","html_url":"https://github.com/uber/tchannel/pull/591#discussion_r31099173","pull_request_url":"https://api.github.com/repos/uber/tchannel/pulls/591","_links":{"self":{"href":"https://api.github.com/repos/uber/tchannel/pulls/comments/31099173"},"html":{"href":"https://github.com/uber/tchannel/pull/591#discussion_r31099173"},"pull_request":{"href":"https://api.github.com/repos/uber/tchannel/pulls/591"}}},"pull_request":{"url":"https://api.github.com/repos/uber/tchannel/pulls/591","id":35442408,"html_url":"https://github.com/uber/tchannel/pull/591","diff_url":"https://github.com/uber/tchannel/pull/591.diff","patch_url":"https://github.com/uber/tchannel/pull/591.patch","issue_url":"https://api.github.com/repos/uber/tchannel/issues/591","number":591,"state":"open","locked":false,"title":"add retry logic","user":{"login":"junchaowu","id":3731781,"avatar_url":"https://avatars.githubusercontent.com/u/3731781?v=3","gravatar_id":"","url":"https://api.github.com/users/junchaowu","html_url":"https://github.com/junchaowu","followers_url":"https://api.github.com/users/junchaowu/followers","following_url":"https://api.github.com/users/junchaowu/following{/other_user}","gists_url":"https://api.github.com/users/junchaowu/gists{/gist_id}","starred_url":"https://api.github.com/users/junchaowu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/junchaowu/subscriptions","organizations_url":"https://api.github.com/users/junchaowu/orgs","repos_url":"https://api.github.com/users/junchaowu/repos","events_url":"https://api.github.com/users/junchaowu/events{/privacy}","received_events_url":"https://api.github.com/users/junchaowu/received_events","type":"User","site_admin":false},"body":"@blampe @breerly @abhinav \r\nThe updated retry logic:\r\nuser can specify number of retries, timeout for each request, and no more total timeout.","created_at":"2015-05-14T17:26:59Z","updated_at":"2015-05-27T02:40:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"79062685712124376e956a9522f4a611552c2801","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/uber/tchannel/pulls/591/commits","review_comments_url":"https://api.github.com/repos/uber/tchannel/pulls/591/comments","review_comment_url":"https://api.github.com/repos/uber/tchannel/pulls/comments{/number}","comments_url":"https://api.github.com/repos/uber/tchannel/issues/591/comments","statuses_url":"https://api.github.com/repos/uber/tchannel/statuses/a610ba5d2f43413012899a1cb25ae27f001bcab5","head":{"label":"uber:wjc_retry","ref":"wjc_retry","sha":"a610ba5d2f43413012899a1cb25ae27f001bcab5","user":{"login":"uber","id":538264,"avatar_url":"https://avatars.githubusercontent.com/u/538264?v=3","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"repo":{"id":28419663,"name":"tchannel","full_name":"uber/tchannel","owner":{"login":"uber","id":538264,"avatar_url":"https://avatars.githubusercontent.com/u/538264?v=3","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uber/tchannel","description":"network multiplexing and framing protocol for RPC","fork":false,"url":"https://api.github.com/repos/uber/tchannel","forks_url":"https://api.github.com/repos/uber/tchannel/forks","keys_url":"https://api.github.com/repos/uber/tchannel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uber/tchannel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uber/tchannel/teams","hooks_url":"https://api.github.com/repos/uber/tchannel/hooks","issue_events_url":"https://api.github.com/repos/uber/tchannel/issues/events{/number}","events_url":"https://api.github.com/repos/uber/tchannel/events","assignees_url":"https://api.github.com/repos/uber/tchannel/assignees{/user}","branches_url":"https://api.github.com/repos/uber/tchannel/branches{/branch}","tags_url":"https://api.github.com/repos/uber/tchannel/tags","blobs_url":"https://api.github.com/repos/uber/tchannel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uber/tchannel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uber/tchannel/git/refs{/sha}","trees_url":"https://api.github.com/repos/uber/tchannel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uber/tchannel/statuses/{sha}","languages_url":"https://api.github.com/repos/uber/tchannel/languages","stargazers_url":"https://api.github.com/repos/uber/tchannel/stargazers","contributors_url":"https://api.github.com/repos/uber/tchannel/contributors","subscribers_url":"https://api.github.com/repos/uber/tchannel/subscribers","subscription_url":"https://api.github.com/repos/uber/tchannel/subscription","commits_url":"https://api.github.com/repos/uber/tchannel/commits{/sha}","git_commits_url":"https://api.github.com/repos/uber/tchannel/git/commits{/sha}","comments_url":"https://api.github.com/repos/uber/tchannel/comments{/number}","issue_comment_url":"https://api.github.com/repos/uber/tchannel/issues/comments{/number}","contents_url":"https://api.github.com/repos/uber/tchannel/contents/{+path}","compare_url":"https://api.github.com/repos/uber/tchannel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uber/tchannel/merges","archive_url":"https://api.github.com/repos/uber/tchannel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uber/tchannel/downloads","issues_url":"https://api.github.com/repos/uber/tchannel/issues{/number}","pulls_url":"https://api.github.com/repos/uber/tchannel/pulls{/number}","milestones_url":"https://api.github.com/repos/uber/tchannel/milestones{/number}","notifications_url":"https://api.github.com/repos/uber/tchannel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uber/tchannel/labels{/name}","releases_url":"https://api.github.com/repos/uber/tchannel/releases{/id}","created_at":"2014-12-23T21:06:57Z","updated_at":"2015-05-26T10:58:46Z","pushed_at":"2015-05-27T01:06:47Z","git_url":"git://github.com/uber/tchannel.git","ssh_url":"git@github.com:uber/tchannel.git","clone_url":"https://github.com/uber/tchannel.git","svn_url":"https://github.com/uber/tchannel","homepage":"","size":19583,"stargazers_count":60,"watchers_count":60,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":66,"forks":3,"open_issues":66,"watchers":60,"default_branch":"master"}},"base":{"label":"uber:master","ref":"master","sha":"a048f45ffc8e4334c23e58325daf7d5873927d86","user":{"login":"uber","id":538264,"avatar_url":"https://avatars.githubusercontent.com/u/538264?v=3","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"repo":{"id":28419663,"name":"tchannel","full_name":"uber/tchannel","owner":{"login":"uber","id":538264,"avatar_url":"https://avatars.githubusercontent.com/u/538264?v=3","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uber/tchannel","description":"network multiplexing and framing protocol for RPC","fork":false,"url":"https://api.github.com/repos/uber/tchannel","forks_url":"https://api.github.com/repos/uber/tchannel/forks","keys_url":"https://api.github.com/repos/uber/tchannel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uber/tchannel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uber/tchannel/teams","hooks_url":"https://api.github.com/repos/uber/tchannel/hooks","issue_events_url":"https://api.github.com/repos/uber/tchannel/issues/events{/number}","events_url":"https://api.github.com/repos/uber/tchannel/events","assignees_url":"https://api.github.com/repos/uber/tchannel/assignees{/user}","branches_url":"https://api.github.com/repos/uber/tchannel/branches{/branch}","tags_url":"https://api.github.com/repos/uber/tchannel/tags","blobs_url":"https://api.github.com/repos/uber/tchannel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uber/tchannel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uber/tchannel/git/refs{/sha}","trees_url":"https://api.github.com/repos/uber/tchannel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uber/tchannel/statuses/{sha}","languages_url":"https://api.github.com/repos/uber/tchannel/languages","stargazers_url":"https://api.github.com/repos/uber/tchannel/stargazers","contributors_url":"https://api.github.com/repos/uber/tchannel/contributors","subscribers_url":"https://api.github.com/repos/uber/tchannel/subscribers","subscription_url":"https://api.github.com/repos/uber/tchannel/subscription","commits_url":"https://api.github.com/repos/uber/tchannel/commits{/sha}","git_commits_url":"https://api.github.com/repos/uber/tchannel/git/commits{/sha}","comments_url":"https://api.github.com/repos/uber/tchannel/comments{/number}","issue_comment_url":"https://api.github.com/repos/uber/tchannel/issues/comments{/number}","contents_url":"https://api.github.com/repos/uber/tchannel/contents/{+path}","compare_url":"https://api.github.com/repos/uber/tchannel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uber/tchannel/merges","archive_url":"https://api.github.com/repos/uber/tchannel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uber/tchannel/downloads","issues_url":"https://api.github.com/repos/uber/tchannel/issues{/number}","pulls_url":"https://api.github.com/repos/uber/tchannel/pulls{/number}","milestones_url":"https://api.github.com/repos/uber/tchannel/milestones{/number}","notifications_url":"https://api.github.com/repos/uber/tchannel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uber/tchannel/labels{/name}","releases_url":"https://api.github.com/repos/uber/tchannel/releases{/id}","created_at":"2014-12-23T21:06:57Z","updated_at":"2015-05-26T10:58:46Z","pushed_at":"2015-05-27T01:06:47Z","git_url":"git://github.com/uber/tchannel.git","ssh_url":"git@github.com:uber/tchannel.git","clone_url":"https://github.com/uber/tchannel.git","svn_url":"https://github.com/uber/tchannel","homepage":"","size":19583,"stargazers_count":60,"watchers_count":60,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":66,"forks":3,"open_issues":66,"watchers":60,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/uber/tchannel/pulls/591"},"html":{"href":"https://github.com/uber/tchannel/pull/591"},"issue":{"href":"https://api.github.com/repos/uber/tchannel/issues/591"},"comments":{"href":"https://api.github.com/repos/uber/tchannel/issues/591/comments"},"review_comments":{"href":"https://api.github.com/repos/uber/tchannel/pulls/591/comments"},"review_comment":{"href":"https://api.github.com/repos/uber/tchannel/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/uber/tchannel/pulls/591/commits"},"statuses":{"href":"https://api.github.com/repos/uber/tchannel/statuses/a610ba5d2f43413012899a1cb25ae27f001bcab5"}}}}