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

     1  package shadow
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestOrderedValue(t *testing.T) {
     9  	var v OrderedValue
    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 not get the value again
    34  	go func() {
    35  		valueCh <- v.Value()
    36  	}()
    37  	select {
    38  	case <-valueCh:
    39  		t.Fatal("shouldn't receive value")
    40  	case <-time.After(10 * time.Millisecond):
    41  	}
    42  
    43  	// We should get the next value
    44  	v.SetValue(21)
    45  	val = <-valueCh
    46  	if val != 21 {
    47  		t.Fatalf("bad: %#v", val)
    48  	}
    49  }
    50  
    51  func TestOrderedValue_setFirst(t *testing.T) {
    52  	var v OrderedValue
    53  
    54  	// Set the value
    55  	v.SetValue(42)
    56  	val := v.Value()
    57  
    58  	// Verify
    59  	if val != 42 {
    60  		t.Fatalf("bad: %#v", val)
    61  	}
    62  
    63  	// We should not get the value again
    64  	valueCh := make(chan interface{})
    65  	go func() {
    66  		valueCh <- v.Value()
    67  	}()
    68  	select {
    69  	case <-valueCh:
    70  		t.Fatal("shouldn't receive value")
    71  	case <-time.After(10 * time.Millisecond):
    72  	}
    73  
    74  	// Set a value so the goroutine doesn't hang around
    75  	v.SetValue(1)
    76  }