Get rid of timeutils.round_to_seconds()

This was introduced in 0bc4de65e3 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
This commit is contained in:
Zane Bitter 2017-05-04 17:24:59 -04:00
parent f3f05c9a3f
commit 159b73211e
4 changed files with 16 additions and 49 deletions

View File

@ -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.

View File

@ -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."""

View File

@ -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')

View File

@ -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))