expose StopContext in EvalContext

The existing Stopped method predates the use of a context, returning a
simple channel for signaling cancellation. The method was not used
however, so we can just update it to return a context to give us access
to cancellation for ephemeral values.
pull/35778/head
James Bardin 2 years ago
parent a224a45f3b
commit d18638bab3

@ -4,6 +4,8 @@
package terraform
import (
"context"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
@ -28,9 +30,9 @@ import (
// EvalContext is the interface that is given to eval nodes to execute.
type EvalContext interface {
// Stopped returns a channel that is closed when evaluation is stopped
// via Terraform.Context.Stop()
Stopped() <-chan struct{}
// Stopped returns a context that is canceled when evaluation is stopped via
// Terraform.Context.Stop()
StopCtx() context.Context
// Path is the current module path.
Path() addrs.ModuleInstance

@ -102,13 +102,13 @@ func (ctx *BuiltinEvalContext) withScope(scope evalContextScope) EvalContext {
return &newCtx
}
func (ctx *BuiltinEvalContext) Stopped() <-chan struct{} {
func (ctx *BuiltinEvalContext) StopCtx() context.Context {
// This can happen during tests. During tests, we just block forever.
if ctx.StopContext == nil {
return nil
return context.TODO()
}
return ctx.StopContext.Done()
return ctx.StopContext
}
func (ctx *BuiltinEvalContext) Hook(fn func(Hook) (HookAction, error)) error {

@ -4,6 +4,8 @@
package terraform
import (
"context"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
@ -31,8 +33,8 @@ import (
// MockEvalContext is a mock version of EvalContext that can be used
// for tests.
type MockEvalContext struct {
StoppedCalled bool
StoppedValue <-chan struct{}
StopCtxCalled bool
StopCtxValue context.Context
HookCalled bool
HookHook Hook
@ -164,9 +166,9 @@ type MockEvalContext struct {
// MockEvalContext implements EvalContext
var _ EvalContext = (*MockEvalContext)(nil)
func (c *MockEvalContext) Stopped() <-chan struct{} {
c.StoppedCalled = true
return c.StoppedValue
func (c *MockEvalContext) StopCtx() context.Context {
c.StopCtxCalled = true
return c.StopCtxValue
}
func (c *MockEvalContext) Hook(fn func(Hook) (HookAction, error)) error {

Loading…
Cancel
Save