Skip to content

Commit

Permalink
Merge pull request #48 from balamg/issue47
Browse files Browse the repository at this point in the history
Fix for #47 : A tuple remains in the rule session's network after retract is invoked
  • Loading branch information
balamg authored May 1, 2019
2 parents 93c6ed6 + 43d4d72 commit cc15574
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions rete/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ func (nw *reteNetworkImpl) retractInternal(ctx context.Context, tuple model.Tupl
if mode == DELETE {
rCtx.addToRtcDeleted(tuple)
}
delete (nw.allHandles, tuple.GetKey().String())
}
}

Expand Down
109 changes: 109 additions & 0 deletions ruleapi/tests/retract_1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package tests

import (
"context"
"github.com/project-flogo/rules/common/model"
"github.com/project-flogo/rules/ruleapi"
"testing"
)

//Retract
func Test_Retract_1(t *testing.T) {

rs, _ := createRuleSession()

//create a rule joining t1 and t3
rule := ruleapi.NewRule("Retract_Test")
err := rule.AddCondition("R7_c1", []string{"t1.none", "t3.none"}, trueCondition, nil)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
rule.SetAction(assert_action)
rule.SetPriority(1)
err = rs.AddRule(rule)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
t.Logf("Rule added: [%s]\n", rule.GetName())

err = rs.Start(nil)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}

//assert a t1
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t1", "t1")
err := rs.Assert(ctx, tuple)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
}
//assert a t3 so that the rule fires for keys t1 and t3
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
err := rs.Assert(ctx, tuple)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
}
//now retract t3
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
rs.Retract(ctx, tuple)
}
/**
now assert with same key again, see that the test does not fail, and rule fires
for keys t1 and t3
*/

{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
err := rs.Assert(ctx, tuple)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
}
/**
now retract t3 again, just to check if a subsequent t1 does not fire the rule
there by proving that t3 has been retracted
*/
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
rs.Retract(ctx, tuple)
}

/**
now assert another t1 with a different key and observe that rule does not fire
for keys t3 and t11 (since t3 has been retracted)
*/
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t1", "t11")
err := rs.Assert(ctx, tuple)
if err != nil {
t.Logf("%s", err)
t.FailNow()
}
}
rs.Unregister()

}

func assert_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t := ctx.Value("key").(*testing.T)
t1 := tuples["t1"]
t3 := tuples["t3"]
t.Logf("Rule fired.. [%s], [%s]\n", t1.GetKey().String(), t3.GetKey().String())
}
23 changes: 22 additions & 1 deletion ruleapi/tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@
"type":"string"
}
]
},
{
"name":"t3",
"properties":[
{
"name":"id",
"type":"string",
"pk-index":0
},
{
"name":"p1",
"type":"int"
},
{
"name":"p2",
"type":"double"
},
{
"name":"p3",
"type":"string"
}
]
}

]

0 comments on commit cc15574

Please sign in to comment.