Skip to content

Commit ad8958f

Browse files
committed
test: add unit test for pkg/rm/tcc/config
1 parent afb8650 commit ad8958f

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

pkg/rm/tcc/config_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package tcc
19+
20+
import (
21+
"flag"
22+
"testing"
23+
"time"
24+
25+
"github.com/stretchr/testify/assert"
26+
"seata.apache.org/seata-go/pkg/rm/tcc/fence"
27+
)
28+
29+
func TestConfig_DefaultValues(t *testing.T) {
30+
cfg := &Config{}
31+
32+
assert.NotNil(t, cfg.FenceConfig)
33+
assert.False(t, cfg.FenceConfig.Enable)
34+
assert.Equal(t, "", cfg.FenceConfig.Url)
35+
assert.Equal(t, "", cfg.FenceConfig.LogTableName)
36+
assert.Equal(t, time.Duration(0), cfg.FenceConfig.CleanPeriod)
37+
}
38+
39+
func TestConfig_SetValues(t *testing.T) {
40+
cfg := &Config{
41+
FenceConfig: fence.Config{
42+
Enable: true,
43+
Url: "root:password@tcp(localhost:3306)/seata?charset=utf8",
44+
LogTableName: "tcc_fence_log",
45+
CleanPeriod: 60 * time.Second,
46+
},
47+
}
48+
49+
assert.True(t, cfg.FenceConfig.Enable)
50+
assert.Equal(t, "root:password@tcp(localhost:3306)/seata?charset=utf8", cfg.FenceConfig.Url)
51+
assert.Equal(t, "tcc_fence_log", cfg.FenceConfig.LogTableName)
52+
assert.Equal(t, 60*time.Second, cfg.FenceConfig.CleanPeriod)
53+
}
54+
55+
func TestConfig_RegisterFlagsWithPrefix(t *testing.T) {
56+
cfg := &Config{}
57+
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
58+
59+
assert.NotPanics(t, func() {
60+
cfg.RegisterFlagsWithPrefix("", flagSet)
61+
})
62+
63+
assert.NotPanics(t, func() {
64+
cfg.RegisterFlagsWithPrefix("tcc", flagSet)
65+
})
66+
}
67+
68+
func TestConfig_RegisterFlagsWithPrefix_FunctionExists(t *testing.T) {
69+
cfg := &Config{}
70+
71+
assert.NotNil(t, cfg.RegisterFlagsWithPrefix)
72+
73+
assert.IsType(t, func(string, *flag.FlagSet) {}, cfg.RegisterFlagsWithPrefix)
74+
}
75+
76+
func TestConfig_FieldTypes(t *testing.T) {
77+
cfg := &Config{}
78+
79+
assert.IsType(t, fence.Config{}, cfg.FenceConfig)
80+
}
81+
82+
func TestConfig_StructInstantiation(t *testing.T) {
83+
cfg1 := Config{}
84+
cfg2 := &Config{}
85+
cfg3 := new(Config)
86+
87+
assert.NotNil(t, &cfg1)
88+
assert.NotNil(t, cfg2)
89+
assert.NotNil(t, cfg3)
90+
91+
assert.False(t, cfg1.FenceConfig.Enable)
92+
assert.False(t, cfg2.FenceConfig.Enable)
93+
assert.False(t, cfg3.FenceConfig.Enable)
94+
}
95+
96+
func TestConfig_FieldAssignment(t *testing.T) {
97+
cfg := &Config{}
98+
99+
cfg.FenceConfig.Enable = true
100+
cfg.FenceConfig.Url = "custom-url"
101+
cfg.FenceConfig.LogTableName = "custom_table"
102+
cfg.FenceConfig.CleanPeriod = 30 * time.Second
103+
104+
assert.True(t, cfg.FenceConfig.Enable)
105+
assert.Equal(t, "custom-url", cfg.FenceConfig.Url)
106+
assert.Equal(t, "custom_table", cfg.FenceConfig.LogTableName)
107+
assert.Equal(t, 30*time.Second, cfg.FenceConfig.CleanPeriod)
108+
}
109+
110+
func TestConfig_ExtremeValues(t *testing.T) {
111+
cfg := &Config{
112+
FenceConfig: fence.Config{
113+
Enable: true,
114+
Url: "very-long-url-string-that-exceeds-normal-length-very-long-url-string-that-exceeds-normal-length-very-long-url-string-that-exceeds-normal-length",
115+
LogTableName: "very_long_table_name_that_exceeds_normal_length_very_long_table_name_that_exceeds_normal_length",
116+
CleanPeriod: 24 * time.Hour,
117+
},
118+
}
119+
120+
assert.True(t, cfg.FenceConfig.Enable)
121+
assert.Equal(t, "very-long-url-string-that-exceeds-normal-length-very-long-url-string-that-exceeds-normal-length-very-long-url-string-that-exceeds-normal-length", cfg.FenceConfig.Url)
122+
assert.Equal(t, "very_long_table_name_that_exceeds_normal_length_very_long_table_name_that_exceeds_normal_length", cfg.FenceConfig.LogTableName)
123+
assert.Equal(t, 24*time.Hour, cfg.FenceConfig.CleanPeriod)
124+
}
125+
126+
func TestConfig_EmptyValues(t *testing.T) {
127+
cfg := &Config{
128+
FenceConfig: fence.Config{
129+
Enable: false,
130+
Url: "",
131+
LogTableName: "",
132+
CleanPeriod: 0,
133+
},
134+
}
135+
136+
assert.False(t, cfg.FenceConfig.Enable)
137+
assert.Equal(t, "", cfg.FenceConfig.Url)
138+
assert.Equal(t, "", cfg.FenceConfig.LogTableName)
139+
assert.Equal(t, time.Duration(0), cfg.FenceConfig.CleanPeriod)
140+
}
141+
142+
func TestConfig_NestedStructure(t *testing.T) {
143+
cfg := &Config{}
144+
145+
assert.NotNil(t, cfg.FenceConfig)
146+
147+
cfg.FenceConfig.Enable = true
148+
assert.True(t, cfg.FenceConfig.Enable)
149+
150+
cfg.FenceConfig.Url = "test-url"
151+
assert.Equal(t, "test-url", cfg.FenceConfig.Url)
152+
153+
cfg.FenceConfig.LogTableName = "test_table"
154+
assert.Equal(t, "test_table", cfg.FenceConfig.LogTableName)
155+
156+
cfg.FenceConfig.CleanPeriod = 5 * time.Minute
157+
assert.Equal(t, 5*time.Minute, cfg.FenceConfig.CleanPeriod)
158+
}
159+
160+
func TestConfig_RegisterFlagsWithPrefix_Delegation(t *testing.T) {
161+
cfg := &Config{}
162+
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
163+
164+
cfg.RegisterFlagsWithPrefix("tcc", flagSet)
165+
166+
assert.NotNil(t, flagSet)
167+
}
168+
169+
func TestConfig_ZeroValueHandling(t *testing.T) {
170+
var cfg Config
171+
172+
assert.False(t, cfg.FenceConfig.Enable)
173+
assert.Equal(t, "", cfg.FenceConfig.Url)
174+
assert.Equal(t, "", cfg.FenceConfig.LogTableName)
175+
assert.Equal(t, time.Duration(0), cfg.FenceConfig.CleanPeriod)
176+
177+
cfg.FenceConfig.Enable = true
178+
cfg.FenceConfig.Url = "assigned-url"
179+
180+
assert.True(t, cfg.FenceConfig.Enable)
181+
assert.Equal(t, "assigned-url", cfg.FenceConfig.Url)
182+
}

0 commit comments

Comments
 (0)