1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00

allow context cancellation in WAIT (#524)

* allow context cancellation in WAIT

* cancel context before running the program
This commit is contained in:
Paul 2020-06-01 13:58:16 +02:00 committed by GitHub
parent 94f13c66af
commit aa058f3542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 16 deletions

View File

@ -32,23 +32,11 @@ func TestProgram(t *testing.T) {
c := compiler.New()
p := c.MustCompile(`WAIT(1000) RETURN TRUE`)
out := make(chan Result)
ctx, cancel := context.WithCancel(context.Background())
go func() {
v, err := p.Run(ctx)
out <- Result{
Value: v,
Error: err,
}
}()
cancel()
o := <-out
_, err := p.Run(ctx)
So(o.Error, ShouldEqual, core.ErrTerminated)
So(err, ShouldEqual, core.ErrTerminated)
})
}

View File

@ -10,7 +10,7 @@ import (
// Wait pauses the execution for a given period.
// @param timeout (Float|Int) - Number value which indicates for how long to stop an execution.
func Wait(_ context.Context, args ...core.Value) (core.Value, error) {
func Wait(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
@ -19,7 +19,13 @@ func Wait(_ context.Context, args ...core.Value) (core.Value, error) {
arg := values.ToInt(args[0])
time.Sleep(time.Millisecond * time.Duration(arg))
timer := time.NewTimer(time.Millisecond * time.Duration(arg))
select {
case <-ctx.Done():
timer.Stop()
return values.None, ctx.Err()
case <-timer.C:
}
return values.None, nil
}