/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_timestamp.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2009, 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import random
 
18
import time
 
19
 
 
20
 
 
21
from bzrlib import (
 
22
    tests,
 
23
    timestamp,
 
24
    )
 
25
from bzrlib.osutils import local_time_offset
 
26
 
 
27
 
 
28
class TestPatchHeader(tests.TestCase):
 
29
 
 
30
    def test_format_patch_date(self):
 
31
        # epoch is always in utc
 
32
        self.assertEqual('1970-01-01 00:00:00 +0000',
 
33
            timestamp.format_patch_date(0))
 
34
        self.assertEqual('1970-01-01 00:00:00 +0000',
 
35
            timestamp.format_patch_date(0, 5 * 3600))
 
36
        self.assertEqual('1970-01-01 00:00:00 +0000',
 
37
            timestamp.format_patch_date(0, -5 * 3600))
 
38
        # regular timestamp with typical timezone
 
39
        self.assertEqual('2007-03-06 10:04:19 -0500',
 
40
            timestamp.format_patch_date(1173193459, -5 * 3600))
 
41
        # the timezone part is HHMM
 
42
        self.assertEqual('2007-03-06 09:34:19 -0530',
 
43
            timestamp.format_patch_date(1173193459, -5.5 * 3600))
 
44
        # timezones can be offset by single minutes (but no less)
 
45
        self.assertEqual('2007-03-06 15:05:19 +0001',
 
46
            timestamp.format_patch_date(1173193459, +1 * 60))
 
47
 
 
48
    def test_parse_patch_date(self):
 
49
        self.assertEqual((0, 0),
 
50
            timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
 
51
        # even though we don't emit pre-epoch dates, we can parse them
 
52
        self.assertEqual((0, -5 * 3600),
 
53
            timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
 
54
        self.assertEqual((0, +5 * 3600),
 
55
            timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
 
56
        self.assertEqual((1173193459, -5 * 3600),
 
57
            timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
 
58
        # offset of three minutes
 
59
        self.assertEqual((1173193459, +3 * 60),
 
60
            timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
 
61
        # No space between time and offset
 
62
        self.assertEqual((1173193459, -5 * 3600),
 
63
            timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
 
64
        # Extra spacing
 
65
        self.assertEqual((1173193459, -5 * 3600),
 
66
            timestamp.parse_patch_date('2007-03-06     10:04:19     -0500'))
 
67
 
 
68
    def test_parse_patch_date_bad(self):
 
69
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
70
            'NOT A TIME')
 
71
        # Extra data at end
 
72
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
73
            '2007-03-06 10:04:19 -0500x')
 
74
        # Missing day
 
75
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
76
            '2007-03 10:04:19 -0500')
 
77
        # Missing seconds
 
78
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
79
            '2007-03-06 10:04 -0500')
 
80
        # Missing offset
 
81
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
82
            '2007-03-06 10:04:19')
 
83
        # Missing plus or minus in offset
 
84
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
85
            '2007-03-06 10:04:19 0500')
 
86
        # Invalid hour in offset
 
87
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
88
            '2007-03-06 10:04:19 +2400')
 
89
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
90
            '2007-03-06 10:04:19 -2400')
 
91
        # Invalid minute in offset
 
92
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
93
            '2007-03-06 10:04:19 -0560')
 
94
        # Too many digits in offset
 
95
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
96
            '2007-03-06 10:04:19 79500')
 
97
        # Minus sign in middle of offset
 
98
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
99
            '2007-03-06 10:04:19 +05-5')
 
100
 
 
101
 
 
102
class UnpackHighresDateTests(tests.TestCase):
 
103
 
 
104
    def test_unpack_highres_date(self):
 
105
        self.assertEquals(
 
106
            (1120153132.3508501, -18000),
 
107
            timestamp.unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500'))
 
108
        self.assertEquals(
 
109
            (1120153132.3508501, 0),
 
110
            timestamp.unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000'))
 
111
        self.assertEquals(
 
112
            (1120153132.3508501, 7200),
 
113
            timestamp.unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200'))
 
114
        self.assertEquals(
 
115
            (1152428738.867522, 19800),
 
116
            timestamp.unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530'))
 
117
 
 
118
    def test_random(self):
 
119
        t = time.time()
 
120
        o = local_time_offset()
 
121
        t2, o2 = timestamp.unpack_highres_date(timestamp.format_highres_date(t, o))
 
122
        self.assertEquals(t, t2)
 
123
        self.assertEquals(o, o2)
 
124
        t -= 24*3600*365*2 # Start 2 years ago
 
125
        o = -12*3600
 
126
        for count in xrange(500):
 
127
            t += random.random()*24*3600*30
 
128
            o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
 
129
            date = timestamp.format_highres_date(t, o)
 
130
            t2, o2 = timestamp.unpack_highres_date(date)
 
131
            self.assertEquals(t, t2,
 
132
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
 
133
            self.assertEquals(o, o2,
 
134
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))