-
Notifications
You must be signed in to change notification settings - Fork 39
further unit test speedups and cleanups #9226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,27 +43,27 @@ def test_read_write(tmp_path: Path) -> None: | |
|
|
||
|
|
||
| def test_current(tmp_path: Path) -> None: | ||
| c = cache.Cache[object](f'{tmp_path}', lag=3) | ||
| c = cache.Cache[object](f'{tmp_path}', lag=1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a bit of a micro-optimisation
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is the single biggest improvement in test run times... there's just no need to wait that long... I nearly changed it to 0.1... |
||
|
|
||
| c.write("resource2", {"value": 2}) | ||
| assert c.current('resource2') is True | ||
|
|
||
| time.sleep(2) | ||
| time.sleep(0.6) | ||
| assert c.current('resource2') is True | ||
|
|
||
| time.sleep(2) | ||
| time.sleep(0.6) | ||
| assert c.current('resource2') is False | ||
|
|
||
|
|
||
| def test_current_mark(tmp_path: Path) -> None: | ||
| c = cache.Cache[object](f'{tmp_path}', lag=3) | ||
| c = cache.Cache[object](f'{tmp_path}', lag=1) | ||
|
|
||
| assert c.current('resource') is False | ||
|
|
||
| c.write("resource", {"value": 1}) | ||
| assert c.current('resource') is True | ||
|
|
||
| time.sleep(2) | ||
| time.sleep(0.5) | ||
| assert c.current('resource') is True | ||
|
|
||
| c.mark() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ | |
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Cockpit; If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| import fnmatch | ||
| import json | ||
| import shutil | ||
| import tempfile | ||
|
|
@@ -28,7 +27,6 @@ | |
| from lib import cache, github | ||
| from lib.test_mock_server import MockHandler, MockServer | ||
|
|
||
| ADDRESS = ("127.0.0.8", 9898) | ||
| GITHUB_ISSUES = [{"number": "5", "state": "open", "created_at": "2011-04-22T13:33:48Z"}, | ||
| {"number": "6", "state": "closed", "closed_at": "2011-04-21T13:33:48Z"}, | ||
| {"number": "7", "state": "open"}] | ||
|
|
@@ -70,10 +68,11 @@ def do_DELETE(self) -> None: | |
|
|
||
| class TestGitHub(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.server = MockServer(ADDRESS, Handler, GITHUB_ISSUES) | ||
| self.server = MockServer(("127.0.0.1", 0), Handler, GITHUB_ISSUES) | ||
| self.server.start() | ||
| self.temp = tempfile.mkdtemp() | ||
| self.api = github.GitHub(f"http://{ADDRESS[0]}:{ADDRESS[1]}/", cacher=cache.Cache(self.temp)) | ||
| url = f"http://{self.server.address[0]}:{self.server.address[1]}/" | ||
| self.api = github.GitHub(url, cacher=cache.Cache(self.temp)) | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.server.kill() | ||
|
|
@@ -88,21 +87,16 @@ def test_cache(self) -> None: | |
| self.assertEqual(count, 1) | ||
|
|
||
| def test_log(self) -> None: | ||
| self.api.get("/test/user") | ||
| self.api.cache.mark(time.time() + 1) | ||
| self.api.get("/test/user") | ||
|
|
||
| expect = ( | ||
| '127.0.0.8:9898 - - * "GET /test/user HTTP/1.1" 200 -\n' | ||
| '127.0.0.8:9898 - - * "GET /test/user HTTP/1.1" 304 -\n' | ||
| ) | ||
|
|
||
| with open(self.api.log.path, "r") as f: | ||
| data = f.read() | ||
|
|
||
| match = fnmatch.fnmatch(data, expect) | ||
| if not match: | ||
| self.fail(f"'{data}' did not match '{expect}'") | ||
| with self.assertLogs('lib.github', level='DEBUG') as logs: | ||
| self.api.get("/test/user") | ||
| self.api.cache.mark(time.time() + 1) | ||
| self.api.get("/test/user") | ||
|
|
||
| netloc = f'{self.server.address[0]}:{self.server.address[1]}' | ||
| self.assertEqual(logs.output, [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation? Doesn't this need to be under the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no. it doesn't. and maybe even shouldn't. the inside the compare that also with the with self.assertRaises(ValueError) as cm:
do_something()
self.assertEqual(str(cm.exception), "invalid value")
self.assertEqual(cm.exception.args, ("invalid value",))because you can't do the compare until after the exception, but the exception is going to shoot you out of the block.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, thanks! I was mostly suprised that "logs" is even available outside of the "with". TIL! |
||
| f'DEBUG:lib.github:{netloc} GET /test/user → 200', | ||
| f'DEBUG:lib.github:{netloc} GET /test/user → 304', | ||
| ]) | ||
|
|
||
| def test_issues_since(self) -> None: | ||
| issues = self.api.issues(since=1499838499) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is "oauth token" the same as "github token" in this context? Why does this message say "oauth token"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is saying "no oauth_token assignment in the config file", got it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya this is specifically about the case where (as a final fallback option) we raid the gh cli config and look for an existing token but fail to find it.