From 7822f3b3caf7190a91c74f3363df87bd7f4b7d93 Mon Sep 17 00:00:00 2001 From: Stefan Schwarzer Date: Sat, 7 Jun 2014 23:32:31 +0200 Subject: [PATCH] Expect time shift in 15-minute units, not hour units (ticket #81). --- ftputil/host.py | 12 ++++++------ test/test_host.py | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/ftputil/host.py b/ftputil/host.py index aa7dacb..7323a1e 100644 --- a/ftputil/host.py +++ b/ftputil/host.py @@ -302,10 +302,10 @@ class FTPHost(object): # Use a positive value for rounding. absolute_time_shift = abs(time_shift) signum = time_shift / absolute_time_shift - # Round it to hours. This code should also work for later Python - # versions because of the explicit `int`. - absolute_rounded_time_shift = \ - int( (absolute_time_shift + 30*minute) / hour ) * hour + # Round absolute time shift to 15-minute units. + absolute_rounded_time_shift = ( + int( (absolute_time_shift + (7.5*minute)) / (15.0*minute) ) * + (15.0*minute)) # Return with correct sign. return signum * absolute_rounded_time_shift @@ -325,13 +325,13 @@ class FTPHost(object): raise ftputil.error.TimeShiftError( "time shift abs({0:.2f} s) > 1 day".format(time_shift)) # Test 2: Fail if the deviation between given time shift and - # full hours is greater than a certain limit. + # 15-minute units is greater than a certain limit. maximum_deviation = 5 * minute if abs(time_shift - self.__rounded_time_shift(time_shift)) > \ maximum_deviation: raise ftputil.error.TimeShiftError( "time shift ({0:.2f} s) deviates more than {1:d} s " - "from full hours".format( + "from 15-minute units".format( time_shift, int(maximum_deviation))) def synchronize_times(self): diff --git a/test/test_host.py b/test/test_host.py index 45497f9..2cbb34b 100644 --- a/test/test_host.py +++ b/test/test_host.py @@ -12,6 +12,7 @@ import posixpath import random import time import unittest +import warnings import ftputil import ftputil.compat @@ -380,12 +381,12 @@ class TestTimeShift(unittest.TestCase): ( 0, 0), ( 0.1, 0), ( -0.1, 0), - ( 1500, 0), - ( -1500, 0), - ( 1800, 3600), - ( -1800, -3600), - ( 2000, 3600), - ( -2000, -3600), + ( 1500, 1800), + ( -1500, -1800), + ( 1800, 1800), + ( -1800, -1800), + ( 2000, 1800), + ( -2000, -1800), ( 5*3600-100, 5*3600), (-5*3600+100, -5*3600)] for time_shift, expected_time_shift in test_data: @@ -406,23 +407,34 @@ class TestTimeShift(unittest.TestCase): 25*3600) self.assertRaises(ftputil.error.TimeShiftError, assert_time_shift, -25*3600) - # Invalid time shift (too large deviation from full hours unacceptable) + # Invalid time shift (too large deviation from 15-minute units + # is unacceptable) self.assertRaises(ftputil.error.TimeShiftError, assert_time_shift, - 10*60) + 8*60) self.assertRaises(ftputil.error.TimeShiftError, assert_time_shift, - -3600-10*60) + -3600-8*60) def test_synchronize_times(self): """Test time synchronization with server.""" host = test_base.ftp_host_factory(ftp_host_class=TimeShiftFTPHost, session_factory=TimeShiftMockSession) - # Valid time shift - host.path.set_mtime(time.time() + 3630) - host.synchronize_times() - self.assertEqual(host.time_shift(), 3600) - # Invalid time shift - host.path.set_mtime(time.time() + 3600+10*60) - self.assertRaises(ftputil.error.TimeShiftError, host.synchronize_times) + # Valid time shifts + test_data = [ + (60*60+30, 60*60), + (60*60-100, 60*60), + (30*60+100, 30*60), + (45*60-100, 45*60), + ] + for measured_time_shift, expected_time_shift in test_data: + host.path.set_mtime(time.time() + measured_time_shift) + host.synchronize_times() + self.assertEqual(host.time_shift(), expected_time_shift) + # Invalid time shifts + measured_time_shifts = [60*60+8*60, 45*60-6*60] + for measured_time_shift in measured_time_shifts: + host.path.set_mtime(time.time() + measured_time_shift) + self.assertRaises(ftputil.error.TimeShiftError, + host.synchronize_times) def test_synchronize_times_for_server_in_east(self): """Test for timestamp correction (see ticket #55).""" -- 2.34.2