github.com/jpedro/terraform@v0.11.12-beta1/helper/shadow/closer_test.go (about)

     1  package shadow
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestClose(t *testing.T) {
     9  	var foo struct {
    10  		A Value
    11  		B KeyedValue
    12  	}
    13  
    14  	if err := Close(&foo); err != nil {
    15  		t.Fatalf("err: %s", err)
    16  	}
    17  
    18  	if v := foo.A.Value(); v != ErrClosed {
    19  		t.Fatalf("bad: %#v", v)
    20  	}
    21  	if v := foo.B.Value("foo"); v != ErrClosed {
    22  		t.Fatalf("bad: %#v", v)
    23  	}
    24  }
    25  
    26  func TestClose_nonPtr(t *testing.T) {
    27  	var foo struct{}
    28  
    29  	if err := Close(foo); err == nil {
    30  		t.Fatal("should error")
    31  	}
    32  }
    33  
    34  func TestClose_unexported(t *testing.T) {
    35  	var foo struct {
    36  		A Value
    37  		b Value
    38  	}
    39  
    40  	if err := Close(&foo); err != nil {
    41  		t.Fatalf("err: %s", err)
    42  	}
    43  
    44  	if v := foo.A.Value(); v != ErrClosed {
    45  		t.Fatalf("bad: %#v", v)
    46  	}
    47  
    48  	// Start trying to get the value
    49  	valueCh := make(chan interface{})
    50  	go func() {
    51  		valueCh <- foo.b.Value()
    52  	}()
    53  
    54  	// We should not get the value
    55  	select {
    56  	case <-valueCh:
    57  		t.Fatal("shouldn't receive value")
    58  	case <-time.After(10 * time.Millisecond):
    59  	}
    60  
    61  	// Set the value
    62  	foo.b.Close()
    63  	val := <-valueCh
    64  
    65  	// Verify
    66  	if val != ErrClosed {
    67  		t.Fatalf("bad: %#v", val)
    68  	}
    69  }