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:
parent
94f13c66af
commit
aa058f3542
@ -32,23 +32,11 @@ func TestProgram(t *testing.T) {
|
|||||||
c := compiler.New()
|
c := compiler.New()
|
||||||
p := c.MustCompile(`WAIT(1000) RETURN TRUE`)
|
p := c.MustCompile(`WAIT(1000) RETURN TRUE`)
|
||||||
|
|
||||||
out := make(chan Result)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
go func() {
|
|
||||||
v, err := p.Run(ctx)
|
|
||||||
|
|
||||||
out <- Result{
|
|
||||||
Value: v,
|
|
||||||
Error: err,
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
o := <-out
|
_, err := p.Run(ctx)
|
||||||
|
|
||||||
So(o.Error, ShouldEqual, core.ErrTerminated)
|
So(err, ShouldEqual, core.ErrTerminated)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
// Wait pauses the execution for a given period.
|
// Wait pauses the execution for a given period.
|
||||||
// @param timeout (Float|Int) - Number value which indicates for how long to stop an execution.
|
// @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)
|
err := core.ValidateArgs(args, 1, 1)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -19,7 +19,13 @@ func Wait(_ context.Context, args ...core.Value) (core.Value, error) {
|
|||||||
|
|
||||||
arg := values.ToInt(args[0])
|
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
|
return values.None, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user