Skip to content

Commit 2fd68d5

Browse files
authored
Polish SystemRulesUpdater and FlowRulesUpdater in data-source helper (alibaba#92)
* Hotfix: If the updated value is nil in PropertyUpdater, sentinel should clear downstream property.
1 parent a24a42d commit 2fd68d5

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

ext/datasource/helper.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func FlowRulesJsonConverter(src []byte) (interface{}, error) {
2424
// FlowRulesUpdater load the newest []flow.FlowRule to downstream flow component.
2525
func FlowRulesUpdater(data interface{}) error {
2626
if data == nil {
27-
return nil
27+
return flow.ClearRules()
2828
}
2929

3030
rules := make([]*flow.FlowRule, 0)
@@ -64,7 +64,7 @@ func SystemRulesJsonConverter(src []byte) (interface{}, error) {
6464
// SystemRulesUpdater load the newest []system.SystemRule to downstream system component.
6565
func SystemRulesUpdater(data interface{}) error {
6666
if data == nil {
67-
return nil
67+
return system.ClearRules()
6868
}
6969

7070
rules := make([]*system.SystemRule, 0)

ext/datasource/helper_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ func TestFlowRulesJsonConverter(t *testing.T) {
8181
func TestFlowRulesUpdater(t *testing.T) {
8282
t.Run("TestFlowRulesUpdater_Nil", func(t *testing.T) {
8383
flow.ClearRules()
84+
flow.LoadRules([]*flow.FlowRule{
85+
{
86+
ID: 0,
87+
Resource: "abc",
88+
LimitOrigin: "default",
89+
MetricType: 0,
90+
Count: 0,
91+
RelationStrategy: 0,
92+
ControlBehavior: 0,
93+
RefResource: "",
94+
WarmUpPeriodSec: 0,
95+
MaxQueueingTimeMs: 0,
96+
ClusterMode: false,
97+
ClusterConfig: flow.ClusterRuleConfig{},
98+
}})
99+
assert.True(t, len(flow.GetRules()) == 1, "Fail to prepare test data.")
84100
err := FlowRulesUpdater(nil)
85101
assert.True(t, err == nil && len(flow.GetRules()) == 0, "Fail to test TestFlowRulesUpdater_Nil")
86102
})
@@ -189,6 +205,15 @@ func TestSystemRulesJsonConvert(t *testing.T) {
189205
func TestSystemRulesUpdater(t *testing.T) {
190206
t.Run("TestSystemRulesUpdater_Nil", func(t *testing.T) {
191207
system.ClearRules()
208+
system.LoadRules([]*system.SystemRule{
209+
&system.SystemRule{
210+
ID: 0,
211+
MetricType: 0,
212+
TriggerCount: 0,
213+
Strategy: 0,
214+
},
215+
})
216+
assert.True(t, len(system.GetRules()) == 1, "Fail to prepare data.")
192217
err := SystemRulesUpdater(nil)
193218
assert.True(t, err == nil && len(system.GetRules()) == 0, "Fail to test TestSystemRulesUpdater_Nil")
194219
})

0 commit comments

Comments
 (0)