|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Verify basic usage of sharding_supervisor.""" | |
7 | |
8 import os | |
9 import subprocess | |
10 import sys | |
11 import unittest | |
12 | |
13 import sharding_supervisor | |
14 | |
15 SHARDING_SUPERVISOR = os.path.join(os.path.dirname(sys.argv[0]), | |
16 'sharding_supervisor.py') | |
17 DUMMY_TEST = os.path.join(os.path.dirname(sys.argv[0]), 'dummy_test.py') | |
18 NUM_CORES = sharding_supervisor.DetectNumCores() | |
19 SHARDS_PER_CORE = sharding_supervisor.SS_DEFAULT_SHARDS_PER_CORE | |
20 | |
M-A Ruel
2012/04/05 00:53:58
2 lines.
nsylvain
2012/04/05 01:15:58
Done.
| |
21 def GenerateExpectedOutput(start, end, num_shards): | |
M-A Ruel
2012/04/05 00:53:58
In general, I prefer new code to use PEP8 names.
nsylvain
2012/04/05 01:15:58
Done.
| |
22 """Generate the expected stdout and stderr for the dummy test.""" | |
23 stdout = '' | |
24 for i in range(start, end): | |
M-A Ruel
2012/04/05 00:53:58
If I were picky, I'd tell you to use xrange(). You
nsylvain
2012/04/05 01:15:58
Good thing you are not picky.
| |
25 stdout += 'Running shard %d of %d\n' % (i, num_shards) | |
M-A Ruel
2012/04/05 00:53:58
stdout = ''.join(
'Running shard %d of %d\n' %
nsylvain
2012/04/05 01:15:58
I actually don't find this code more readable and
| |
26 stderr = '\nALL SHARDS PASSED!\nALL TESTS PASSED!\n' | |
27 | |
28 return (stdout, stderr) | |
29 | |
30 | |
31 class ShardingSupervisorUnittest(unittest.TestCase): | |
32 def testBasicRun(self): | |
33 # Default test. | |
34 expected_shards = NUM_CORES * SHARDS_PER_CORE | |
35 (expected_out, expected_err) = GenerateExpectedOutput(0, expected_shards, | |
M-A Ruel
2012/04/05 00:53:58
This kind of alignment makes me sad. :/
nsylvain
2012/04/05 01:15:58
what about this one?
| |
36 expected_shards) | |
37 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', DUMMY_TEST], | |
38 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
39 | |
40 p.wait(); | |
M-A Ruel
2012/04/05 00:53:58
; ?
p.wait()?
See http://docs.python.org/library/
nsylvain
2012/04/05 01:15:58
Done.
| |
41 self.assertEqual(expected_out, p.stdout.read()) | |
42 self.assertEqual(expected_err, p.stderr.read()) | |
43 self.assertEqual(0, p.returncode) | |
44 | |
45 def testShardPerCore(self): | |
46 """Test the --shards_per_core parameter.""" | |
47 expected_shards = NUM_CORES * 25 | |
48 (expected_out, expected_err) = GenerateExpectedOutput(0, expected_shards, | |
49 expected_shards) | |
50 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', | |
51 '--shards_per_core', '25', DUMMY_TEST], | |
52 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
53 | |
54 p.wait(); | |
55 self.assertEqual(expected_out, p.stdout.read()) | |
56 self.assertEqual(expected_err, p.stderr.read()) | |
57 self.assertEqual(0, p.returncode) | |
58 | |
59 def testSlaveSharding(self): | |
60 """Test the --total-slaves and --slave-index parameters.""" | |
61 total_shards = 6 | |
62 expected_shards = NUM_CORES * SHARDS_PER_CORE * total_shards | |
63 | |
64 # Test every single index to make sure they run correctly. | |
65 for index in range(total_shards): | |
66 begin = NUM_CORES * SHARDS_PER_CORE * index | |
67 end = begin + NUM_CORES * SHARDS_PER_CORE | |
68 (expected_out, expected_err) = GenerateExpectedOutput(begin, end, | |
69 expected_shards) | |
70 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', | |
71 '--total-slaves', str(total_shards), | |
72 '--slave-index', str(index), | |
73 DUMMY_TEST], | |
74 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
75 | |
76 p.wait(); | |
77 self.assertEqual(expected_out, p.stdout.read()) | |
78 self.assertEqual(expected_err, p.stderr.read()) | |
79 self.assertEqual(0, p.returncode) | |
80 | |
81 | |
82 if __name__ == '__main__': | |
83 unittest.main() | |
OLD | NEW |