diff options
| author | Joanna Wang <jojwang@google.com> | 2022-12-09 17:49:07 -0500 |
|---|---|---|
| committer | Joanna Wang <jojwang@google.com> | 2022-12-09 22:49:31 +0000 |
| commit | 0324e43242ff078dfce70e80ce8e00d394f5ccdb (patch) | |
| tree | d9a08362186827c10c8fbe01162692c9ac1a575a /tests/test_repo_trace.py | |
| parent | 8d25584f6987bbef81277996203f0967c4d8b4da (diff) | |
| download | git-repo-0324e43242ff078dfce70e80ce8e00d394f5ccdb.tar.gz | |
repo_trace: Avoid race conditions with trace_file updating.
Change-Id: I0bc1bb3c8f60465dc6bee5081688a9f163dd8cf8
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/354515
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: Joanna Wang <jojwang@google.com>
Diffstat (limited to 'tests/test_repo_trace.py')
| -rw-r--r-- | tests/test_repo_trace.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/test_repo_trace.py b/tests/test_repo_trace.py new file mode 100644 index 00000000..5faf2938 --- /dev/null +++ b/tests/test_repo_trace.py | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | # Copyright 2022 The Android Open Source Project | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | """Unittests for the repo_trace.py module.""" | ||
| 16 | |||
| 17 | import os | ||
| 18 | import unittest | ||
| 19 | from unittest import mock | ||
| 20 | |||
| 21 | import repo_trace | ||
| 22 | |||
| 23 | |||
| 24 | class TraceTests(unittest.TestCase): | ||
| 25 | """Check Trace behavior.""" | ||
| 26 | |||
| 27 | def testTrace_MaxSizeEnforced(self): | ||
| 28 | content = 'git chicken' | ||
| 29 | |||
| 30 | with repo_trace.Trace(content, first_trace=True): | ||
| 31 | pass | ||
| 32 | first_trace_size = os.path.getsize(repo_trace._TRACE_FILE) | ||
| 33 | |||
| 34 | with repo_trace.Trace(content): | ||
| 35 | pass | ||
| 36 | self.assertGreater( | ||
| 37 | os.path.getsize(repo_trace._TRACE_FILE), first_trace_size) | ||
| 38 | |||
| 39 | # Check we clear everything is the last chunk is larger than _MAX_SIZE. | ||
| 40 | with mock.patch('repo_trace._MAX_SIZE', 0): | ||
| 41 | with repo_trace.Trace(content, first_trace=True): | ||
| 42 | pass | ||
| 43 | self.assertEqual(first_trace_size, | ||
| 44 | os.path.getsize(repo_trace._TRACE_FILE)) | ||
| 45 | |||
| 46 | # Check we only clear the chunks we need to. | ||
| 47 | repo_trace._MAX_SIZE = (first_trace_size + 1) / (1024 * 1024) | ||
| 48 | with repo_trace.Trace(content, first_trace=True): | ||
| 49 | pass | ||
| 50 | self.assertEqual(first_trace_size * 2, | ||
| 51 | os.path.getsize(repo_trace._TRACE_FILE)) | ||
| 52 | |||
| 53 | with repo_trace.Trace(content, first_trace=True): | ||
| 54 | pass | ||
| 55 | self.assertEqual(first_trace_size * 2, | ||
| 56 | os.path.getsize(repo_trace._TRACE_FILE)) | ||
