# [pycrypto] Two flaky tests

Peter McKenzie petermck at google.com
Sun Mar 17 16:20:46 PDT 2013

```Greetings,

I've noticed that a couple of the pycrypto (2.6) tests are a little
flaky.  Our automated internal test infrastructure tends to be pretty
good at exposing this sort of thing, and it impacts our developer
productivity, so I was hoping to get the tests patched.

The problems, with suggested fixes are described below.

File: lib/Crypto/SelfTest/Random/test_random.py

Code (in functon test_isPrime()):

for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517,
346141L * 692281L, 1007119L * 2014237L, 3589477L * 7178953L,
4859419L * 9718837L, 2730439L * 5460877L,
245127919L * 490255837L, 963939391L * 1927878781L,
4186358431L * 8372716861L, 1576820467L * 3153640933L):
self.assertEqual(number.isPrime(long(composite)), False)

The default probability for isPrime to return a false positive is
1e-6.  That's enough for us to see them occasionally, so I propose
changing the assert to be:

self.assertEqual(number.isPrime(long(composite),
false_positive_prob=1e-9), False)

File: lib/Crypto/SelfTest/Util/test_number.py

Code (in function runTest()):

# Test choice
seq = range(10000)
x = random.choice(seq)
y = random.choice(seq)
self.assertNotEqual(x, y)

This assert will fail approximately one in 10000 runs which is way too
often for us.  Possible fixes:
a) remove the assert. I'm fine with that, but presumably someone wants
to test that x and y are not equal all the time.
b) make seq a bigger range.  I'd want the range to be at least 100
million, which needlessly chews up time and CPU.
c) which I quite like: add the following code immediately prior to the assert:

if x == y:
y = random.choice(seq)

That reduces the chance of failure to about 1 in 10000**2, which is acceptable.

Thanks,
Peter McKenzie
```

More information about the pycrypto mailing list