github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/helper/shadow/keyed_value_test.go (about)

     1  package shadow
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestKeyedValue(t *testing.T) {
     9  	var v KeyedValue
    10  
    11  	// Start trying to get the value
    12  	valueCh := make(chan interface{})
    13  	go func() {
    14  		valueCh <- v.Value("foo")
    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("foo", 42)
    26  	val := <-valueCh
    27  
    28  	// Verify
    29  	if val != 42 {
    30  		t.Fatalf("bad: %#v", val)
    31  	}
    32  
    33  	// We should get the next value
    34  	val = v.Value("foo")
    35  	if val != 42 {
    36  		t.Fatalf("bad: %#v", val)
    37  	}
    38  }
    39  
    40  func TestKeyedValue_setFirst(t *testing.T) {
    41  	var v KeyedValue
    42  
    43  	// Set the value
    44  	v.SetValue("foo", 42)
    45  	val := v.Value("foo")
    46  
    47  	// Verify
    48  	if val != 42 {
    49  		t.Fatalf("bad: %#v", val)
    50  	}
    51  }
    52  
    53  func TestKeyedValueOk(t *testing.T) {
    54  	var v KeyedValue
    55  
    56  	// Try
    57  	val, ok := v.ValueOk("foo")
    58  	if ok {
    59  		t.Fatal("should not be ok")
    60  	}
    61  
    62  	// Set
    63  	v.SetValue("foo", 42)
    64  
    65  	// Try again
    66  	val, ok = v.ValueOk("foo")
    67  	if !ok {
    68  		t.Fatal("should be ok")
    69  	}
    70  
    71  	// Verify
    72  	if val != 42 {
    73  		t.Fatalf("bad: %#v", val)
    74  	}
    75  }
    76  
    77  func TestKeyedValueClose(t *testing.T) {
    78  	var v KeyedValue
    79  
    80  	// Close
    81  	v.Close()
    82  
    83  	// Try again
    84  	val, ok := v.ValueOk("foo")
    85  	if !ok {
    86  		t.Fatal("should be ok")
    87  	}
    88  
    89  	// Verify
    90  	if val != ErrClosed {
    91  		t.Fatalf("bad: %#v", val)
    92  	}
    93  }
    94  
    95  func TestKeyedValueClose_blocked(t *testing.T) {
    96  	var v KeyedValue
    97  
    98  	// Start reading this should be blocking
    99  	valueCh := make(chan interface{})
   100  	go func() {
   101  		valueCh <- v.Value("foo")
   102  	}()
   103  
   104  	// We should not get the value
   105  	select {
   106  	case <-valueCh:
   107  		t.Fatal("shouldn't receive value")
   108  	case <-time.After(10 * time.Millisecond):
   109  	}
   110  
   111  	// Close
   112  	v.Close()
   113  
   114  	// Verify
   115  	val := <-valueCh
   116  	if val != ErrClosed {
   117  		t.Fatalf("bad: %#v", val)
   118  	}
   119  }
   120  
   121  func TestKeyedValueClose_existing(t *testing.T) {
   122  	var v KeyedValue
   123  
   124  	// Set a value
   125  	v.SetValue("foo", "bar")
   126  
   127  	// Close
   128  	v.Close()
   129  
   130  	// Try again
   131  	val, ok := v.ValueOk("foo")
   132  	if !ok {
   133  		t.Fatal("should be ok")
   134  	}
   135  
   136  	// Verify
   137  	if val != "bar" {
   138  		t.Fatalf("bad: %#v", val)
   139  	}
   140  }
   141  
   142  func TestKeyedValueClose_existingBlocked(t *testing.T) {
   143  	var v KeyedValue
   144  
   145  	// Start reading this should be blocking
   146  	valueCh := make(chan interface{})
   147  	go func() {
   148  		valueCh <- v.Value("foo")
   149  	}()
   150  
   151  	// Wait
   152  	time.Sleep(10 * time.Millisecond)
   153  
   154  	// Set a value
   155  	v.SetValue("foo", "bar")
   156  
   157  	// Close
   158  	v.Close()
   159  
   160  	// Try again
   161  	val, ok := v.ValueOk("foo")
   162  	if !ok {
   163  		t.Fatal("should be ok")
   164  	}
   165  
   166  	// Verify
   167  	if val != "bar" {
   168  		t.Fatalf("bad: %#v", val)
   169  	}
   170  }
   171  
   172  func TestKeyedValueInit(t *testing.T) {
   173  	var v KeyedValue
   174  
   175  	v.Init("foo", 42)
   176  
   177  	// We should get the value
   178  	val := v.Value("foo")
   179  	if val != 42 {
   180  		t.Fatalf("bad: %#v", val)
   181  	}
   182  
   183  	// We should get the value
   184  	val = v.Value("foo")
   185  	if val != 42 {
   186  		t.Fatalf("bad: %#v", val)
   187  	}
   188  
   189  	// This should do nothing
   190  	v.Init("foo", 84)
   191  
   192  	// We should get the value
   193  	val = v.Value("foo")
   194  	if val != 42 {
   195  		t.Fatalf("bad: %#v", val)
   196  	}
   197  }
   198  
   199  func TestKeyedValueInit_set(t *testing.T) {
   200  	var v KeyedValue
   201  
   202  	v.SetValue("foo", 42)
   203  
   204  	// We should get the value
   205  	val := v.Value("foo")
   206  	if val != 42 {
   207  		t.Fatalf("bad: %#v", val)
   208  	}
   209  
   210  	// We should get the value
   211  	val = v.Value("foo")
   212  	if val != 42 {
   213  		t.Fatalf("bad: %#v", val)
   214  	}
   215  
   216  	// This should do nothing
   217  	v.Init("foo", 84)
   218  
   219  	// We should get the value
   220  	val = v.Value("foo")
   221  	if val != 42 {
   222  		t.Fatalf("bad: %#v", val)
   223  	}
   224  }
   225  
   226  func TestKeyedValueWaitForChange(t *testing.T) {
   227  	var v KeyedValue
   228  
   229  	// Set a value
   230  	v.SetValue("foo", 42)
   231  
   232  	// Start reading this should be blocking
   233  	valueCh := make(chan interface{})
   234  	go func() {
   235  		valueCh <- v.WaitForChange("foo")
   236  	}()
   237  
   238  	// We should not get the value
   239  	select {
   240  	case <-valueCh:
   241  		t.Fatal("shouldn't receive value")
   242  	case <-time.After(10 * time.Millisecond):
   243  	}
   244  
   245  	// Set a new value
   246  	v.SetValue("foo", 84)
   247  
   248  	// Verify
   249  	val := <-valueCh
   250  	if val != 84 {
   251  		t.Fatalf("bad: %#v", val)
   252  	}
   253  }
   254  
   255  func TestKeyedValueWaitForChange_initial(t *testing.T) {
   256  	var v KeyedValue
   257  
   258  	// Start reading this should be blocking
   259  	valueCh := make(chan interface{})
   260  	go func() {
   261  		valueCh <- v.WaitForChange("foo")
   262  	}()
   263  
   264  	// We should not get the value
   265  	select {
   266  	case <-valueCh:
   267  		t.Fatal("shouldn't receive value")
   268  	case <-time.After(10 * time.Millisecond):
   269  	}
   270  
   271  	// Set a new value
   272  	v.SetValue("foo", 84)
   273  
   274  	// Verify
   275  	val := <-valueCh
   276  	if val != 84 {
   277  		t.Fatalf("bad: %#v", val)
   278  	}
   279  }
   280  
   281  func TestKeyedValueWaitForChange_closed(t *testing.T) {
   282  	var v KeyedValue
   283  
   284  	// Start reading this should be blocking
   285  	valueCh := make(chan interface{})
   286  	go func() {
   287  		valueCh <- v.WaitForChange("foo")
   288  	}()
   289  
   290  	// We should not get the value
   291  	select {
   292  	case <-valueCh:
   293  		t.Fatal("shouldn't receive value")
   294  	case <-time.After(10 * time.Millisecond):
   295  	}
   296  
   297  	// Close
   298  	v.Close()
   299  
   300  	// Verify
   301  	val := <-valueCh
   302  	if val != ErrClosed {
   303  		t.Fatalf("bad: %#v", val)
   304  	}
   305  
   306  	// Set a value
   307  	v.SetValue("foo", 42)
   308  
   309  	// Try again
   310  	val = v.WaitForChange("foo")
   311  	if val != ErrClosed {
   312  		t.Fatalf("bad: %#v", val)
   313  	}
   314  }
   315  
   316  func TestKeyedValueWaitForChange_closedFirst(t *testing.T) {
   317  	var v KeyedValue
   318  
   319  	// Close
   320  	v.Close()
   321  
   322  	// Verify
   323  	val := v.WaitForChange("foo")
   324  	if val != ErrClosed {
   325  		t.Fatalf("bad: %#v", val)
   326  	}
   327  
   328  	// Set a value
   329  	v.SetValue("foo", 42)
   330  
   331  	// Try again
   332  	val = v.WaitForChange("foo")
   333  	if val != ErrClosed {
   334  		t.Fatalf("bad: %#v", val)
   335  	}
   336  }