mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
更新
This commit is contained in:
@@ -20,13 +20,16 @@ func TestNewRuleChainInstance(t *testing.T) {
|
||||
|
||||
func TestScriptEngine(t *testing.T) {
|
||||
metadata := message.NewDefaultMetadata(map[string]interface{}{"device": "aa"})
|
||||
msg := message.NewMessageWithDetail("1", message.MessageTypeConnectEvent, []byte{}, metadata)
|
||||
msg := message.NewMessageWithDetail("1", message.MessageTypeConnectEvent, map[string]interface{}{"aa": 5}, metadata)
|
||||
scriptEngine := nodes.NewScriptEngine()
|
||||
const script = `
|
||||
function Switch(msg, metadata, msgType) {
|
||||
function nextRelation(metadata, msg) {
|
||||
return ['one','nine'];
|
||||
}
|
||||
if(metadata.device === 'aa') {
|
||||
return ['six'];
|
||||
}
|
||||
if(msgType === 'Post telemetry') {
|
||||
return ['two'];
|
||||
}
|
||||
@@ -40,3 +43,22 @@ func TestScriptEngine(t *testing.T) {
|
||||
}
|
||||
t.Log(SwitchResults)
|
||||
}
|
||||
|
||||
func TestScriptOnMessage(t *testing.T) {
|
||||
metadata := message.NewDefaultMetadata(map[string]interface{}{"device": "aa"})
|
||||
msg := message.NewMessageWithDetail("1", message.MessageTypeConnectEvent, map[string]interface{}{"aa": 5}, metadata)
|
||||
scriptEngine := nodes.NewScriptEngine()
|
||||
const script = `
|
||||
function Transform(msg, metadata, msgType) {
|
||||
msg.bb = "33"
|
||||
metadata.event = 55
|
||||
return {msg: msg, metadata: metadata, msgType: msgType};
|
||||
}
|
||||
`
|
||||
ScriptOnMessageResults, err := scriptEngine.ScriptOnMessage(msg, script)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(ScriptOnMessageResults.GetMetadata())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user