Merge lp:~bac/launchpad/bug-891641 into lp:launchpad

Proposed by Brad Crittenden
Status: Merged
Approved by: Brad Crittenden
Approved revision: no longer in the source branch.
Merged at revision: 14318
Proposed branch: lp:~bac/launchpad/bug-891641
Merge into: lp:launchpad
Diff against target: 36 lines (+7/-3)
1 file modified
lib/canonical/launchpad/tests/test_token_creation.py (+7/-3)
To merge this branch: bzr merge lp:~bac/launchpad/bug-891641
Reviewer Review Type Date Requested Status
j.c.sackett (community) Approve
Review via email: mp+82544@code.launchpad.net

Commit message

[r=jcsackett][bug=891641] Clean up a test that sets random.seed(0) to restore entropy.

Description of the change

= Summary =

If random.seed(n) is used to provide determinism for a test then the
random number generator must be properly reseeded or subsequent tests
will use predictable random numbers.

== Proposed fix ==

Add a cleanup task to call random.seed() with no parameters, which
will correctly set the seed value using operating system-based entropy
if available or the system clock.

== Pre-implementation notes ==

None.

== Implementation details ==

As above.

== Tests ==

bin/test test_token_creation

== Demo and Q/A ==

None

= Launchpad lint =

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/canonical/launchpad/tests/test_token_creation.py

To post a comment you must log in.
Revision history for this message
j.c.sackett (jcsackett) wrote :

Looks fine to me.

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

I wish you had not done that. Making tests more random is not good.

If this has to use the prng it would be better to save and restore the state.

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

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-08-12 11:19:40 +0000
3+++ lib/canonical/launchpad/tests/test_token_creation.py 2011-11-17 14:51:48 +0000
4@@ -4,7 +4,7 @@
5 __metaclass__ = type
6
7 import random
8-import unittest
9+import testtools
10
11 from canonical.database.constants import UTC_NOW
12 from canonical.launchpad.components.tokens import (
13@@ -16,17 +16,21 @@
14 from canonical.testing.layers import DatabaseFunctionalLayer
15
16
17-class Test_create_token(unittest.TestCase):
18+class Test_create_token(testtools.TestCase):
19
20 def test_length(self):
21 token = create_token(99)
22 self.assertEquals(len(token), 99)
23
24
25-class Test_create_unique_token_for_table(unittest.TestCase):
26+class Test_create_unique_token_for_table(testtools.TestCase):
27 layer = DatabaseFunctionalLayer
28
29 def test_token_uniqueness(self):
30+ # Since the prng will be seeded in this test it is important we clean
31+ # up by calling seed with no parameters, which will use OS-provided
32+ # entropy if available or use the system clock.
33+ self.addCleanup(random.seed)
34 # Calling create_unique_token_for_table() twice with the same
35 # random.seed() will generate two identical tokens, as the token was
36 # never inserted in the table.