Skip to content

Commit 6025998

Browse files
committed
fix: test error in cloud mode
1 parent c11b7cf commit 6025998

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

swanlab/data/run/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def __init__(
8686
should_save=not self.__operator.disabled,
8787
version=get_package_version(),
8888
)
89+
self.__mode = get_mode()
8990
self.__public = SwanLabPublicConfig(self.__project_name, self.__settings)
9091
self.__operator.before_run(self.__settings)
9192
# ---------------------------------- 初始化日志记录器 ----------------------------------
@@ -216,7 +217,7 @@ def public(self):
216217

217218
@property
218219
def mode(self) -> str:
219-
return get_mode()
220+
return self.__mode
220221

221222
@property
222223
def state(self) -> SwanLabRunState:

swanlab/data/run/public.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from swankit.core import SwanLabSharedSettings
22

33
from swanlab.api import get_http
4+
from swanlab.env import get_mode
45
from swanlab.package import get_project_url, get_experiment_url
56

67

@@ -10,7 +11,13 @@ class SwanlabCloudConfig:
1011
"""
1112

1213
def __init__(self):
13-
self.__http = None
14+
# 不可以直接使用get_http,因为init时http对象还没有初始化
15+
self.__mode = get_mode()
16+
if self.__mode == "cloud":
17+
self.__http = get_http()
18+
else:
19+
self.__http = None
20+
self.__available = self.__http is not None
1421

1522
def __get_property_from_http(self, name: str):
1623
"""
@@ -27,12 +34,7 @@ def available(self):
2734
"""
2835
Whether the SwanLab is running in cloud mode.
2936
"""
30-
try:
31-
if self.__http is None:
32-
self.__http = get_http()
33-
return True
34-
except ValueError:
35-
return False
37+
return self.__available
3638

3739
@property
3840
def project_name(self):

test/unit/data/test_sdk.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def setup_function():
2626
在当前测试文件下的每个测试函数执行前后执行
2727
"""
2828
swanlog.disable_log()
29+
run = get_run()
30+
if run is not None:
31+
run.finish()
2932
yield
3033
run = get_run()
3134
if run is not None:
@@ -153,6 +156,8 @@ def test_init_local(self):
153156
run = S.init(mode="local")
154157
assert os.environ[MODE] == "local"
155158
run.log({"TestInitMode": 1}) # 不会报错
159+
assert run.mode == "local"
160+
assert run.public.cloud.available is False
156161
assert get_run() is not None
157162
assert run.public.cloud.project_name is None
158163

@@ -181,6 +186,17 @@ def test_init_error(self):
181186
S.init(mode="123456") # noqa
182187
assert get_run() is None
183188

189+
@pytest.mark.skipif(T.is_skip_cloud_test, reason="skip cloud test")
190+
def test_init_multiple(self):
191+
# 先初始化cloud
192+
self.test_init_cloud()
193+
get_run().finish()
194+
# 再初始化local
195+
self.test_init_local()
196+
get_run().finish()
197+
# 再初始化disabled
198+
self.test_init_disabled()
199+
184200
# ---------------------------------- 测试环境变量输入 ----------------------------------
185201

186202
def test_init_disabled_env(self):

0 commit comments

Comments
 (0)