Commit 2553b1c9 authored by Julien Muchembled's avatar Julien Muchembled

client: remove useless calls to time.time()

parent b70de293
...@@ -95,25 +95,17 @@ class ConnectionPool(object): ...@@ -95,25 +95,17 @@ class ConnectionPool(object):
@profiler_decorator @profiler_decorator
def notifyFailure(self, node): def notifyFailure(self, node):
self._notifyFailure(node.getUUID(), time.time() + MAX_FAILURE_AGE) self.node_failure_dict[node.getUUID()] = time.time() + MAX_FAILURE_AGE
def _notifyFailure(self, uuid, at):
self.node_failure_dict[uuid] = at
@profiler_decorator @profiler_decorator
def getCellSortKey(self, cell): def getCellSortKey(self, cell):
return self._getCellSortKey(cell.getUUID(), time.time()) uuid = cell.getUUID()
def _getCellSortKey(self, uuid, now):
if uuid in self.connection_dict: if uuid in self.connection_dict:
result = CELL_CONNECTED return CELL_CONNECTED
else:
failure = self.node_failure_dict.get(uuid) failure = self.node_failure_dict.get(uuid)
if failure is None or failure < now: if failure is None or failure < time.time():
result = CELL_GOOD return CELL_GOOD
else: return CELL_FAILED
result = CELL_FAILED
return result
@profiler_decorator @profiler_decorator
def getConnForCell(self, cell): def getConnForCell(self, cell):
......
...@@ -14,12 +14,13 @@ ...@@ -14,12 +14,13 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest import time, unittest
from mock import Mock, ReturnValues from mock import Mock, ReturnValues
from .. import NeoUnitTestBase from .. import NeoUnitTestBase
from neo.client.app import ConnectionPool from neo.client.app import ConnectionPool
from neo.client.exception import NEOStorageError from neo.client.exception import NEOStorageError
from neo.client import pool
class ConnectionPoolTests(NeoUnitTestBase): class ConnectionPoolTests(NeoUnitTestBase):
...@@ -47,16 +48,22 @@ class ConnectionPoolTests(NeoUnitTestBase): ...@@ -47,16 +48,22 @@ class ConnectionPoolTests(NeoUnitTestBase):
# TODO: test getConnForNode (requires splitting complex functionalities) # TODO: test getConnForNode (requires splitting complex functionalities)
def test_CellSortKey(self): def test_CellSortKey(self):
pool = ConnectionPool(None) cp = ConnectionPool(None)
node_uuid_1 = self.getStorageUUID() node_uuid_1 = self.getStorageUUID()
node_uuid_2 = self.getStorageUUID() node_uuid_2 = self.getStorageUUID()
node_uuid_3 = self.getStorageUUID() node_uuid_3 = self.getStorageUUID()
# We are connected to node 1 # We are connected to node 1
pool.connection_dict[node_uuid_1] = None cp.connection_dict[node_uuid_1] = None
def uuid_now(func, uuid, now):
pool.time = Mock({'time': now})
try:
return func(Mock({'getUUID': uuid}))
finally:
pool.time = time
# A connection to node 3 failed, will be forgotten at 5 # A connection to node 3 failed, will be forgotten at 5
pool._notifyFailure(node_uuid_3, 5) uuid_now(cp.notifyFailure, node_uuid_3, 5 - pool.MAX_FAILURE_AGE)
getCellSortKey = pool._getCellSortKey def getCellSortKey(*args):
return uuid_now(cp.getCellSortKey, *args)
# At 0, key values are not ambiguous # At 0, key values are not ambiguous
self.assertTrue(getCellSortKey(node_uuid_1, 0) < getCellSortKey( self.assertTrue(getCellSortKey(node_uuid_1, 0) < getCellSortKey(
node_uuid_2, 0) < getCellSortKey(node_uuid_3, 0)) node_uuid_2, 0) < getCellSortKey(node_uuid_3, 0))
......
...@@ -743,8 +743,8 @@ class NEOCluster(object): ...@@ -743,8 +743,8 @@ class NEOCluster(object):
raise raise
def extraCellSortKey(self, key): def extraCellSortKey(self, key):
return Patch(self.client.cp, _getCellSortKey=lambda orig, *args: return Patch(self.client.cp, getCellSortKey=lambda orig, cell:
(orig(*args), key(*args))) (orig(cell), key(cell)))
class NEOThreadedTest(NeoTestBase): class NEOThreadedTest(NeoTestBase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment