5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 This tests the -j command line option, and the num_jobs
27 SConscript settable option.
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
37 _python_ = TestSCons._python_
42 # if threads are not supported, then
43 # there is nothing to test
48 test = TestSCons.TestSCons()
50 test.write('build.py', r"""
53 file = open(sys.argv[1], 'wb')
54 file.write(str(time.time()) + '\n')
56 file.write(str(time.time()))
62 test.write(['foo','foo.in'], r"""
66 test.write('SConstruct', """
67 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
68 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
70 env.MyBuild(target = 'f1', source = 'f1.in')
71 env.MyBuild(target = 'f2', source = 'f2.in')
73 def copyn(env, target, source):
78 shutil.copy(str(source[0]), str(t))
80 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'],
86 def RunTest(args, extra):
87 """extra is used to make scons rebuild the output file"""
88 test.write('f1.in', 'f1.in'+extra)
89 test.write('f2.in', 'f2.in'+extra)
91 test.run(arguments = args)
94 start1,finish1 = map(float, string.split(str, "\n"))
97 start2,finish2 = map(float, string.split(str, "\n"))
99 return start2, finish1
101 # Test 2 parallel jobs.
102 # fail if the second file was not started
103 # before the first one was finished.
104 start2, finish1 = RunTest('-j 2 f1 f2', "first")
105 test.fail_test(not (start2 < finish1))
107 # re-run the test with the same input, fail if we don't
108 # get back the same times, which would indicate that
109 # SCons rebuilt the files even though nothing changed
110 s2, f1 = RunTest('-j 2 f1 f2', "first")
111 test.fail_test(start2 != s2)
112 test.fail_test(finish1 != f1)
114 # Test a single serial job.
115 # fail if the second file was started
116 # before the first one was finished
117 start2, finish1 = RunTest('f1 f2', "second")
118 test.fail_test(start2 < finish1)
120 # Make sure that a parallel build using a list builder
122 test.run(arguments='-j 2 out')
125 # Test that we fall back and warn properly if there's no threading.py
126 # module (simulated), which is the case if this version of Python wasn't
127 # built with threading support.
129 test.subdir('pythonlib')
131 test.write(['pythonlib', 'threading.py'], """\
135 save_pythonpath = os.environ.get('PYTHONPATH', '')
136 os.environ['PYTHONPATH'] = test.workpath('pythonlib')
138 #start2, finish1 = RunTest('-j 2 f1, f2', "fifth")
140 test.write('f1.in', 'f1.in pythonlib\n')
141 test.write('f2.in', 'f2.in pythonlib\n')
143 test.run(arguments = "-j 2 f1 f2", stderr=None)
146 """scons: warning: parallel builds are unsupported by this version of Python;
147 \tignoring -j or num_jobs option.
149 test.must_contain_all_lines(test.stderr(), [warn])
151 str = test.read("f1")
152 start1,finish1 = map(float, string.split(str, "\n"))
154 str = test.read("f2")
155 start2,finish2 = map(float, string.split(str, "\n"))
157 test.fail_test(start2 < finish1)
159 os.environ['PYTHONPATH'] = save_pythonpath
162 # Test SetJobs() with no -j:
163 test.write('SConstruct', """
164 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
165 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
167 env.MyBuild(target = 'f1', source = 'f1.in')
168 env.MyBuild(target = 'f2', source = 'f2.in')
170 def copyn(env, target, source):
175 shutil.copy(str(source[0]), str(t))
177 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'], source='foo/foo.in', action=copyn)
178 env.Install('out', t)
180 assert GetOption('num_jobs') == 1
181 SetOption('num_jobs', 2)
182 assert GetOption('num_jobs') == 2
185 # This should be a parallel build because the SConscript sets jobs to 2.
186 # fail if the second file was not started
187 # before the first one was finished
188 start2, finish1 = RunTest('f1 f2', "third")
189 test.fail_test(not (start2 < finish1))
191 # Test SetJobs() with -j:
192 test.write('SConstruct', """
193 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
194 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
196 env.MyBuild(target = 'f1', source = 'f1.in')
197 env.MyBuild(target = 'f2', source = 'f2.in')
199 def copyn(env, target, source):
204 shutil.copy(str(source[0]), str(t))
206 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'], source='foo/foo.in', action=copyn)
207 env.Install('out', t)
209 assert GetOption('num_jobs') == 1
210 SetOption('num_jobs', 2)
211 assert GetOption('num_jobs') == 1
214 # This should be a serial build since -j 1 overrides the call to SetJobs().
215 # fail if the second file was started
216 # before the first one was finished
217 start2, finish1 = RunTest('-j 1 f1 f2', "fourth")
218 test.fail_test(start2 < finish1)
226 # indent-tabs-mode:nil
228 # vim: set expandtab tabstop=4 shiftwidth=4: