Merge lp:~mbp/launchpad/less-randomness into lp:launchpad

Proposed by Martin Pool
Status: Merged
Approved by: Martin Pool
Approved revision: no longer in the source branch.
Merged at revision: 14343
Proposed branch: lp:~mbp/launchpad/less-randomness
Merge into: lp:launchpad
Diff against target: 22 lines (+3/-5)
1 file modified
lib/canonical/launchpad/tests/test_token_creation.py (+3/-5)
To merge this branch: bzr merge lp:~mbp/launchpad/less-randomness
Reviewer Review Type Date Requested Status
Ian Booth (community) code Approve
Review via email: mp+82613@code.launchpad.net

Commit message

[r=wallyworld][no-qa] Test_create_token: save and restore random state, rather than making things more random

Description of the change

As an update to https://code.launchpad.net/~bac/launchpad/bug-891641/+merge/82544 this actually saves and restores the prng state, rather than randomizing it, when Test_create_token runs.

To post a comment you must log in.
Revision history for this message
Ian Booth (wallyworld) wrote :

Looks like an acceptable change. Doesn't appear that anything will break :-)

review: Approve (code)
Revision history for this message
Martin Pool (mbp) wrote :

socratic dialog:

<wallyworld_> poolie: isn't the whole point of using a rng to provide different input each time?
<poolie> wallyworld_, so, generally, my experience is that using a rng in tests is a bad idea
 if there are some random values that catch a bug, which is possible
 there are problems
 one is, the particular values are likely to come up only very rarely, so the bug may be latent for a long time
 secondly, it will be hard to reproduce it, so perhaps hard to understand why it's failing
 oh, and also, it may fail at inconvenient times
 for example on buildbot but not ec2 etc
<poolie> wallyworld_, so this particular test is testing code that itself uses the rng, so i think it has a valid reason to touch it
 but it should isoltae itself
<wallyworld_> poolie: that all makes sense. thanks for explaining

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/canonical/launchpad/tests/test_token_creation.py'
2--- lib/canonical/launchpad/tests/test_token_creation.py 2011-11-17 14:42:51 +0000
3+++ lib/canonical/launchpad/tests/test_token_creation.py 2011-11-17 23:24:25 +0000
4@@ -1,4 +1,4 @@
5-# Copyright 2009 Canonical Ltd. This software is licensed under the
6+# Copyright 2009, 2011 Canonical Ltd. This software is licensed under the
7 # GNU Affero General Public License version 3 (see the file LICENSE).
8
9 __metaclass__ = type
10@@ -27,10 +27,8 @@
11 layer = DatabaseFunctionalLayer
12
13 def test_token_uniqueness(self):
14- # Since the prng will be seeded in this test it is important we clean
15- # up by calling seed with no parameters, which will use OS-provided
16- # entropy if available or use the system clock.
17- self.addCleanup(random.seed)
18+ orig_state = random.getstate()
19+ self.addCleanup(lambda: random.setstate(orig_state))
20 # Calling create_unique_token_for_table() twice with the same
21 # random.seed() will generate two identical tokens, as the token was
22 # never inserted in the table.