Skip to content

Commit f1d201d

Browse files
authored
fix: update request timeout to use environment configuration (langgenius#245)
1 parent 3245089 commit f1d201d

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

python/dify_plugin/interfaces/model/openai_compatible/llm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
logger = logging.getLogger(__name__)
4949

50+
_plugin_config = DifyPluginEnv()
51+
5052

5153
def _gen_tool_call_id() -> str:
5254
return f"chatcmpl-tool-{uuid.uuid4().hex!s}"
@@ -473,7 +475,7 @@ def _generate(
473475
endpoint_url,
474476
headers=headers,
475477
json=data,
476-
timeout=(10, DifyPluginEnv.MAX_REQUEST_TIMEOUT),
478+
timeout=(10, _plugin_config.MAX_REQUEST_TIMEOUT),
477479
stream=stream,
478480
)
479481

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Tests for _plugin_config configuration object in openai_compatible/llm.py
3+
4+
These tests verify:
5+
1. Configuration object is properly instantiated at module level
6+
2. Configuration object is a singleton (only one instance at module level)
7+
3. Configuration object has correct default values
8+
4. Configuration object has correct types
9+
"""
10+
11+
from dify_plugin.config.config import DifyPluginEnv
12+
from dify_plugin.interfaces.model.openai_compatible import llm
13+
14+
15+
def test_plugin_config_exists():
16+
"""Test that _plugin_config object exists"""
17+
assert hasattr(llm, "_plugin_config")
18+
assert llm._plugin_config is not None
19+
20+
21+
def test_plugin_config_type():
22+
"""Test that _plugin_config is an instance of DifyPluginEnv"""
23+
assert isinstance(llm._plugin_config, DifyPluginEnv)
24+
25+
26+
def test_plugin_config_has_max_request_timeout():
27+
"""Test that _plugin_config has MAX_REQUEST_TIMEOUT attribute"""
28+
assert hasattr(llm._plugin_config, "MAX_REQUEST_TIMEOUT")
29+
assert isinstance(llm._plugin_config.MAX_REQUEST_TIMEOUT, int)
30+
# Default value should be 300 seconds (unless overridden by environment variable)
31+
assert llm._plugin_config.MAX_REQUEST_TIMEOUT > 0
32+
33+
34+
def test_plugin_config_has_max_invocation_timeout():
35+
"""Test that _plugin_config has MAX_INVOCATION_TIMEOUT attribute"""
36+
assert hasattr(llm._plugin_config, "MAX_INVOCATION_TIMEOUT")
37+
assert isinstance(llm._plugin_config.MAX_INVOCATION_TIMEOUT, int)
38+
# Default value should be 250 seconds (unless overridden by environment variable)
39+
assert llm._plugin_config.MAX_INVOCATION_TIMEOUT > 0
40+
41+
42+
def test_plugin_config_singleton():
43+
"""Test that configuration object is instantiated only once at module level"""
44+
# Get the id of the config object
45+
config_id_1 = id(llm._plugin_config)
46+
47+
# Import again, should be the same object
48+
from dify_plugin.interfaces.model.openai_compatible import llm as llm2
49+
50+
config_id_2 = id(llm2._plugin_config)
51+
52+
# Verify it's the same object
53+
assert config_id_1 == config_id_2
54+
55+
56+
def test_plugin_config_timeout_values_reasonable():
57+
"""Test that timeout configuration values are within reasonable range"""
58+
# MAX_REQUEST_TIMEOUT should be between 1 and 3600 seconds
59+
assert 1 <= llm._plugin_config.MAX_REQUEST_TIMEOUT <= 3600
60+
61+
# MAX_INVOCATION_TIMEOUT should be between 1 and 3600 seconds
62+
assert 1 <= llm._plugin_config.MAX_INVOCATION_TIMEOUT <= 3600
63+
64+
65+
def test_plugin_config_can_create_new_instance():
66+
"""Test that new DifyPluginEnv instances can be created (verify the config class itself works)"""
67+
new_config = DifyPluginEnv()
68+
assert isinstance(new_config, DifyPluginEnv)
69+
assert hasattr(new_config, "MAX_REQUEST_TIMEOUT")
70+
assert hasattr(new_config, "MAX_INVOCATION_TIMEOUT")

0 commit comments

Comments
 (0)