This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from looplab/fsm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow async state transition to be canceled
This adds a context and cancelation facility to the type `AsyncError`. Async state transitions can now be canceled by calling `CancelTransition` on the AsyncError returned by `fsm.Event`. The context on that error can also be handed off as described in looplab#77 (comment).
- Loading branch information
1 parent
74cac90
commit 7db91e5
Showing
5 changed files
with
201 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package fsm | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type uncancel struct { | ||
context.Context | ||
} | ||
|
||
func (*uncancel) Deadline() (deadline time.Time, ok bool) { return } | ||
func (*uncancel) Done() <-chan struct{} { return nil } | ||
func (*uncancel) Err() error { return nil } | ||
|
||
// uncancelContext returns a context which ignores the cancellation of the parent and only keeps the values. | ||
// Also returns a new cancel function. | ||
// This is useful to keep a background task running while the initial request is finished. | ||
func uncancelContext(ctx context.Context) (context.Context, context.CancelFunc) { | ||
return context.WithCancel(&uncancel{ctx}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package fsm | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestUncancel(t *testing.T) { | ||
t.Run("create a new context", func(t *testing.T) { | ||
t.Run("and cancel it", func(t *testing.T) { | ||
ctx := context.Background() | ||
ctx = context.WithValue(ctx, "key1", "value1") | ||
ctx, cancelFunc := context.WithCancel(ctx) | ||
cancelFunc() | ||
|
||
if ctx.Err() != context.Canceled { | ||
t.Errorf("expected context error 'context canceled', got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
default: | ||
t.Error("expected context to be done but it wasn't") | ||
} | ||
|
||
t.Run("and uncancel it", func(t *testing.T) { | ||
ctx, newCancelFunc := uncancelContext(ctx) | ||
if ctx.Err() != nil { | ||
t.Errorf("expected context error to be nil, got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
t.Fail() | ||
default: | ||
} | ||
|
||
t.Run("now it should still contain the values", func(t *testing.T) { | ||
if ctx.Value("key1") != "value1" { | ||
t.Errorf("expected context value of key 'key1' to be 'value1', got %v", ctx.Value("key1")) | ||
} | ||
}) | ||
t.Run("and cancel the child", func(t *testing.T) { | ||
newCancelFunc() | ||
if ctx.Err() != context.Canceled { | ||
t.Errorf("expected context error 'context canceled', got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
default: | ||
t.Error("expected context to be done but it wasn't") | ||
} | ||
}) | ||
}) | ||
}) | ||
t.Run("and uncancel it", func(t *testing.T) { | ||
ctx := context.Background() | ||
parent := ctx | ||
ctx, newCancelFunc := uncancelContext(ctx) | ||
if ctx.Err() != nil { | ||
t.Errorf("expected context error to be nil, got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
t.Fail() | ||
default: | ||
} | ||
|
||
t.Run("and cancel the child", func(t *testing.T) { | ||
newCancelFunc() | ||
if ctx.Err() != context.Canceled { | ||
t.Errorf("expected context error 'context canceled', got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
default: | ||
t.Error("expected context to be done but it wasn't") | ||
} | ||
|
||
t.Run("and ensure the parent is not affected", func(t *testing.T) { | ||
if parent.Err() != nil { | ||
t.Errorf("expected parent context error to be nil, got %v", ctx.Err()) | ||
} | ||
select { | ||
case <-parent.Done(): | ||
t.Fail() | ||
default: | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |