Mercurial > hg
annotate mercurial/testing/__init__.py @ 47804:5ad37164a8fe stable
testing: make sure write_file is "atomic"
This make sure viewer cannot see the new file with partial content.
This was likely the cause of some flakiness in `test-nointerrupt.t`
Differential Revision: https://phab.mercurial-scm.org/D11250
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Tue, 03 Aug 2021 19:26:26 +0200 |
parents | 1bad89a67745 |
children | 6000f5b25c9b |
rev | line source |
---|---|
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
1 from __future__ import ( |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
2 absolute_import, |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
3 division, |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
4 ) |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
5 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
6 import os |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
7 import time |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
8 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
9 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
10 # work around check-code complains |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
11 # |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
12 # This is a simple log level module doing simple test related work, we can't |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
13 # import more things, and we do not need it. |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
14 environ = getattr(os, 'environ') |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
15 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
16 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
17 def _timeout_factor(): |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
18 """return the current modification to timeout""" |
47493
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
19 default = int(environ.get('HGTEST_TIMEOUT_DEFAULT', 360)) |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
20 current = int(environ.get('HGTEST_TIMEOUT', default)) |
47493
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
21 if current == 0: |
2dac94edd98d
testing: fix _timeout_factor
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46985
diff
changeset
|
22 return 1 |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
23 return current / float(default) |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
24 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
25 |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
26 def wait_file(path, timeout=10): |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
27 timeout *= _timeout_factor() |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
28 start = time.time() |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
29 while not os.path.exists(path): |
47657
1bad89a67745
testing: do not stop waiting if timeout is 0 (issue6541)
Cédric Krier <ced@b2ck.com>
parents:
47493
diff
changeset
|
30 if timeout and time.time() - start > timeout: |
46984
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
31 raise RuntimeError(b"timed out waiting for file: %s" % path) |
99c629101b73
testing: add a utility function to wait for file create
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
39772
diff
changeset
|
32 time.sleep(0.01) |
46985
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
33 |
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
34 |
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
35 def write_file(path, content=b''): |
47804
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
36 if content: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
37 write_path = b'%s.tmp' % path |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
38 else: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
39 write_path = path |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
40 with open(write_path, 'wb') as f: |
46985
52cee44aa1a0
testing: add a `write_file` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46984
diff
changeset
|
41 f.write(content) |
47804
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
42 if path != write_path: |
5ad37164a8fe
testing: make sure write_file is "atomic"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47657
diff
changeset
|
43 os.rename(write_path, path) |