de7a52e81f
Motivation: 通知规则的事件名此前仅支持精确匹配,无法用一条规则覆盖多类事件(如 trade.* 下的全部交易事件),规则配置维护成本高。本次引入通配符匹配,让规则可按模式批量命中事件,同时保证精确规则优先。 Changes: * 事件名支持 * 和 ? 通配符匹配 * 匹配优先级:精确匹配 > 更具体的通配 > 泛匹配,同分时取更小 ID * 精确命中未命中时回退为列出源下启用规则并按优先级挑选 * 抽取缓存预热逻辑为独立方法 * 更新文档说明事件名通配规则
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package engine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"aiaa-notification-service/internal/model"
|
|
)
|
|
|
|
func TestPickEventRuleExact(t *testing.T) {
|
|
rules := []model.Rule{
|
|
{ID: 1, Event: "trade.*", Enabled: 1},
|
|
{ID: 2, Event: "trade.open", Enabled: 1},
|
|
}
|
|
got := PickEventRule("trade.open", rules)
|
|
if got == nil || got.ID != 2 {
|
|
t.Fatalf("want exact id=2, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestPickEventRuleWildcard(t *testing.T) {
|
|
rules := []model.Rule{
|
|
{ID: 1, Event: "trade.*", Enabled: 1},
|
|
}
|
|
got := PickEventRule("trade.close", rules)
|
|
if got == nil || got.Event != "trade.*" {
|
|
t.Fatalf("want trade.*, got %#v", got)
|
|
}
|
|
if PickEventRule("order.open", rules) != nil {
|
|
t.Fatal("trade.* must not match order.open")
|
|
}
|
|
}
|
|
|
|
func TestPickEventRuleMoreSpecificWildcardWins(t *testing.T) {
|
|
rules := []model.Rule{
|
|
{ID: 1, Event: "*", Enabled: 1},
|
|
{ID: 2, Event: "trade.*", Enabled: 1},
|
|
}
|
|
got := PickEventRule("trade.open", rules)
|
|
if got == nil || got.ID != 2 {
|
|
t.Fatalf("want trade.* id=2, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestPickEventRuleSkipsDisabled(t *testing.T) {
|
|
rules := []model.Rule{
|
|
{ID: 1, Event: "trade.*", Enabled: 0},
|
|
{ID: 2, Event: "*", Enabled: 1},
|
|
}
|
|
got := PickEventRule("trade.open", rules)
|
|
if got == nil || got.ID != 2 {
|
|
t.Fatalf("want * id=2, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestPickEventRuleNoMatch(t *testing.T) {
|
|
rules := []model.Rule{
|
|
{ID: 1, Event: "trade.open", Enabled: 1},
|
|
}
|
|
if PickEventRule("trade.close", rules) != nil {
|
|
t.Fatal("expected no match")
|
|
}
|
|
}
|