1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
# Copyright (c) 2010-2023 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for swift.common.utils.timestamp"""
import random
import time
import unittest
from unittest import mock
from swift.common.utils import timestamp
class TestTimestamp(unittest.TestCase):
"""Tests for swift.common.utils.timestamp.Timestamp"""
def test_invalid_input(self):
with self.assertRaises(ValueError):
timestamp.Timestamp(time.time(), offset=-1)
with self.assertRaises(ValueError):
timestamp.Timestamp('123.456_78_90')
def test_invalid_string_conversion(self):
t = timestamp.Timestamp.now()
self.assertRaises(TypeError, str, t)
def test_offset_limit(self):
t = 1417462430.78693
# can't have a offset above MAX_OFFSET
with self.assertRaises(ValueError):
timestamp.Timestamp(t, offset=timestamp.MAX_OFFSET + 1)
# exactly max offset is fine
ts = timestamp.Timestamp(t, offset=timestamp.MAX_OFFSET)
self.assertEqual(ts.internal, '1417462430.78693_ffffffffffffffff')
# but you can't offset it further
with self.assertRaises(ValueError):
timestamp.Timestamp(ts.internal, offset=1)
# unless you start below it
ts = timestamp.Timestamp(t, offset=timestamp.MAX_OFFSET - 1)
self.assertEqual(timestamp.Timestamp(ts.internal, offset=1),
'1417462430.78693_ffffffffffffffff')
def test_normal_format_no_offset(self):
expected = '1402436408.91203'
test_values = (
'1402436408.91203',
'1402436408.91203_00000000',
'1402436408.912030000',
'1402436408.912030000_0000000000000',
'000001402436408.912030000',
'000001402436408.912030000_0000000000',
1402436408.91203,
1402436408.912029,
1402436408.9120300000000000,
1402436408.91202999999999999,
timestamp.Timestamp(1402436408.91203),
timestamp.Timestamp(1402436408.91203, offset=0),
timestamp.Timestamp(1402436408.912029),
timestamp.Timestamp(1402436408.912029, offset=0),
timestamp.Timestamp('1402436408.91203'),
timestamp.Timestamp('1402436408.91203', offset=0),
timestamp.Timestamp('1402436408.91203_00000000'),
timestamp.Timestamp('1402436408.91203_00000000', offset=0),
)
for value in test_values:
ts = timestamp.Timestamp(value)
self.assertEqual(ts.normal, expected)
# timestamp instance can also compare to string or float
self.assertEqual(ts, expected)
self.assertEqual(ts, float(expected))
self.assertEqual(ts, timestamp.normalize_timestamp(expected))
def test_isoformat(self):
expected = '2014-06-10T22:47:32.054580'
test_values = (
'1402440452.05458',
'1402440452.054579',
'1402440452.05458_00000000',
'1402440452.054579_00000000',
'1402440452.054580000',
'1402440452.054579999',
'1402440452.054580000_0000000000000',
'1402440452.054579999_0000ff00',
'000001402440452.054580000',
'000001402440452.0545799',
'000001402440452.054580000_0000000000',
'000001402440452.054579999999_00000fffff',
1402440452.05458,
1402440452.054579,
1402440452.0545800000000000,
1402440452.054579999,
timestamp.Timestamp(1402440452.05458),
timestamp.Timestamp(1402440452.0545799),
timestamp.Timestamp(1402440452.05458, offset=0),
timestamp.Timestamp(1402440452.05457999999, offset=0),
timestamp.Timestamp(1402440452.05458, offset=100),
timestamp.Timestamp(1402440452.054579, offset=100),
timestamp.Timestamp('1402440452.05458'),
timestamp.Timestamp('1402440452.054579999'),
timestamp.Timestamp('1402440452.05458', offset=0),
timestamp.Timestamp('1402440452.054579', offset=0),
timestamp.Timestamp('1402440452.05458', offset=300),
timestamp.Timestamp('1402440452.05457999', offset=300),
timestamp.Timestamp('1402440452.05458_00000000'),
timestamp.Timestamp('1402440452.05457999_00000000'),
timestamp.Timestamp('1402440452.05458_00000000', offset=0),
timestamp.Timestamp('1402440452.05457999_00000aaa', offset=0),
timestamp.Timestamp('1402440452.05458_00000000', offset=400),
timestamp.Timestamp('1402440452.054579_0a', offset=400),
)
for value in test_values:
self.assertEqual(timestamp.Timestamp(value).isoformat, expected)
expected = '1970-01-01T00:00:00.000000'
test_values = (
'0',
'0000000000.00000',
'0000000000.00000_ffffffffffff',
0,
0.0,
)
for value in test_values:
self.assertEqual(timestamp.Timestamp(value).isoformat, expected)
def test_from_isoformat(self):
ts = timestamp.Timestamp.from_isoformat('2014-06-10T22:47:32.054580')
self.assertIsInstance(ts, timestamp.Timestamp)
self.assertEqual(1402440452.05458, float(ts))
self.assertEqual('2014-06-10T22:47:32.054580', ts.isoformat)
ts = timestamp.Timestamp.from_isoformat('1970-01-01T00:00:00.000000')
self.assertIsInstance(ts, timestamp.Timestamp)
self.assertEqual(0.0, float(ts))
self.assertEqual('1970-01-01T00:00:00.000000', ts.isoformat)
ts = timestamp.Timestamp(1402440452.05458)
self.assertIsInstance(ts, timestamp.Timestamp)
self.assertEqual(ts, timestamp.Timestamp.from_isoformat(ts.isoformat))
def test_ceil(self):
self.assertEqual(0.0, timestamp.Timestamp(0).ceil())
self.assertEqual(1.0, timestamp.Timestamp(0.00001).ceil())
self.assertEqual(0.0, timestamp.Timestamp(0.000001).ceil())
self.assertEqual(1.0, timestamp.Timestamp(0.000006).ceil())
self.assertEqual(12345678.0, timestamp.Timestamp(12345678.0).ceil())
self.assertEqual(12345678.0,
timestamp.Timestamp(12345678.000001).ceil())
self.assertEqual(12345679.0,
timestamp.Timestamp(12345678.000006).ceil())
def test_not_equal(self):
ts = '1402436408.91203_0000000000000001'
test_values = (
timestamp.Timestamp('1402436408.91203_0000000000000002'),
timestamp.Timestamp('1402436408.91203'),
timestamp.Timestamp(1402436408.91203),
timestamp.Timestamp(1402436408.91204),
timestamp.Timestamp(1402436408.91203, offset=0),
timestamp.Timestamp(1402436408.91203, offset=2),
)
for value in test_values:
self.assertTrue(value != ts)
self.assertIs(True, timestamp.Timestamp(ts) == ts) # sanity
self.assertIs(False,
timestamp.Timestamp(ts) != timestamp.Timestamp(ts))
self.assertIs(False, timestamp.Timestamp(ts) != ts)
self.assertIs(False, timestamp.Timestamp(ts) is None)
self.assertIs(True, timestamp.Timestamp(ts) is not None)
def test_no_force_internal_no_offset(self):
"""Test that internal is the same as normal with no offset"""
with mock.patch('swift.common.utils.timestamp.FORCE_INTERNAL',
new=False):
self.assertEqual(timestamp.Timestamp(0).internal,
'0000000000.00000')
self.assertEqual(timestamp.Timestamp(1402437380.58186).internal,
'1402437380.58186')
self.assertEqual(timestamp.Timestamp(1402437380.581859).internal,
'1402437380.58186')
self.assertEqual(timestamp.Timestamp(0).internal,
timestamp.normalize_timestamp(0))
def test_no_force_internal_with_offset(self):
"""Test that internal always includes the offset if significant"""
with mock.patch('swift.common.utils.timestamp.FORCE_INTERNAL',
new=False):
self.assertEqual(timestamp.Timestamp(0, offset=1).internal,
'0000000000.00000_0000000000000001')
self.assertEqual(
timestamp.Timestamp(1402437380.58186, offset=16).internal,
'1402437380.58186_0000000000000010')
self.assertEqual(
timestamp.Timestamp(1402437380.581859, offset=240).internal,
'1402437380.58186_00000000000000f0')
self.assertEqual(
timestamp.Timestamp('1402437380.581859_00000001',
offset=240).internal,
'1402437380.58186_00000000000000f1')
def test_force_internal(self):
"""Test that internal always includes the offset if forced"""
with mock.patch('swift.common.utils.timestamp.FORCE_INTERNAL',
new=True):
self.assertEqual(timestamp.Timestamp(0).internal,
'0000000000.00000_0000000000000000')
self.assertEqual(timestamp.Timestamp(1402437380.58186).internal,
'1402437380.58186_0000000000000000')
self.assertEqual(timestamp.Timestamp(1402437380.581859).internal,
'1402437380.58186_0000000000000000')
self.assertEqual(timestamp.Timestamp(0, offset=1).internal,
'0000000000.00000_0000000000000001')
self.assertEqual(
timestamp.Timestamp(1402437380.58186, offset=16).internal,
'1402437380.58186_0000000000000010')
self.assertEqual(
timestamp.Timestamp(1402437380.581859, offset=16).internal,
'1402437380.58186_0000000000000010')
def test_internal_format_no_offset(self):
expected = '1402436408.91203_0000000000000000'
test_values = (
'1402436408.91203',
'1402436408.91203_00000000',
'1402436408.912030000',
'1402436408.912030000_0000000000000',
'000001402436408.912030000',
'000001402436408.912030000_0000000000',
1402436408.91203,
1402436408.9120300000000000,
1402436408.912029,
1402436408.912029999999999999,
timestamp.Timestamp(1402436408.91203),
timestamp.Timestamp(1402436408.91203, offset=0),
timestamp.Timestamp(1402436408.912029),
timestamp.Timestamp(1402436408.91202999999999999, offset=0),
timestamp.Timestamp('1402436408.91203'),
timestamp.Timestamp('1402436408.91203', offset=0),
timestamp.Timestamp('1402436408.912029'),
timestamp.Timestamp('1402436408.912029', offset=0),
timestamp.Timestamp('1402436408.912029999999999'),
timestamp.Timestamp('1402436408.912029999999999', offset=0),
)
for value in test_values:
# timestamp instance is always equivalent
self.assertEqual(timestamp.Timestamp(value), expected)
if timestamp.FORCE_INTERNAL:
# the FORCE_INTERNAL flag makes the internal format always
# include the offset portion of the timestamp even when it's
# not significant and would be bad during upgrades
self.assertEqual(timestamp.Timestamp(value).internal, expected)
else:
# unless we FORCE_INTERNAL, when there's no offset the
# internal format is equivalent to the normalized format
self.assertEqual(timestamp.Timestamp(value).internal,
'1402436408.91203')
def test_internal_format_with_offset(self):
expected = '1402436408.91203_00000000000000f0'
test_values = (
'1402436408.91203_000000f0',
u'1402436408.91203_000000f0',
b'1402436408.91203_000000f0',
'1402436408.912030000_0000000000f0',
'1402436408.912029_000000f0',
'1402436408.91202999999_0000000000f0',
'000001402436408.912030000_000000000f0',
'000001402436408.9120299999_000000000f0',
timestamp.Timestamp(1402436408.91203, offset=240),
timestamp.Timestamp(1402436408.912029, offset=240),
timestamp.Timestamp('1402436408.91203', offset=240),
timestamp.Timestamp('1402436408.91203_00000000', offset=240),
timestamp.Timestamp('1402436408.91203_0000000f', offset=225),
timestamp.Timestamp('1402436408.9120299999', offset=240),
timestamp.Timestamp('1402436408.9120299999_00000000', offset=240),
timestamp.Timestamp('1402436408.9120299999_00000010', offset=224),
)
for value in test_values:
ts = timestamp.Timestamp(value)
self.assertEqual(ts.internal, expected)
# can compare with offset if the string is internalized
self.assertEqual(ts, expected)
# if comparison value only includes the normalized portion and the
# timestamp includes an offset, it is considered greater
normal = timestamp.Timestamp(expected).normal
self.assertTrue(ts > normal,
'%r is not bigger than %r given %r' % (
ts, normal, value))
self.assertTrue(ts > float(normal),
'%r is not bigger than %f given %r' % (
ts, float(normal), value))
def test_short_format_with_offset(self):
expected = '1402436408.91203_f0'
ts = timestamp.Timestamp(1402436408.91203, 0xf0)
self.assertEqual(expected, ts.short)
expected = '1402436408.91203'
ts = timestamp.Timestamp(1402436408.91203)
self.assertEqual(expected, ts.short)
def test_raw(self):
expected = 140243640891203
ts = timestamp.Timestamp(1402436408.91203)
self.assertEqual(expected, ts.raw)
self.assertEqual('1402436408.91203', ts.normal)
# 'raw' does not include offset
ts = timestamp.Timestamp(1402436408.91203, 0xf0)
self.assertEqual(expected, ts.raw)
expected = 175507756652338
ts = timestamp.Timestamp(1755077566.523385)
self.assertEqual(expected, ts.raw)
self.assertEqual('1755077566.52338', ts.normal)
def test_delta(self):
def _assertWithinBounds(expected, timestamp):
tolerance = 0.00001
minimum = expected - tolerance
maximum = expected + tolerance
self.assertTrue(float(timestamp) > minimum)
self.assertTrue(float(timestamp) < maximum)
ts = timestamp.Timestamp(1402436408.91203, delta=100)
_assertWithinBounds(1402436408.91303, ts)
self.assertEqual(140243640891303, ts.raw)
ts = timestamp.Timestamp(1402436408.91203, delta=-100)
_assertWithinBounds(1402436408.91103, ts)
self.assertEqual(140243640891103, ts.raw)
ts = timestamp.Timestamp(1402436408.91203, delta=0)
_assertWithinBounds(1402436408.91203, ts)
self.assertEqual(140243640891203, ts.raw)
# delta is independent of offset
ts = timestamp.Timestamp(1402436408.91203, offset=42, delta=100)
self.assertEqual(140243640891303, ts.raw)
self.assertEqual(42, ts.offset)
# cannot go negative
self.assertRaises(ValueError, timestamp.Timestamp, 1402436408.91203,
delta=-140243640891203)
def test_int(self):
expected = 1402437965
test_values = (
'1402437965.91203',
'1402437965.91203_00000000',
'1402437965.912030000',
'1402437965.912030000_0000000000000',
'000001402437965.912030000',
'000001402437965.912030000_0000000000',
1402437965.91203,
1402437965.9120300000000000,
1402437965.912029,
1402437965.912029999999999999,
timestamp.Timestamp(1402437965.91203),
timestamp.Timestamp(1402437965.91203, offset=0),
timestamp.Timestamp(1402437965.91203, offset=500),
timestamp.Timestamp(1402437965.912029),
timestamp.Timestamp(1402437965.91202999999999999, offset=0),
timestamp.Timestamp(1402437965.91202999999999999, offset=300),
timestamp.Timestamp('1402437965.91203'),
timestamp.Timestamp('1402437965.91203', offset=0),
timestamp.Timestamp('1402437965.91203', offset=400),
timestamp.Timestamp('1402437965.912029'),
timestamp.Timestamp('1402437965.912029', offset=0),
timestamp.Timestamp('1402437965.912029', offset=200),
timestamp.Timestamp('1402437965.912029999999999'),
timestamp.Timestamp('1402437965.912029999999999', offset=0),
timestamp.Timestamp('1402437965.912029999999999', offset=100),
)
for value in test_values:
ts = timestamp.Timestamp(value)
self.assertEqual(int(ts), expected)
self.assertTrue(ts > expected)
def test_float(self):
expected = 1402438115.91203
test_values = (
'1402438115.91203',
'1402438115.91203_00000000',
'1402438115.912030000',
'1402438115.912030000_0000000000000',
'000001402438115.912030000',
'000001402438115.912030000_0000000000',
1402438115.91203,
1402438115.9120300000000000,
1402438115.912029,
1402438115.912029999999999999,
timestamp.Timestamp(1402438115.91203),
timestamp.Timestamp(1402438115.91203, offset=0),
timestamp.Timestamp(1402438115.91203, offset=500),
timestamp.Timestamp(1402438115.912029),
timestamp.Timestamp(1402438115.91202999999999999, offset=0),
timestamp.Timestamp(1402438115.91202999999999999, offset=300),
timestamp.Timestamp('1402438115.91203'),
timestamp.Timestamp('1402438115.91203', offset=0),
timestamp.Timestamp('1402438115.91203', offset=400),
timestamp.Timestamp('1402438115.912029'),
timestamp.Timestamp('1402438115.912029', offset=0),
timestamp.Timestamp('1402438115.912029', offset=200),
timestamp.Timestamp('1402438115.912029999999999'),
timestamp.Timestamp('1402438115.912029999999999', offset=0),
timestamp.Timestamp('1402438115.912029999999999', offset=100),
)
tolerance = 0.00001
minimum = expected - tolerance
maximum = expected + tolerance
for value in test_values:
ts = timestamp.Timestamp(value)
self.assertTrue(float(ts) > minimum,
'%f is not bigger than %f given %r' % (
ts, minimum, value))
self.assertTrue(float(ts) < maximum,
'%f is not smaller than %f given %r' % (
ts, maximum, value))
# direct comparison of timestamp works too
self.assertTrue(ts > minimum,
'%s is not bigger than %f given %r' % (
ts.normal, minimum, value))
self.assertTrue(ts < maximum,
'%s is not smaller than %f given %r' % (
ts.normal, maximum, value))
# ... even against strings
self.assertTrue(ts > '%f' % minimum,
'%s is not bigger than %s given %r' % (
ts.normal, minimum, value))
self.assertTrue(ts < '%f' % maximum,
'%s is not smaller than %s given %r' % (
ts.normal, maximum, value))
def test_false(self):
self.assertFalse(timestamp.Timestamp(0))
self.assertFalse(timestamp.Timestamp(0, offset=0))
self.assertFalse(timestamp.Timestamp('0'))
self.assertFalse(timestamp.Timestamp('0', offset=0))
self.assertFalse(timestamp.Timestamp(0.0))
self.assertFalse(timestamp.Timestamp(0.0, offset=0))
self.assertFalse(timestamp.Timestamp('0.0'))
self.assertFalse(timestamp.Timestamp('0.0', offset=0))
self.assertFalse(timestamp.Timestamp(00000000.00000000))
self.assertFalse(timestamp.Timestamp(00000000.00000000, offset=0))
self.assertFalse(timestamp.Timestamp('00000000.00000000'))
self.assertFalse(timestamp.Timestamp('00000000.00000000', offset=0))
def test_true(self):
self.assertTrue(timestamp.Timestamp(1))
self.assertTrue(timestamp.Timestamp(1, offset=1))
self.assertTrue(timestamp.Timestamp(0, offset=1))
self.assertTrue(timestamp.Timestamp('1'))
self.assertTrue(timestamp.Timestamp('1', offset=1))
self.assertTrue(timestamp.Timestamp('0', offset=1))
self.assertTrue(timestamp.Timestamp(1.1))
self.assertTrue(timestamp.Timestamp(1.1, offset=1))
self.assertTrue(timestamp.Timestamp(0.0, offset=1))
self.assertTrue(timestamp.Timestamp('1.1'))
self.assertTrue(timestamp.Timestamp('1.1', offset=1))
self.assertTrue(timestamp.Timestamp('0.0', offset=1))
self.assertTrue(timestamp.Timestamp(11111111.11111111))
self.assertTrue(timestamp.Timestamp(11111111.11111111, offset=1))
self.assertTrue(timestamp.Timestamp(00000000.00000000, offset=1))
self.assertTrue(timestamp.Timestamp('11111111.11111111'))
self.assertTrue(timestamp.Timestamp('11111111.11111111', offset=1))
self.assertTrue(timestamp.Timestamp('00000000.00000000', offset=1))
def test_greater_no_offset(self):
now = time.time()
older = now - 1
ts = timestamp.Timestamp(now)
test_values = (
0, '0', 0.0, '0.0', '0000.0000', '000.000_000',
1, '1', 1.1, '1.1', '1111.1111', '111.111_111',
1402443112.213252, '1402443112.213252', '1402443112.213252_ffff',
older, '%f' % older, '%f_0000ffff' % older,
)
for value in test_values:
other = timestamp.Timestamp(value)
self.assertNotEqual(ts, other) # sanity
self.assertTrue(ts > value,
'%r is not greater than %r given %r' % (
ts, value, value))
self.assertTrue(ts > other,
'%r is not greater than %r given %r' % (
ts, other, value))
self.assertTrue(ts > other.normal,
'%r is not greater than %r given %r' % (
ts, other.normal, value))
self.assertTrue(ts > other.internal,
'%r is not greater than %r given %r' % (
ts, other.internal, value))
self.assertTrue(ts > float(other),
'%r is not greater than %r given %r' % (
ts, float(other), value))
self.assertTrue(ts > int(other),
'%r is not greater than %r given %r' % (
ts, int(other), value))
def _test_greater_with_offset(self, now, test_values):
for offset in range(1, 1000, 100):
ts = timestamp.Timestamp(now, offset=offset)
for value in test_values:
other = timestamp.Timestamp(value)
self.assertNotEqual(ts, other) # sanity
self.assertTrue(ts > value,
'%r is not greater than %r given %r' % (
ts, value, value))
self.assertTrue(ts > other,
'%r is not greater than %r given %r' % (
ts, other, value))
self.assertTrue(ts > other.normal,
'%r is not greater than %r given %r' % (
ts, other.normal, value))
self.assertTrue(ts > other.internal,
'%r is not greater than %r given %r' % (
ts, other.internal, value))
self.assertTrue(ts > float(other),
'%r is not greater than %r given %r' % (
ts, float(other), value))
self.assertTrue(ts > int(other),
'%r is not greater than %r given %r' % (
ts, int(other), value))
def test_greater_with_offset(self):
# Part 1: use the natural time of the Python. This is deliciously
# unpredictable, but completely legitimate and realistic. Finds bugs!
now = time.time()
older = now - 1
test_values = (
0, '0', 0.0, '0.0', '0000.0000', '000.000_000',
1, '1', 1.1, '1.1', '1111.1111', '111.111_111',
1402443346.935174, '1402443346.93517', '1402443346.935169_ffff',
older, now,
)
self._test_greater_with_offset(now, test_values)
# Part 2: Same as above, but with fixed time values that reproduce
# specific corner cases.
now = 1519830570.6949348
older = now - 1
test_values = (
0, '0', 0.0, '0.0', '0000.0000', '000.000_000',
1, '1', 1.1, '1.1', '1111.1111', '111.111_111',
1402443346.935174, '1402443346.93517', '1402443346.935169_ffff',
older, now,
)
self._test_greater_with_offset(now, test_values)
# Part 3: The '%f' problem. Timestamps cannot be converted to %f
# strings, then back to timestamps, then compared with originals.
# You can only "import" a floating point representation once.
now = 1519830570.6949348
now = float('%f' % now)
older = now - 1
test_values = (
0, '0', 0.0, '0.0', '0000.0000', '000.000_000',
1, '1', 1.1, '1.1', '1111.1111', '111.111_111',
older, '%f' % older, '%f_0000ffff' % older,
now, '%f' % now, '%s_00000000' % now,
)
self._test_greater_with_offset(now, test_values)
def test_smaller_no_offset(self):
now = time.time()
newer = now + 1
ts = timestamp.Timestamp(now)
test_values = (
9999999999.99999, '9999999999.99999', '9999999999.99999_ffff',
newer, '%f' % newer, '%f_0000ffff' % newer,
)
for value in test_values:
other = timestamp.Timestamp(value)
self.assertNotEqual(ts, other) # sanity
self.assertTrue(ts < value,
'%r is not smaller than %r given %r' % (
ts, value, value))
self.assertTrue(ts < other,
'%r is not smaller than %r given %r' % (
ts, other, value))
self.assertTrue(ts < other.normal,
'%r is not smaller than %r given %r' % (
ts, other.normal, value))
self.assertTrue(ts < other.internal,
'%r is not smaller than %r given %r' % (
ts, other.internal, value))
self.assertTrue(ts < float(other),
'%r is not smaller than %r given %r' % (
ts, float(other), value))
self.assertTrue(ts < int(other),
'%r is not smaller than %r given %r' % (
ts, int(other), value))
def test_smaller_with_offset(self):
now = time.time()
newer = now + 1
test_values = (
9999999999.99999, '9999999999.99999', '9999999999.99999_ffff',
newer, '%f' % newer, '%f_0000ffff' % newer,
)
for offset in range(1, 1000, 100):
ts = timestamp.Timestamp(now, offset=offset)
for value in test_values:
other = timestamp.Timestamp(value)
self.assertNotEqual(ts, other) # sanity
self.assertTrue(ts < value,
'%r is not smaller than %r given %r' % (
ts, value, value))
self.assertTrue(ts < other,
'%r is not smaller than %r given %r' % (
ts, other, value))
self.assertTrue(ts < other.normal,
'%r is not smaller than %r given %r' % (
ts, other.normal, value))
self.assertTrue(ts < other.internal,
'%r is not smaller than %r given %r' % (
ts, other.internal, value))
self.assertTrue(ts < float(other),
'%r is not smaller than %r given %r' % (
ts, float(other), value))
self.assertTrue(ts < int(other),
'%r is not smaller than %r given %r' % (
ts, int(other), value))
def test_cmp_with_none(self):
self.assertGreater(timestamp.Timestamp(0), None)
self.assertGreater(timestamp.Timestamp(1.0), None)
self.assertGreater(timestamp.Timestamp(1.0, 42), None)
def test_ordering(self):
given = [
'1402444820.62590_000000000000000a',
'1402444820.62589_0000000000000001',
'1402444821.52589_0000000000000004',
'1402444920.62589_0000000000000004',
'1402444821.62589_000000000000000a',
'1402444821.72589_000000000000000a',
'1402444920.62589_0000000000000002',
'1402444820.62589_0000000000000002',
'1402444820.62589_000000000000000a',
'1402444820.62590_0000000000000004',
'1402444920.62589_000000000000000a',
'1402444820.62590_0000000000000002',
'1402444821.52589_0000000000000002',
'1402444821.52589_0000000000000000',
'1402444920.62589',
'1402444821.62589_0000000000000004',
'1402444821.72589_0000000000000001',
'1402444820.62590',
'1402444820.62590_0000000000000001',
'1402444820.62589_0000000000000004',
'1402444821.72589_0000000000000000',
'1402444821.52589_000000000000000a',
'1402444821.72589_0000000000000004',
'1402444821.62589',
'1402444821.52589_0000000000000001',
'1402444821.62589_0000000000000001',
'1402444821.62589_0000000000000002',
'1402444821.72589_0000000000000002',
'1402444820.62589',
'1402444920.62589_0000000000000001']
expected = [
'1402444820.62589',
'1402444820.62589_0000000000000001',
'1402444820.62589_0000000000000002',
'1402444820.62589_0000000000000004',
'1402444820.62589_000000000000000a',
'1402444820.62590',
'1402444820.62590_0000000000000001',
'1402444820.62590_0000000000000002',
'1402444820.62590_0000000000000004',
'1402444820.62590_000000000000000a',
'1402444821.52589',
'1402444821.52589_0000000000000001',
'1402444821.52589_0000000000000002',
'1402444821.52589_0000000000000004',
'1402444821.52589_000000000000000a',
'1402444821.62589',
'1402444821.62589_0000000000000001',
'1402444821.62589_0000000000000002',
'1402444821.62589_0000000000000004',
'1402444821.62589_000000000000000a',
'1402444821.72589',
'1402444821.72589_0000000000000001',
'1402444821.72589_0000000000000002',
'1402444821.72589_0000000000000004',
'1402444821.72589_000000000000000a',
'1402444920.62589',
'1402444920.62589_0000000000000001',
'1402444920.62589_0000000000000002',
'1402444920.62589_0000000000000004',
'1402444920.62589_000000000000000a',
]
# less visual version
"""
now = time.time()
given = [
timestamp.Timestamp(now + i, offset=offset).internal
for i in (0, 0.00001, 0.9, 1.0, 1.1, 100.0)
for offset in (0, 1, 2, 4, 10)
]
expected = [t for t in given]
random.shuffle(given)
"""
self.assertEqual(len(given), len(expected)) # sanity
timestamps = [timestamp.Timestamp(t) for t in given]
# our expected values don't include insignificant offsets
with mock.patch('swift.common.utils.timestamp.FORCE_INTERNAL',
new=False):
self.assertEqual(
[t.internal for t in sorted(timestamps)], expected)
# string sorting works as well
self.assertEqual(
sorted([t.internal for t in timestamps]), expected)
def test_hashable(self):
ts_0 = timestamp.Timestamp('1402444821.72589')
ts_0_also = timestamp.Timestamp('1402444821.72589')
self.assertEqual(ts_0, ts_0_also) # sanity
self.assertEqual(hash(ts_0), hash(ts_0_also))
d = {ts_0: 'whatever'}
self.assertIn(ts_0, d) # sanity
self.assertIn(ts_0_also, d)
def test_out_of_range_comparisons(self):
now = timestamp.Timestamp.now()
def check_is_later(val):
self.assertTrue(now != val)
self.assertFalse(now == val)
self.assertTrue(now <= val)
self.assertTrue(now < val)
self.assertTrue(val > now)
self.assertTrue(val >= now)
check_is_later(1e30)
check_is_later(1579753284000) # someone gave us ms instead of s!
check_is_later('1579753284000')
check_is_later(b'1e15')
check_is_later(u'1.e+10_f')
def check_is_earlier(val):
self.assertTrue(now != val)
self.assertFalse(now == val)
self.assertTrue(now >= val)
self.assertTrue(now > val)
self.assertTrue(val < now)
self.assertTrue(val <= now)
check_is_earlier(-1)
check_is_earlier(-0.1)
check_is_earlier('-9999999')
check_is_earlier(b'-9999.999')
check_is_earlier(u'-1234_5678')
def test_inversion(self):
ts = timestamp.Timestamp(0)
self.assertIsInstance(~ts, timestamp.Timestamp)
self.assertEqual((~ts).internal, '9999999999.99999')
ts = timestamp.Timestamp(123456.789)
self.assertIsInstance(~ts, timestamp.Timestamp)
self.assertEqual(ts.internal, '0000123456.78900')
self.assertEqual((~ts).internal, '9999876543.21099')
timestamps = sorted(timestamp.Timestamp(random.random() * 1e10)
for _ in range(20))
self.assertEqual([x.internal for x in timestamps],
sorted(x.internal for x in timestamps))
self.assertEqual([(~x).internal for x in reversed(timestamps)],
sorted((~x).internal for x in timestamps))
ts = timestamp.Timestamp.now()
self.assertGreater(~ts, ts) # NB: will break around 2128
ts = timestamp.Timestamp.now(offset=1)
with self.assertRaises(ValueError) as caught:
~ts
self.assertEqual(caught.exception.args[0],
'Cannot invert timestamps with offsets')
def test_inversion_reversibility(self):
ts = timestamp.Timestamp(1755077566.523385)
inv = ~ts
inv_inv = ~inv
self.assertEqual(ts, inv_inv)
self.assertEqual(ts.normal, inv_inv.normal)
inv_inv_inv = ~inv_inv
self.assertEqual(inv, inv_inv_inv)
self.assertEqual(inv.normal, inv_inv_inv.normal)
ts = timestamp.Timestamp.now()
inv = ~ts
inv_inv = ~inv
self.assertEqual(ts, inv_inv)
self.assertEqual(ts.normal, inv_inv.normal)
class TestTimestampEncoding(unittest.TestCase):
def setUp(self):
t0 = timestamp.Timestamp(0.0)
t1 = timestamp.Timestamp(997.9996)
t2 = timestamp.Timestamp(999)
t3 = timestamp.Timestamp(1000, 24)
t4 = timestamp.Timestamp(1001)
t5 = timestamp.Timestamp(1002.00040)
# encodings that are expected when explicit = False
self.non_explicit_encodings = (
('0000001000.00000_18', (t3, t3, t3)),
('0000001000.00000_18', (t3, t3, None)),
)
# mappings that are expected when explicit = True
self.explicit_encodings = (
('0000001000.00000_18+0+0', (t3, t3, t3)),
('0000001000.00000_18+0', (t3, t3, None)),
)
# mappings that are expected when explicit = True or False
self.encodings = (
('0000001000.00000_18+0+186a0', (t3, t3, t4)),
('0000001000.00000_18+186a0+186c8', (t3, t4, t5)),
('0000001000.00000_18-186a0+0', (t3, t2, t2)),
('0000001000.00000_18+0-186a0', (t3, t3, t2)),
('0000001000.00000_18-186a0-186c8', (t3, t2, t1)),
('0000001000.00000_18', (t3, None, None)),
('0000001000.00000_18+186a0', (t3, t4, None)),
('0000001000.00000_18-186a0', (t3, t2, None)),
('0000001000.00000_18', (t3, None, t1)),
('0000001000.00000_18-5f5e100', (t3, t0, None)),
('0000001000.00000_18+0-5f5e100', (t3, t3, t0)),
('0000001000.00000_18-5f5e100+5f45a60', (t3, t0, t2)),
)
# decodings that are expected when explicit = False
self.non_explicit_decodings = (
('0000001000.00000_18', (t3, t3, t3)),
('0000001000.00000_18+186a0', (t3, t4, t4)),
('0000001000.00000_18-186a0', (t3, t2, t2)),
('0000001000.00000_18+186a0', (t3, t4, t4)),
('0000001000.00000_18-186a0', (t3, t2, t2)),
('0000001000.00000_18-5f5e100', (t3, t0, t0)),
)
# decodings that are expected when explicit = True
self.explicit_decodings = (
('0000001000.00000_18+0+0', (t3, t3, t3)),
('0000001000.00000_18+0', (t3, t3, None)),
('0000001000.00000_18', (t3, None, None)),
('0000001000.00000_18+186a0', (t3, t4, None)),
('0000001000.00000_18-186a0', (t3, t2, None)),
('0000001000.00000_18-5f5e100', (t3, t0, None)),
)
# decodings that are expected when explicit = True or False
self.decodings = (
('0000001000.00000_18+0+186a0', (t3, t3, t4)),
('0000001000.00000_18+186a0+186c8', (t3, t4, t5)),
('0000001000.00000_18-186a0+0', (t3, t2, t2)),
('0000001000.00000_18+0-186a0', (t3, t3, t2)),
('0000001000.00000_18-186a0-186c8', (t3, t2, t1)),
('0000001000.00000_18-5f5e100+5f45a60', (t3, t0, t2)),
)
def _assertEqual(self, expected, actual, test):
self.assertEqual(expected, actual,
'Got %s but expected %s for parameters %s'
% (actual, expected, test))
def test_encoding(self):
for test in self.explicit_encodings:
actual = timestamp.encode_timestamps(test[1][0], test[1][1],
test[1][2], True)
self._assertEqual(test[0], actual, test[1])
for test in self.non_explicit_encodings:
actual = timestamp.encode_timestamps(test[1][0], test[1][1],
test[1][2], False)
self._assertEqual(test[0], actual, test[1])
for explicit in (True, False):
for test in self.encodings:
actual = timestamp.encode_timestamps(test[1][0], test[1][1],
test[1][2], explicit)
self._assertEqual(test[0], actual, test[1])
def test_decoding(self):
for test in self.explicit_decodings:
actual = timestamp.decode_timestamps(test[0], True)
self._assertEqual(test[1], actual, test[0])
for test in self.non_explicit_decodings:
actual = timestamp.decode_timestamps(test[0], False)
self._assertEqual(test[1], actual, test[0])
for explicit in (True, False):
for test in self.decodings:
actual = timestamp.decode_timestamps(test[0], explicit)
self._assertEqual(test[1], actual, test[0])
|