github.com/MontFerret/ferret@v0.18.0/pkg/runtime/program_test.go (about) 1 package runtime_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/compiler" 10 "github.com/MontFerret/ferret/pkg/runtime/core" 11 ) 12 13 type Result struct { 14 Value []byte 15 Error error 16 } 17 18 func TestProgram(t *testing.T) { 19 Convey("Should recover from panic", t, func() { 20 c := compiler.New() 21 c.RegisterFunction("panic", func(ctx context.Context, args ...core.Value) (core.Value, error) { 22 panic("test") 23 }) 24 25 p := c.MustCompile(`RETURN PANIC()`) 26 27 _, err := p.Run(context.Background()) 28 29 So(err, ShouldBeError) 30 So(err.Error(), ShouldEqual, "test") 31 }) 32 33 Convey("Should stop an execution when context is cancelled", t, func() { 34 c := compiler.New() 35 p := c.MustCompile(`WAIT(1000) RETURN TRUE`) 36 37 ctx, cancel := context.WithCancel(context.Background()) 38 cancel() 39 40 _, err := p.Run(ctx) 41 42 So(err, ShouldEqual, core.ErrTerminated) 43 }) 44 }