7
7
package nacos_test
8
8
9
9
import (
10
+ "net/url"
10
11
"testing"
12
+ "time"
11
13
12
14
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
13
15
"github.com/nacos-group/nacos-sdk-go/v2/vo"
14
16
17
+ "github.com/gogf/gf/v2/encoding/gjson"
15
18
"github.com/gogf/gf/v2/frame/g"
16
19
"github.com/gogf/gf/v2/os/gctx"
17
20
"github.com/gogf/gf/v2/test/gtest"
34
37
DataId : "config.toml" ,
35
38
Group : "test" ,
36
39
}
40
+ configPublishUrl = "http://localhost:8848/nacos/v2/cs/config?type=toml&namespaceId=public&group=test&dataId=config.toml"
37
41
)
38
42
39
43
func TestNacos (t * testing.T ) {
@@ -48,7 +52,6 @@ func TestNacos(t *testing.T) {
48
52
config .SetAdapter (adapter )
49
53
50
54
t .Assert (config .Available (ctx ), true )
51
-
52
55
v , err := config .Get (ctx , `server.address` )
53
56
t .AssertNil (err )
54
57
t .Assert (v .String (), ":8000" )
@@ -58,3 +61,41 @@ func TestNacos(t *testing.T) {
58
61
t .AssertGT (len (m ), 0 )
59
62
})
60
63
}
64
+
65
+ func TestNacosOnConfigChangeFunc (t * testing.T ) {
66
+ gtest .C (t , func (t * gtest.T ) {
67
+ adapter , _ := nacos .New (ctx , nacos.Config {
68
+ ServerConfigs : []constant.ServerConfig {serverConfig },
69
+ ClientConfig : clientConfig ,
70
+ ConfigParam : configParam ,
71
+ Watch : true ,
72
+ OnConfigChange : func (namespace , group , dataId , data string ) {
73
+ gtest .Assert ("public" , namespace )
74
+ gtest .Assert ("test" , group )
75
+ gtest .Assert ("config.toml" , dataId )
76
+ gtest .Assert ("gf" , g .Cfg ().MustGet (gctx .GetInitCtx (), "app.name" ).String ())
77
+ },
78
+ })
79
+ g .Cfg ().SetAdapter (adapter )
80
+ t .Assert (g .Cfg ().Available (ctx ), true )
81
+ appName , err := g .Cfg ().Get (ctx , "app.name" )
82
+ t .AssertNil (err )
83
+ t .Assert (appName .String (), "" )
84
+ c , err := g .Cfg ().Data (ctx )
85
+ t .AssertNil (err )
86
+ j := gjson .New (c )
87
+ err = j .Set ("app.name" , "gf" )
88
+ t .AssertNil (err )
89
+ res , err := j .ToTomlString ()
90
+ t .AssertNil (err )
91
+ _ , err = g .Client ().Post (ctx , configPublishUrl + "&content=" + url .QueryEscape (res ))
92
+ t .AssertNil (err )
93
+ time .Sleep (5 * time .Second )
94
+ err = j .Remove ("app" )
95
+ t .AssertNil (err )
96
+ res2 , err := j .ToTomlString ()
97
+ t .AssertNil (err )
98
+ _ , err = g .Client ().Post (ctx , configPublishUrl + "&content=" + url .QueryEscape (res2 ))
99
+ t .AssertNil (err )
100
+ })
101
+ }
0 commit comments