blob: df317555053d4d45a52d4ab5a1d4e4423e4a607b [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
Oleh Prypin69c02222018-05-23 15:18:12 +02002
3# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
Mirko Bonadeidac422f2018-06-05 09:12:34 +020011from contextlib import contextmanager
12
Yves Gereyea766fa2018-09-25 15:34:27 +020013import multiprocessing
Mirko Bonadeidac422f2018-06-05 09:12:34 +020014import os
15import tempfile
Oleh Prypin69c02222018-05-23 15:18:12 +020016import unittest
17
Yves Gereyea766fa2018-09-25 15:34:27 +020018# pylint: disable=invalid-name
19gtest_parallel_wrapper = __import__('gtest-parallel-wrapper')
Oleh Prypin69c02222018-05-23 15:18:12 +020020
21
Mirko Bonadeidac422f2018-06-05 09:12:34 +020022@contextmanager
23def TemporaryDirectory():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010024 tmp_dir = tempfile.mkdtemp()
25 yield tmp_dir
26 os.rmdir(tmp_dir)
Mirko Bonadeidac422f2018-06-05 09:12:34 +020027
28
Yves Gereyea766fa2018-09-25 15:34:27 +020029class GtestParallelWrapperHelpersTest(unittest.TestCase):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010030 def testGetWorkersAsIs(self):
31 # pylint: disable=protected-access
32 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('12'), 12)
Yves Gereyea766fa2018-09-25 15:34:27 +020033
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010034 def testGetTwiceWorkers(self):
35 expected = 2 * multiprocessing.cpu_count()
36 # pylint: disable=protected-access
37 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('2x'), expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020038
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010039 def testGetHalfWorkers(self):
40 expected = max(multiprocessing.cpu_count() // 2, 1)
41 # pylint: disable=protected-access
42 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('0.5x'),
43 expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020044
45
Oleh Prypin69c02222018-05-23 15:18:12 +020046class GtestParallelWrapperTest(unittest.TestCase):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010047 @classmethod
48 def _Expected(cls, gtest_parallel_args):
49 return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
Yves Gereyea766fa2018-09-25 15:34:27 +020050
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010051 def testOverwrite(self):
52 result = gtest_parallel_wrapper.ParseArgs(
53 ['--timeout=123', 'exec', '--timeout', '124'])
54 expected = self._Expected(['--timeout=124', 'exec'])
55 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020056
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010057 def testMixing(self):
58 result = gtest_parallel_wrapper.ParseArgs(
59 ['--timeout=123', '--param1', 'exec', '--param2', '--timeout', '124'])
60 expected = self._Expected(
61 ['--timeout=124', 'exec', '--', '--param1', '--param2'])
62 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020063
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010064 def testMixingPositional(self):
65 result = gtest_parallel_wrapper.ParseArgs([
66 '--timeout=123', 'exec', '--foo1', 'bar1', '--timeout', '124', '--foo2',
67 'bar2'
68 ])
69 expected = self._Expected(
70 ['--timeout=124', 'exec', '--', '--foo1', 'bar1', '--foo2', 'bar2'])
71 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020072
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010073 def testDoubleDash1(self):
74 result = gtest_parallel_wrapper.ParseArgs(
75 ['--timeout', '123', 'exec', '--', '--timeout', '124'])
76 expected = self._Expected(
77 ['--timeout=123', 'exec', '--', '--timeout', '124'])
78 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020079
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010080 def testDoubleDash2(self):
81 result = gtest_parallel_wrapper.ParseArgs(
82 ['--timeout=123', '--', 'exec', '--timeout=124'])
83 expected = self._Expected(['--timeout=123', 'exec', '--', '--timeout=124'])
84 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020085
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010086 def testArtifacts(self):
87 with TemporaryDirectory() as tmp_dir:
88 output_dir = os.path.join(tmp_dir, 'foo')
89 result = gtest_parallel_wrapper.ParseArgs(
90 ['exec', '--store-test-artifacts', '--output_dir', output_dir])
91 exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
92 exp = self._Expected([
93 '--output_dir=' + output_dir, 'exec', '--',
94 '--test_artifacts_dir=' + exp_artifacts_dir
95 ])
96 self.assertEqual(result.gtest_parallel_args, exp)
97 self.assertEqual(result.output_dir, output_dir)
98 self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
Oleh Prypin69c02222018-05-23 15:18:12 +020099
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100100 def testNoDirsSpecified(self):
101 result = gtest_parallel_wrapper.ParseArgs(['exec'])
102 self.assertEqual(result.output_dir, None)
103 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200104
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100105 def testOutputDirSpecified(self):
106 result = gtest_parallel_wrapper.ParseArgs(
107 ['exec', '--output_dir', '/tmp/foo'])
108 self.assertEqual(result.output_dir, '/tmp/foo')
109 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200110
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100111 def testShortArg(self):
112 result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
113 expected = self._Expected(['--output_dir=/tmp/foo', 'exec'])
114 self.assertEqual(result.gtest_parallel_args, expected)
115 self.assertEqual(result.output_dir, '/tmp/foo')
Oleh Prypin69c02222018-05-23 15:18:12 +0200116
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100117 def testBoolArg(self):
118 result = gtest_parallel_wrapper.ParseArgs(
119 ['--gtest_also_run_disabled_tests', 'exec'])
120 expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
121 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200122
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100123 def testNoArgs(self):
124 result = gtest_parallel_wrapper.ParseArgs(['exec'])
125 expected = self._Expected(['exec'])
126 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200127
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100128 def testDocExample(self):
129 with TemporaryDirectory() as tmp_dir:
130 output_dir = os.path.join(tmp_dir, 'foo')
131 result = gtest_parallel_wrapper.ParseArgs([
132 'some_test', '--some_flag=some_value', '--another_flag',
133 '--output_dir=' + output_dir, '--store-test-artifacts',
134 '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
135 '--baz'
136 ])
137 expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
138 expected = self._Expected([
139 '--output_dir=' + output_dir, 'some_test', '--',
140 '--test_artifacts_dir=' + expected_artifacts_dir,
141 '--some_flag=some_value', '--another_flag',
142 '--isolated_script_test_perf_output=SOME_OTHER_DIR', '--foo=bar',
143 '--baz'
144 ])
145 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200146
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100147 def testStandardWorkers(self):
148 """Check integer value is passed as-is."""
149 result = gtest_parallel_wrapper.ParseArgs(['--workers', '17', 'exec'])
150 expected = self._Expected(['--workers=17', 'exec'])
151 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200152
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100153 def testTwoWorkersPerCpuCore(self):
154 result = gtest_parallel_wrapper.ParseArgs(['--workers', '2x', 'exec'])
155 workers = 2 * multiprocessing.cpu_count()
156 expected = self._Expected(['--workers=%s' % workers, 'exec'])
157 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200158
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100159 def testUseHalfTheCpuCores(self):
160 result = gtest_parallel_wrapper.ParseArgs(['--workers', '0.5x', 'exec'])
161 workers = max(multiprocessing.cpu_count() // 2, 1)
162 expected = self._Expected(['--workers=%s' % workers, 'exec'])
163 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200164
Oleh Prypin69c02222018-05-23 15:18:12 +0200165
166if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100167 unittest.main()