This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
fix: settings run id #32
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,10 @@ | |
| @description: 测试 settings | ||
| """ | ||
|
|
||
| import os.path | ||
| import time | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
|
|
@@ -26,3 +30,67 @@ def test_lazy_settings(): | |
| settings.exp_colors = ("green", "yellow") | ||
| with pytest.raises(ValueError, match="description can only be set once"): | ||
| settings.description = "test description 2" | ||
|
|
||
|
|
||
| def test_settings(): | ||
| """ | ||
| 主要测试 settings 初始化时遇见相同文件夹自动创建新文件夹的功能 | ||
| """ | ||
| from swankit.core.settings import SwanLabSharedSettings | ||
| from tutils import TEMP_DIR | ||
|
|
||
| # 测试创建文件夹 | ||
| # 尽量保持代码运行在一秒的开始 | ||
| time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒 | ||
|
||
| start = time.time() | ||
| run_id = "111111" | ||
| run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id)) | ||
| settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=True) | ||
| assert time.time() - start < 1 # 此时不应该等待一秒 | ||
| assert settings.run_dir == run_dir | ||
| assert settings.run_id == run_id | ||
|
|
||
| # 测试创建同名文件夹 | ||
| # 尽量保持代码运行在一秒的开始 | ||
| time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒 | ||
| start = time.time() | ||
| # 测试创建一个存在的文件夹 | ||
| run_id = "123456" | ||
| run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id)) | ||
| os.mkdir(run_dir) | ||
|
||
| settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=True) | ||
| assert 2 > time.time() - start >= 1 # 确保至少等待了一秒 | ||
| assert settings.run_dir != run_dir # 确保没有使用原来的文件夹 | ||
| assert settings.run_id == run_id # 确保run_id正确 | ||
|
|
||
|
|
||
| def test_settings_no_save(): | ||
| """ | ||
| 测试不保存的情况,此时不会检查文件夹唯一性 | ||
| """ | ||
| from swankit.core.settings import SwanLabSharedSettings | ||
| from tutils import TEMP_DIR | ||
|
|
||
| # 测试创建文件夹 | ||
| # 尽量保持代码运行在一秒的开始 | ||
| time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒 | ||
| start = time.time() | ||
| run_id = "111111" | ||
| run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id)) | ||
| settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=False) | ||
| assert time.time() - start < 1 # 此时不应该等待一秒 | ||
| assert settings.run_dir == run_dir | ||
| assert settings.run_id == run_id | ||
|
|
||
| # 测试创建同名文件夹 | ||
| # 尽量保持代码运行在一秒的开始 | ||
| time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒 | ||
| start = time.time() | ||
| # 测试创建一个存在的文件夹 | ||
| run_id = "123456" | ||
| run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id)) | ||
| os.mkdir(run_dir) | ||
| settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=False) | ||
| assert time.time() - start < 1 # 此时不应该等待一秒 | ||
| assert settings.run_dir == run_dir | ||
| assert settings.run_id == run_id | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using
andfor control flow and side-effects here is subtle. Consider replacing this with an explicitif run_dir is not None: time.sleep(1)to improve readability.