github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/helper/shadow/value_test.go (about)

     1  package shadow
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestValue(t *testing.T) {
     9  	var v Value
    10  
    11  	// Start trying to get the value
    12  	valueCh := make(chan interface{})
    13  	go func() {
    14  		valueCh <- v.Value()
    15  	}()
    16  
    17  	// We should not get the value
    18  	select {
    19  	case <-valueCh:
    20  		t.Fatal("shouldn't receive value")
    21  	case <-time.After(10 * time.Millisecond):
    22  	}
    23  
    24  	// Set the value
    25  	v.SetValue(42)
    26  	val := <-valueCh
    27  
    28  	// Verify
    29  	if val != 42 {
    30  		t.Fatalf("bad: %#v", val)
    31  	}
    32  
    33  	// We should be able to ask for the value again immediately
    34  	if val := v.Value(); val != 42 {
    35  		t.Fatalf("bad: %#v", val)
    36  	}
    37  
    38  	// We can change the value
    39  	v.SetValue(84)
    40  	if val := v.Value(); val != 84 {
    41  		t.Fatalf("bad: %#v", val)
    42  	}
    43  }
    44  
    45  func TestValueClose(t *testing.T) {
    46  	var v Value
    47  
    48  	// Close
    49  	v.Close()
    50  
    51  	// Verify
    52  	val := v.Value()
    53  	if val != ErrClosed {
    54  		t.Fatalf("bad: %#v", val)
    55  	}
    56  }
    57  
    58  func TestValueClose_blocked(t *testing.T) {
    59  	var v Value
    60  
    61  	// Start trying to get the value
    62  	valueCh := make(chan interface{})
    63  	go func() {
    64  		valueCh <- v.Value()
    65  	}()
    66  
    67  	// We should not get the value
    68  	select {
    69  	case <-valueCh:
    70  		t.Fatal("shouldn't receive value")
    71  	case <-time.After(10 * time.Millisecond):
    72  	}
    73  
    74  	// Set the value
    75  	v.Close()
    76  	val := <-valueCh
    77  
    78  	// Verify
    79  	if val != ErrClosed {
    80  		t.Fatalf("bad: %#v", val)
    81  	}
    82  
    83  	// We should be able to ask for the value again immediately
    84  	if val := v.Value(); val != ErrClosed {
    85  		t.Fatalf("bad: %#v", val)
    86  	}
    87  }
    88  
    89  func TestValueClose_existing(t *testing.T) {
    90  	var v Value
    91  
    92  	// Set the value
    93  	v.SetValue(42)
    94  
    95  	// Close
    96  	v.Close()
    97  
    98  	// Verify
    99  	val := v.Value()
   100  	if val != 42 {
   101  		t.Fatalf("bad: %#v", val)
   102  	}
   103  }