Merge lp:~henninge/launchpad/bug-458924 into lp:launchpad

Proposed by Henning Eggers
Status: Merged
Approved by: Henning Eggers
Approved revision: no longer in the source branch.
Merged at revision: not available
Proposed branch: lp:~henninge/launchpad/bug-458924
Merge into: lp:launchpad
Diff against target: 92 lines
1 file modified
lib/lp/testing/__init__.py (+58/-9)
To merge this branch: bzr merge lp:~henninge/launchpad/bug-458924
Reviewer Review Type Date Requested Status
Abel Deuring (community) Approve
Canonical Launchpad Engineering code Pending
Review via email: mp+13831@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Henning Eggers (henninge) wrote :

= Details =
See bug 458924.
This is a simple improvement to FakeTime. I thought that with these improvements I could use FakeTime instead of some local implementations in my tests but I found that lp.testing.time_counter is more what I need. Still, I think this improvement to FakeTime might be useful anyway.

== Test ==
All tests currently using FakeTime should still pass. They are found in codehosting.

bin/test -vvct TestBranchRewriter -t TestLoggingUIFactory -t TestBranchFileSystemClient

= Launchpad lint =
I did *not* fix the imports because this is an __init__.py file and people might be importing from here.

Checking for conflicts. and issues in doctests and templates.
Running jslint, xmllint, pyflakes, and pylint.
Using normal rules.

Linting changed files:
  lib/lp/testing/__init__.py

== Pyflakes notices ==

lib/lp/testing/__init__.py
    20: 'InvalidURLJoin' imported but unused
    43: 'is_logged_in' imported but unused
    45: 'test_tales' imported but unused

Revision history for this message
Abel Deuring (adeuring) wrote :

Hi Henning,

nice branch! Salgado approved my branch that allows running the doc string tests in lp/testing, so we now have also a short test for your changes.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/testing/__init__.py'
2--- lib/lp/testing/__init__.py 2009-10-20 01:55:17 +0000
3+++ lib/lp/testing/__init__.py 2009-10-23 11:16:13 +0000
4@@ -12,6 +12,7 @@
5 import shutil
6 import subprocess
7 import tempfile
8+import time
9 import unittest
10
11 from bzrlib.branch import Branch as BzrBranch
12@@ -53,23 +54,71 @@
13
14
15 class FakeTime:
16- """Provides a controllable implementation of time.time()."""
17-
18- def __init__(self, start):
19+ """Provides a controllable implementation of time.time().
20+
21+ You can either advance the time manually using advance() or have it done
22+ automatically using next_now(). The amount of seconds to advance the
23+ time by is set during initialization but can also be changed for single
24+ calls of advance() or next_now().
25+
26+ >>> faketime = FakeTime(1000)
27+ >>> print faketime.now()
28+ 1000
29+ >>> print faketime.now()
30+ 1000
31+ >>> faketime.advance(10)
32+ >>> print faketime.now()
33+ 1010
34+ >>> print faketime.next_now()
35+ 1011
36+ >>> print faketime.next_now(100)
37+ 1111
38+ >>> faketime = FakeTime(1000, 5)
39+ >>> print faketime.next_now()
40+ 1005
41+ >>> print faketime.next_now()
42+ 1010
43+ """
44+
45+ def __init__(self, start=None, advance=1):
46 """Set up the instance.
47
48 :param start: The value that will initially be returned by `now()`.
49- """
50- self._now = start
51-
52- def advance(self, amount):
53- """Advance the value that will be returned by `now()` by 'amount'."""
54- self._now += amount
55+ If None, the current time will be used.
56+ :param advance: The value in secounds to advance the clock by by
57+ default.
58+ """
59+ if start is not None:
60+ self._now = start
61+ else:
62+ self._now = time.time()
63+ self._advance = advance
64+
65+ def advance(self, amount=None):
66+ """Advance the value that will be returned by `now()`.
67+
68+ :param amount: The amount of seconds to advance the value by.
69+ If None, the configured default value will be used.
70+ """
71+ if amount is None:
72+ self._now += self._advance
73+ else:
74+ self._now += amount
75
76 def now(self):
77 """Use this bound method instead of time.time in tests."""
78 return self._now
79
80+ def next_now(self, amount=None):
81+ """Read the current time and advance it.
82+
83+ Calls advance() and returns the current value of now().
84+ :param amount: The amount of seconds to advance the value by.
85+ If None, the configured default value will be used.
86+ """
87+ self.advance(amount)
88+ return self.now()
89+
90
91 class StormStatementRecorder:
92 """A storm tracer to count queries."""