tests: Make random tests more robust
[libfiu.git] / tests / test-fiu_ctrl.py
blob04e2fbcf30b7849bdae048bbe398d1b98c712da8
1 """
2 Tests for the fiu_ctrl.py module.
4 Note the command line utility is covered by the utils/ tests, not from here,
5 this is just for the Python module.
6 """
8 import subprocess
9 import fiu_ctrl
10 import errno
11 import time
13 fiu_ctrl.PLIBPATH = "./libs/"
15 def run_cat(**kwargs):
16 return fiu_ctrl.Subprocess(["./small-cat"],
17 stdin = subprocess.PIPE, stdout = subprocess.PIPE,
18 stderr = subprocess.PIPE, **kwargs)
20 # Run without any failure point being enabled.
21 cmd = run_cat()
22 p = cmd.start()
23 out, err = p.communicate('test\n')
24 assert out == 'test\n', out
25 assert err == '', err
27 # Enable before starting.
28 cmd = run_cat(fiu_enable_posix = True)
29 cmd.enable('posix/io/rw/*', failinfo = errno.ENOSPC)
30 p = cmd.start()
31 out, err = p.communicate('test\n')
32 assert out == '', out
33 assert 'space' in err, err
35 # Enable after starting.
36 cmd = run_cat(fiu_enable_posix = True)
37 p = cmd.start()
38 cmd.enable('posix/io/rw/*', failinfo = errno.ENOSPC)
39 out, err = p.communicate('test\n')
40 assert out == '', out
41 assert 'space' in err, err
43 # Enable-disable.
44 cmd = run_cat(fiu_enable_posix = True)
45 p = cmd.start()
46 cmd.enable('posix/io/rw/*', failinfo = errno.ENOSPC)
47 cmd.disable('posix/io/rw/*')
48 out, err = p.communicate('test\n')
49 assert out == 'test\n', (out, err)
51 # Enable random.
52 # This relies on cat doing a reasonably small number of read and writes, which
53 # our small-cat does.
54 result = { True: 0, False: 0 }
55 for i in range(50):
56 cmd = run_cat(fiu_enable_posix = True)
57 p = cmd.start()
58 cmd.enable_random('posix/io/rw/*', failinfo = errno.ENOSPC,
59 probability = 0.5)
60 out, err = p.communicate('test\n')
61 if 'space' in err:
62 result[False] += 1
63 elif out == 'test\n':
64 result[True] += 1
65 else:
66 assert False, (out, err)
68 assert 10 < result[True] < 40, result
69 assert 10 < result[False] < 40, result