xref: /aosp_15_r20/external/webrtc/tools_webrtc/gtest_parallel_wrapper_test.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/usr/bin/env vpython3
2
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
11from contextlib import contextmanager
12
13import multiprocessing
14import os
15import tempfile
16import unittest
17
18# pylint: disable=invalid-name
19gtest_parallel_wrapper = __import__('gtest-parallel-wrapper')
20
21
22@contextmanager
23def TemporaryDirectory():
24  tmp_dir = tempfile.mkdtemp()
25  yield tmp_dir
26  os.rmdir(tmp_dir)
27
28
29class GtestParallelWrapperHelpersTest(unittest.TestCase):
30  def testGetWorkersAsIs(self):
31    # pylint: disable=protected-access
32    self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('12'), 12)
33
34  def testGetTwiceWorkers(self):
35    expected = 2 * multiprocessing.cpu_count()
36    # pylint: disable=protected-access
37    self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('2x'), expected)
38
39  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)
44
45
46class GtestParallelWrapperTest(unittest.TestCase):
47  @classmethod
48  def _Expected(cls, gtest_parallel_args):
49    return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
50
51  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)
56
57  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)
63
64  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)
72
73  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)
79
80  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)
85
86  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)
99
100  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)
104
105  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)
110
111  def testJsonTestResults(self):
112    result = gtest_parallel_wrapper.ParseArgs(
113        ['--isolated-script-test-output', '/tmp/foo', 'exec'])
114    expected = self._Expected(['--dump_json_test_results=/tmp/foo', 'exec'])
115    self.assertEqual(result.gtest_parallel_args, expected)
116
117  def testShortArg(self):
118    result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
119    expected = self._Expected(['--output_dir=/tmp/foo', 'exec'])
120    self.assertEqual(result.gtest_parallel_args, expected)
121    self.assertEqual(result.output_dir, '/tmp/foo')
122
123  def testBoolArg(self):
124    result = gtest_parallel_wrapper.ParseArgs(
125        ['--gtest_also_run_disabled_tests', 'exec'])
126    expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
127    self.assertEqual(result.gtest_parallel_args, expected)
128
129  def testNoArgs(self):
130    result = gtest_parallel_wrapper.ParseArgs(['exec'])
131    expected = self._Expected(['exec'])
132    self.assertEqual(result.gtest_parallel_args, expected)
133
134  def testDocExample(self):
135    with TemporaryDirectory() as tmp_dir:
136      output_dir = os.path.join(tmp_dir, 'foo')
137      result = gtest_parallel_wrapper.ParseArgs([
138          'some_test', '--some_flag=some_value', '--another_flag',
139          '--output_dir=' + output_dir, '--store-test-artifacts',
140          '--isolated-script-test-output=SOME_DIR',
141          '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
142          '--baz'
143      ])
144      expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
145      expected = self._Expected([
146          '--output_dir=' + output_dir, '--dump_json_test_results=SOME_DIR',
147          'some_test', '--', '--test_artifacts_dir=' + expected_artifacts_dir,
148          '--some_flag=some_value', '--another_flag',
149          '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
150          '--baz'
151      ])
152      self.assertEqual(result.gtest_parallel_args, expected)
153
154  def testStandardWorkers(self):
155    """Check integer value is passed as-is."""
156    result = gtest_parallel_wrapper.ParseArgs(['--workers', '17', 'exec'])
157    expected = self._Expected(['--workers=17', 'exec'])
158    self.assertEqual(result.gtest_parallel_args, expected)
159
160  def testTwoWorkersPerCpuCore(self):
161    result = gtest_parallel_wrapper.ParseArgs(['--workers', '2x', 'exec'])
162    workers = 2 * multiprocessing.cpu_count()
163    expected = self._Expected(['--workers=%s' % workers, 'exec'])
164    self.assertEqual(result.gtest_parallel_args, expected)
165
166  def testUseHalfTheCpuCores(self):
167    result = gtest_parallel_wrapper.ParseArgs(['--workers', '0.5x', 'exec'])
168    workers = max(multiprocessing.cpu_count() // 2, 1)
169    expected = self._Expected(['--workers=%s' % workers, 'exec'])
170    self.assertEqual(result.gtest_parallel_args, expected)
171
172
173if __name__ == '__main__':
174  unittest.main()
175