opentofu/helper/shadow/closer_test.go
Mitchell Hashimoto 6557a3de18
helper/shadow: Close for auto-closing all values
Fixes #10122

The simple fix was that we forgot to close `ReadDataApply` for the
provider. But I've always felt that this section of the code was brittle
and I wanted to put in a more robust solution. The `shadow.Close` method
uses reflection to automatically close all values.
2016-11-15 08:54:26 -08:00

70 lines
1.0 KiB
Go

package shadow
import (
"testing"
"time"
)
func TestClose(t *testing.T) {
var foo struct {
A Value
B KeyedValue
}
if err := Close(&foo); err != nil {
t.Fatalf("err: %s", err)
}
if v := foo.A.Value(); v != ErrClosed {
t.Fatalf("bad: %#v", v)
}
if v := foo.B.Value("foo"); v != ErrClosed {
t.Fatalf("bad: %#v", v)
}
}
func TestClose_nonPtr(t *testing.T) {
var foo struct{}
if err := Close(foo); err == nil {
t.Fatal("should error")
}
}
func TestClose_unexported(t *testing.T) {
var foo struct {
A Value
b Value
}
if err := Close(&foo); err != nil {
t.Fatalf("err: %s", err)
}
if v := foo.A.Value(); v != ErrClosed {
t.Fatalf("bad: %#v", v)
}
// Start trying to get the value
valueCh := make(chan interface{})
go func() {
valueCh <- foo.b.Value()
}()
// We should not get the value
select {
case <-valueCh:
t.Fatal("shouldn't receive value")
case <-time.After(10 * time.Millisecond):
}
// Set the value
foo.b.Close()
val := <-valueCh
// Verify
if val != ErrClosed {
t.Fatalf("bad: %#v", val)
}
}