Skip to content

Commit

Permalink
Merge pull request #8 from emre/node_picker_fix
Browse files Browse the repository at this point in the history
Exclude the nodes cant handle the node picker request
  • Loading branch information
emre committed Jan 17, 2022
2 parents 56529ad + a9c882e commit 5b52b63
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lighthive/node_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ async def compare_nodes(nodes, logger):

node_performance_results[response.url] = measured_time_in_seconds

node_performance_results_sorted = OrderedDict(sorted(node_performance_results.items(), key=lambda x: x[1]))
measurements_in_str = ""
for node, time_elapsed in node_performance_results_sorted.items():
for node, time_elapsed in node_performance_results.items():
measurements_in_str += f"{node}: {time_elapsed:.2f} [s]\n"
logger.info("Measurements: \n%s", measurements_in_str)

# remove the nodes can't handle the request
node_performance_results = {k: v for k, v in node_performance_results.items() if v != -1}

# sort the nodes by response time
node_performance_results_sorted = OrderedDict(sorted(node_performance_results.items(), key=lambda x: x[1]))

return list(node_performance_results_sorted.keys())


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='lighthive',
version='0.3.0',
version='0.3.2',
packages=find_packages('.'),
url='http://github.com/emre/lighthive',
license='MIT',
Expand Down
20 changes: 20 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,26 @@ def rpc_request(*args, **kwargs):

self.assertListEqual(nodes, ["c", "a", "b"])

@unittest.mock.patch('lighthive.node_picker.rpc_request')
def test_sort_nodes_with_negative_result(self, rpc_request_mock):

test_nodes = ["a", "b", "c", "d"]

elapsed_mock = unittest.mock.MagicMock()
elapsed_mock.total_seconds.side_effect = [2, -1, 5, 9]

def rpc_request(*args, **kwargs):
f = asyncio.Future()
f.elapsed = elapsed_mock
f.url = args[1]

return f

rpc_request_mock.side_effect = rpc_request
nodes = lighthive.node_picker.sort_nodes_by_response_time(test_nodes, unittest.mock.MagicMock())

self.assertListEqual(nodes, ["a", "c", "d"])


if __name__ == '__main__':
unittest.main()

0 comments on commit 5b52b63

Please sign in to comment.