1
+ package org .lowcoder .domain .query .util ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
+
6
+ import java .util .HashMap ;
7
+ import java .util .Map ;
8
+
9
+ import org .junit .jupiter .api .BeforeEach ;
10
+ import org .junit .jupiter .api .Test ;
11
+ import org .lowcoder .sdk .exception .PluginException ;
12
+
13
+ public class QueryTimeoutUtilsTest {
14
+
15
+ private static final int MAX_QUERY_TIMEOUT = 30 ; // 30 seconds
16
+
17
+ private final QueryTimeoutUtils queryTimeoutUtils = new QueryTimeoutUtils ();
18
+ @ BeforeEach
19
+ public void setUp () {
20
+ // Set the default query timeout to 10 seconds (10000 milliseconds)
21
+ queryTimeoutUtils .setDefaultQueryTimeoutMillis (10 );
22
+ }
23
+
24
+ @ Test
25
+ public void testParseQueryTimeoutMs_withValidSeconds () {
26
+ // Test parsing a valid timeout in seconds
27
+ int timeout = QueryTimeoutUtils .parseQueryTimeoutMs ("5s" , MAX_QUERY_TIMEOUT );
28
+ assertEquals (5000 , timeout );
29
+ }
30
+
31
+ @ Test
32
+ public void testParseQueryTimeoutMs_withValidMilliseconds () {
33
+ // Test parsing a valid timeout in milliseconds
34
+ int timeout = QueryTimeoutUtils .parseQueryTimeoutMs ("500ms" , MAX_QUERY_TIMEOUT );
35
+ assertEquals (500 , timeout );
36
+ }
37
+
38
+ @ Test
39
+ public void testParseQueryTimeoutMs_withDefaultTimeout () {
40
+ // Test when the timeout string is null or blank, should return default timeout
41
+ int timeout = QueryTimeoutUtils .parseQueryTimeoutMs ("" , MAX_QUERY_TIMEOUT );
42
+ assertEquals (10000 , timeout ); // Default is 10 seconds (10000 milliseconds)
43
+ }
44
+
45
+ @ Test
46
+ public void testParseQueryTimeoutMs_withExceedingMaxTimeout () {
47
+ // Test when the timeout exceeds the maximum allowed timeout
48
+ assertThrows (PluginException .class , () -> {
49
+ QueryTimeoutUtils .parseQueryTimeoutMs ("60s" , MAX_QUERY_TIMEOUT );
50
+ });
51
+ }
52
+
53
+ @ Test
54
+ public void testParseQueryTimeoutMs_withInvalidTimeout () {
55
+ // Test when the timeout string is invalid
56
+ assertThrows (PluginException .class , () -> {
57
+ QueryTimeoutUtils .parseQueryTimeoutMs ("invalid" , MAX_QUERY_TIMEOUT );
58
+ });
59
+ }
60
+
61
+ @ Test
62
+ public void testParseQueryTimeoutMs_withTemplateString () {
63
+ // Test parsing a template string that includes a parameter
64
+ Map <String , Object > params = new HashMap <>();
65
+ params .put ("timeoutValue" , 5 ); // 5 seconds
66
+
67
+ int timeout = QueryTimeoutUtils .parseQueryTimeoutMs ("{{timeoutValue}}s" , params , MAX_QUERY_TIMEOUT );
68
+ assertEquals (5000 , timeout );
69
+ }
70
+
71
+ @ Test
72
+ public void testParseQueryTimeoutMs_withDefaultTimeoutExceedingMax () {
73
+ // Test when the default timeout exceeds the maximum allowed timeout
74
+ queryTimeoutUtils .setDefaultQueryTimeoutMillis (40 ); // Set default to 40 seconds
75
+
76
+ int timeout = QueryTimeoutUtils .parseQueryTimeoutMs ("" , MAX_QUERY_TIMEOUT );
77
+ assertEquals (30000 , timeout ); // Max query timeout is 30 seconds (30000 milliseconds)
78
+ }
79
+ }
0 commit comments