From 159b73211e7669dd01ba483a8127c847fdc99deb Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Thu, 4 May 2017 17:24:59 -0400 Subject: [PATCH] Get rid of timeutils.round_to_seconds() This was introduced in 0bc4de65e3bd9dc9902e6833373c78df0badbcb4 to try to ensure that we never got a negative difference in times, which would trigger a bug in our handling of time differences. Now that bug 1688327 is fixed, there's no need for this complexity any longer. Change-Id: Iff76ff5ab2b1d642d464f111e78e248faf6ac77c Related-Bug: #1688327 Related-Bug: #1493166 --- heat/common/timeutils.py | 10 ---------- heat/engine/stack.py | 8 ++------ heat/tests/test_stack.py | 32 ++++++++++++++------------------ heat/tests/test_timeutils.py | 15 --------------- 4 files changed, 16 insertions(+), 49 deletions(-) diff --git a/heat/common/timeutils.py b/heat/common/timeutils.py index 9e4d0b8728..570bbf39b2 100644 --- a/heat/common/timeutils.py +++ b/heat/common/timeutils.py @@ -13,7 +13,6 @@ """Utilities for handling ISO 8601 duration format.""" -import datetime import random import re import time @@ -78,15 +77,6 @@ def retry_backoff_delay(attempt, scale_factor=1.0, jitter_max=0.0): return exp + random.random() * jitter_max -def round_to_seconds(dt): - """Round a datetime to the nearest second.""" - rounding = 0 - if dt.microsecond >= 500000: - rounding = 1 - return dt + datetime.timedelta(0, rounding, - -dt.microsecond) - - def isotime(at): """Stringify UTC time in ISO 8601 format. diff --git a/heat/engine/stack.py b/heat/engine/stack.py index 41650ef957..f7361d236d 100644 --- a/heat/engine/stack.py +++ b/heat/engine/stack.py @@ -13,7 +13,6 @@ import collections import copy -import datetime import eventlet import functools import itertools @@ -35,7 +34,6 @@ from heat.common import exception from heat.common.i18n import _ from heat.common import identifier from heat.common import lifecycle_plugin_utils -from heat.common import timeutils from heat.engine import dependencies from heat.engine import environment from heat.engine import event @@ -2093,10 +2091,8 @@ class Stack(collections.Mapping): def time_elapsed(self): """Time elapsed in seconds since the stack operation started.""" - start_time = timeutils.round_to_seconds(self.updated_time or - self.created_time) - nowish = timeutils.round_to_seconds(datetime.datetime.utcnow()) - return int((nowish - start_time).total_seconds()) + start_time = self.updated_time or self.created_time + return (oslo_timeutils.utcnow() - start_time).total_seconds() def time_remaining(self): """Time left before stack times out.""" diff --git a/heat/tests/test_stack.py b/heat/tests/test_stack.py index 6b72e74162..63176eceab 100644 --- a/heat/tests/test_stack.py +++ b/heat/tests/test_stack.py @@ -110,47 +110,43 @@ class StackTest(common.HeatTestCase): timeout_mins=10) self.assertEqual(600, self.stack.timeout_secs()) - @mock.patch.object(stack, 'datetime') - def test_time_elapsed(self, mock_dt): + @mock.patch.object(stack, 'oslo_timeutils') + def test_time_elapsed(self, mock_tu): self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl) # dummy create time 10:00:00 self.stack.created_time = datetime.datetime(2015, 7, 27, 10, 0, 0) # mock utcnow set to 10:10:00 (600s offset) - mock_dt.datetime.utcnow.return_value = datetime.datetime(2015, 7, 27, - 10, 10, 0) + mock_tu.utcnow.return_value = datetime.datetime(2015, 7, 27, 10, 10, 0) self.assertEqual(600, self.stack.time_elapsed()) - @mock.patch.object(stack, 'datetime') - def test_time_elapsed_negative(self, mock_dt): + @mock.patch.object(stack, 'oslo_timeutils') + def test_time_elapsed_negative(self, mock_tu): self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl) # dummy create time 10:00:00 self.stack.created_time = datetime.datetime(2015, 7, 27, 10, 0, 0) # mock utcnow set to 09:59:50 (-10s offset) - mock_dt.datetime.utcnow.return_value = datetime.datetime(2015, 7, 27, - 9, 59, 50) + mock_tu.utcnow.return_value = datetime.datetime(2015, 7, 27, 9, 59, 50) self.assertEqual(-10, self.stack.time_elapsed()) - @mock.patch.object(stack, 'datetime') - def test_time_elapsed_ms(self, mock_dt): + @mock.patch.object(stack, 'oslo_timeutils') + def test_time_elapsed_ms(self, mock_tu): self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl) # dummy create time 10:00:00 self.stack.created_time = datetime.datetime(2015, 7, 27, 10, 5, 0) # mock utcnow set to microsecond offset - mock_dt.datetime.utcnow.return_value = datetime.datetime(2015, 7, 27, - 10, 4, 59, - 750000) - self.assertEqual(0, self.stack.time_elapsed()) + mock_tu.utcnow.return_value = datetime.datetime(2015, 7, 27, + 10, 4, 59, 750000) + self.assertEqual(-0.25, self.stack.time_elapsed()) - @mock.patch.object(stack, 'datetime') - def test_time_elapsed_with_updated_time(self, mock_dt): + @mock.patch.object(stack, 'oslo_timeutils') + def test_time_elapsed_with_updated_time(self, mock_tu): self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl) # dummy create time 10:00:00 self.stack.created_time = datetime.datetime(2015, 7, 27, 10, 0, 0) # dummy updated time 11:00:00; should consider this not created_time self.stack.updated_time = datetime.datetime(2015, 7, 27, 11, 0, 0) # mock utcnow set to 11:10:00 (600s offset) - mock_dt.datetime.utcnow.return_value = datetime.datetime(2015, 7, 27, - 11, 10, 0) + mock_tu.utcnow.return_value = datetime.datetime(2015, 7, 27, 11, 10, 0) self.assertEqual(600, self.stack.time_elapsed()) @mock.patch.object(stack.Stack, 'time_elapsed') diff --git a/heat/tests/test_timeutils.py b/heat/tests/test_timeutils.py index b3a06a9b08..6779513e00 100644 --- a/heat/tests/test_timeutils.py +++ b/heat/tests/test_timeutils.py @@ -11,7 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime from testtools import matchers from heat.common import timeutils as util @@ -155,17 +154,3 @@ class RetryBackoffJitterTest(common.HeatTestCase): self.attempt, self.scale_factor, self.jitter_max) self.assertThat(delay, matchers.GreaterThan(self.delay_from)) self.assertThat(delay, matchers.LessThan(self.delay_to)) - - -class RoundToSecondsTest(common.HeatTestCase): - - scenarios = [('down', dict(in_secs=5, in_ms=12345, out_secs=5)), - ('up', dict(in_secs=5, in_ms=501000, out_secs=6)), - ('same', dict(in_secs=5, in_ms=0, out_secs=5))] - - def test_rounding(self): - inp = datetime.datetime(2015, 7, 27, 4, - 34, self.in_secs, self.in_ms) - exp = datetime.datetime(2015, 7, 27, 4, - 34, self.out_secs, 0) - self.assertEqual(exp, util.round_to_seconds(inp))