{"action":"created","comment":{"url":"https://api.github.com/repos/uber/tchannel/pulls/comments/39426526","id":39426526,"diff_hunk":"@@ -0,0 +1,271 @@\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+'use strict';\n+\n+var DebugLogtron = require('debug-logtron');\n+var fs = require('fs');\n+var path = require('path');\n+\n+var TChannelAsThrift = require('../../as/thrift');\n+var HyperbahnClient = require('../../hyperbahn/index.js');\n+\n+var source = fs.readFileSync(path.join(__dirname, '../../hyperbahn/hyperbahn.thrift'), 'utf8');\n+var thrift = new TChannelAsThrift({source: source});\n+\n+module.exports = runTests;\n+\n+if (require.main === module) {\n+    runTests(require('../lib/hyperbahn-cluster.js'));\n+}\n+\n+function covertHost(host) {\n+    var res = '';\n+    console.log(host.ip.ipv4);\n+    console.log((host.ip.ipv4 & 0xff000000) >> 24);\n+    res += ((host.ip.ipv4 & 0xff000000) >> 24) + '.';\n+    res += ((host.ip.ipv4 & 0xff0000) >> 16) + '.';\n+    res += ((host.ip.ipv4 & 0xff00) >> 8) + '.';\n+    res += host.ip.ipv4 & 0xff;\n+    return res + ':' + host.port;\n+}\n+\n+function runTests(HyperbahnCluster) {\n+    HyperbahnCluster.test('get no host', {\n+        size: 5\n+    }, function t(cluster, assert) {\n+        var bob = cluster.remotes.bob;\n+        var bobSub = bob.channel.makeSubChannel({\n+            serviceName: 'hyperbahn',\n+            peers: cluster.hostPortList\n+        });\n+        var request = bobSub.request({\n+            headers: {\n+                cn: 'test'\n+            },\n+            serviceName: 'hyperbahn',\n+            hasNoParent: true\n+        });\n+        thrift.send(request,\n+            'Hyperbahn::discover',\n+            null,\n+            {\n+                query: {\n+                    serviceName: 'matt'\n+                }\n+            },\n+            onResponse\n+        );\n+        function onResponse(err, res) {\n+            if (err) {\n+                assert.end(err);\n+            }\n+            assert.ok(res, 'should be a result');\n+            assert.ok(!res.ok, 'result should be not ok');\n+            assert.equals(res.body.message, 'no peer available for matt', 'error message as expected');\n+            assert.end();\n+        }\n+    });\n+\n+    HyperbahnCluster.test('get host port as expected', {\n+        size: 5\n+    }, function t(cluster, assert) {\n+        var bob = cluster.remotes.bob;\n+        var steve = cluster.remotes.steve;\n+        var steveSub = steve.channel.makeSubChannel({\n+            serviceName: 'hyperbahn',\n+            peers: cluster.hostPortList\n+        });\n+        var request = steveSub.request({\n+            headers: {\n+                cn: 'test'\n+            },\n+            serviceName: 'hyperbahn',\n+            hasNoParent: true\n+        });\n+\n+        var client = new HyperbahnClient({\n+            serviceName: 'hello-bob',\n+            callerName: 'hello-bob-test',\n+            hostPortList: cluster.hostPortList,\n+            tchannel: bob.channel,\n+            logger: DebugLogtron('hyperbahnClient')\n+        });\n+\n+        client.once('advertised', onResponse);\n+        client.advertise();\n+\n+        function onResponse() {\n+            thrift.send(request,\n+                'Hyperbahn::discover',\n+                null,\n+                {\n+                    query: {\n+                        serviceName: 'hello-bob'\n+                    }\n+                },\n+                check\n+            );\n+        }\n+\n+        function check(err, res) {\n+            if (err) {\n+                assert.end(err);\n+            }\n+            assert.ok(res, 'should be a result');\n+            assert.ok(res.ok, 'result should be ok');\n+            console.log(res.body.peers[0]);\n+            assert.equals(covertHost(res.body.peers[0]), bob.channel.hostPort,\n+                'should get the expected hostPort');\n+            client.destroy();\n+            assert.end();\n+        }\n+    });\n+\n+    HyperbahnCluster.test('malformed thrift IDL: empty serviceName', {\n+        size: 5\n+    }, function t(cluster, assert) {\n+        var bob = cluster.remotes.bob;\n+        var bobSub = bob.channel.makeSubChannel({\n+            serviceName: 'hyperbahn',\n+            peers: cluster.hostPortList\n+        });\n+        var request = bobSub.request({\n+            headers: {\n+                cn: 'test'\n+            },\n+            serviceName: 'hyperbahn',\n+            hasNoParent: true\n+        });\n+        thrift.send(request,\n+            'Hyperbahn::discover',\n+            null,\n+            {\n+                query: {\n+                    serviceName: ''\n+                }\n+            },\n+            onResponse\n+        );\n+        function onResponse(err, res) {\n+            if (err) {\n+                assert.end(err);\n+            }\n+            assert.ok(!res.ok, 'should be not ok');\n+            assert.equals(res.body.message, 'invalid service name: ', 'error message as expected');\n+            assert.end();\n+        }\n+    });\n+\n+    HyperbahnCluster.test('malformed thrift IDL: an empty body', {\n+        size: 5\n+    }, function t(cluster, assert) {\n+        var bob = cluster.remotes.bob;\n+        var bobSub = bob.channel.makeSubChannel({\n+            serviceName: 'hyperbahn',\n+            peers: cluster.hostPortList\n+        });\n+        var request = bobSub.request({\n+            headers: {\n+                cn: 'test'\n+            },\n+            serviceName: 'hyperbahn',\n+            hasNoParent: true\n+        });\n+        var badSource = fs.readFileSync(path.join(__dirname, 'bad-hyperbahn-empty-req-body.thrift'), 'utf8');\n+        var badThrift = new TChannelAsThrift({source: badSource});\n+        badThrift.send(request,\n+            'Hyperbahn::discover',\n+            null,\n+            {},\n+            onResponse\n+        );\n+        function onResponse(err, res) {\n+            assert.ok(err, 'should be error');\n+            assert.equals(err.type, 'tchannel.bad-request', 'error should be bad request');\n+            assert.end();\n+        }\n+    });\n+\n+    HyperbahnCluster.test('malformed thrift IDL: a body with a query without the serviceName field', {\n+        size: 5\n+    }, function t(cluster, assert) {\n+        var bob = cluster.remotes.bob;\n+        var bobSub = bob.channel.makeSubChannel({\n+            serviceName: 'hyperbahn',\n+            peers: cluster.hostPortList\n+        });\n+        var request = bobSub.request({\n+            headers: {\n+                cn: 'test'\n+            },\n+            serviceName: 'hyperbahn',\n+            hasNoParent: true\n+        });\n+        var badSource = fs.readFileSync(path.join(__dirname, 'bad-hyperbahn-empty-req-body.thrift'), 'utf8');\n+        var badThrift = new TChannelAsThrift({source: badSource});\n+        badThrift.send(request,\n+            'Hyperbahn::discover',\n+            null,\n+            {query: {}},\n+            onResponse\n+        );\n+        function onResponse(err, res) {\n+            assert.ok(err, 'should be error');\n+            assert.equals(err.type, 'tchannel.bad-request', 'error should be bad request');","path":"node/test/hyperbahn/hostports.js","position":232,"original_position":232,"commit_id":"64be6a5417ba58b2078256b18c490d8e8c307e0d","original_commit_id":"64be6a5417ba58b2078256b18c490d8e8c307e0d","user":{"login":"Raynos","id":479538,"avatar_url":"https://avatars.githubusercontent.com/u/479538?v=3","gravatar_id":"","url":"https://api.github.com/users/Raynos","html_url":"https://github.com/Raynos","followers_url":"https://api.github.com/users/Raynos/followers","following_url":"https://api.github.com/users/Raynos/following{/other_user}","gists_url":"https://api.github.com/users/Raynos/gists{/gist_id}","starred_url":"https://api.github.com/users/Raynos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Raynos/subscriptions","organizations_url":"https://api.github.com/users/Raynos/orgs","repos_url":"https://api.github.com/users/Raynos/repos","events_url":"https://api.github.com/users/Raynos/events{/privacy}","received_events_url":"https://api.github.com/users/Raynos/received_events","type":"User","site_admin":false},"body":"Can we add an assertion on the `err.message` to show why it failed ?","created_at":"2015-09-14T18:09:49Z","updated_at":"2015-09-14T18:09:49Z","html_url":"https://github.com/uber/tchannel/pull/1188#discussion_r39426526","pull_request_url":"https://api.github.com/repos/uber/tchannel/pulls/1188","_links":{"self":{"href":"https://api.github.com/repos/uber/tchannel/pulls/comments/39426526"},"html":{"href":"https://github.com/uber/tchannel/pull/1188#discussion_r39426526"},"pull_request":{"href":"https://api.github.com/repos/uber/tchannel/pulls/1188"}}},"pull_request":{"url":"https://api.github.com/repos/uber/tchannel/pulls/1188","id":42894262,"html_url":"https://github.com/uber/tchannel/pull/1188","diff_url":"https://github.com/uber/tchannel/pull/1188.diff","patch_url":"https://github.com/uber/tchannel/pull/1188.patch","issue_url":"https://api.github.com/repos/uber/tchannel/issues/1188","number":1188,"state":"open","locked":false,"title":"discovery api for getting hostports by service name","user":{"login":"ShanniLi","id":5845092,"avatar_url":"https://avatars.githubusercontent.com/u/5845092?v=3","gravatar_id":"","url":"https://api.github.com/users/ShanniLi","html_url":"https://github.com/ShanniLi","followers_url":"https://api.github.com/users/ShanniLi/followers","following_url":"https://api.github.com/users/ShanniLi/following{/other_user}","gists_url":"https://api.github.com/users/ShanniLi/gists{/gist_id}","starred_url":"https://api.github.com/users/ShanniLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShanniLi/subscriptions","organizations_url":"https://api.github.com/users/ShanniLi/orgs","repos_url":"https://api.github.com/users/ShanniLi/repos","events_url":"https://api.github.com/users/ShanniLi/events{/privacy}","received_events_url":"https://api.github.com/users/ShanniLi/received_events","type":"User","site_admin":false},"body":"r @jcorbin @kriskowal @anson627  \r\ncc @blampe","created_at":"2015-08-20T06:01:18Z","updated_at":"2015-09-14T18:09:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"eb98a4538e3af964b1fe09e6b6653b454f93d647","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/uber/tchannel/pulls/1188/commits","review_comments_url":"https://api.github.com/repos/uber/tchannel/pulls/1188/comments","review_comment_url":"https://api.github.com/repos/uber/tchannel/pulls/comments{/number}","comments_url":"https://api.github.com/repos/uber/tchannel/issues/1188/comments","statuses_url":"https://api.github.com/repos/uber/tchannel/statuses/64be6a5417ba58b2078256b18c490d8e8c307e0d","head":{"label":"uber:service-hostports","ref":"service-hostports","sha":"64be6a5417ba58b2078256b18c490d8e8c307e0d","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-09-14T17:41:24Z","pushed_at":"2015-09-14T18:09:26Z","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":20640,"stargazers_count":284,"watchers_count":284,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":99,"forks":34,"open_issues":99,"watchers":284,"default_branch":"master"}},"base":{"label":"uber:master","ref":"master","sha":"1c3d6af4e4e56808f1c5dae1e33192ac78ba5923","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-09-14T17:41:24Z","pushed_at":"2015-09-14T18:09:26Z","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":20640,"stargazers_count":284,"watchers_count":284,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":99,"forks":34,"open_issues":99,"watchers":284,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/uber/tchannel/pulls/1188"},"html":{"href":"https://github.com/uber/tchannel/pull/1188"},"issue":{"href":"https://api.github.com/repos/uber/tchannel/issues/1188"},"comments":{"href":"https://api.github.com/repos/uber/tchannel/issues/1188/comments"},"review_comments":{"href":"https://api.github.com/repos/uber/tchannel/pulls/1188/comments"},"review_comment":{"href":"https://api.github.com/repos/uber/tchannel/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/uber/tchannel/pulls/1188/commits"},"statuses":{"href":"https://api.github.com/repos/uber/tchannel/statuses/64be6a5417ba58b2078256b18c490d8e8c307e0d"}}}}