github.com/cmalfait/terraform@v0.11.12-beta1/helper/shadow/compared_value_test.go (about) 1 package shadow 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestComparedValue(t *testing.T) { 9 v := &ComparedValue{ 10 Func: func(k, v interface{}) bool { return k == v }, 11 } 12 13 // Start trying to get the value 14 valueCh := make(chan interface{}) 15 go func() { 16 valueCh <- v.Value("foo") 17 }() 18 19 // We should not get the value 20 select { 21 case <-valueCh: 22 t.Fatal("shouldn't receive value") 23 case <-time.After(10 * time.Millisecond): 24 } 25 26 // Set the value 27 v.SetValue("foo") 28 val := <-valueCh 29 30 // Verify 31 if val != "foo" { 32 t.Fatalf("bad: %#v", val) 33 } 34 35 // We should get the next value 36 val = v.Value("foo") 37 if val != "foo" { 38 t.Fatalf("bad: %#v", val) 39 } 40 } 41 42 func TestComparedValue_setFirst(t *testing.T) { 43 v := &ComparedValue{ 44 Func: func(k, v interface{}) bool { return k == v }, 45 } 46 47 // Set the value 48 v.SetValue("foo") 49 val := v.Value("foo") 50 51 // Verify 52 if val != "foo" { 53 t.Fatalf("bad: %#v", val) 54 } 55 } 56 57 func TestComparedValueOk(t *testing.T) { 58 v := &ComparedValue{ 59 Func: func(k, v interface{}) bool { return k == v }, 60 } 61 62 // Try 63 val, ok := v.ValueOk("foo") 64 if ok { 65 t.Fatal("should not be ok") 66 } 67 68 // Set 69 v.SetValue("foo") 70 71 // Try again 72 val, ok = v.ValueOk("foo") 73 if !ok { 74 t.Fatal("should be ok") 75 } 76 77 // Verify 78 if val != "foo" { 79 t.Fatalf("bad: %#v", val) 80 } 81 } 82 83 func TestComparedValueClose(t *testing.T) { 84 v := &ComparedValue{ 85 Func: func(k, v interface{}) bool { return k == v }, 86 } 87 88 // Close 89 v.Close() 90 91 // Try again 92 val, ok := v.ValueOk("foo") 93 if !ok { 94 t.Fatal("should be ok") 95 } 96 97 // Verify 98 if val != ErrClosed { 99 t.Fatalf("bad: %#v", val) 100 } 101 } 102 103 func TestComparedValueClose_blocked(t *testing.T) { 104 var v ComparedValue 105 106 // Start reading this should be blocking 107 valueCh := make(chan interface{}) 108 go func() { 109 valueCh <- v.Value("foo") 110 }() 111 112 // We should not get the value 113 select { 114 case <-valueCh: 115 t.Fatal("shouldn't receive value") 116 case <-time.After(10 * time.Millisecond): 117 } 118 119 // Close 120 v.Close() 121 122 // Verify 123 val := <-valueCh 124 if val != ErrClosed { 125 t.Fatalf("bad: %#v", val) 126 } 127 } 128 129 func TestComparedValueClose_existing(t *testing.T) { 130 var v ComparedValue 131 132 // Set a value 133 v.SetValue("foo") 134 135 // Close 136 v.Close() 137 138 // Try again 139 val, ok := v.ValueOk("foo") 140 if !ok { 141 t.Fatal("should be ok") 142 } 143 144 // Verify 145 if val != "foo" { 146 t.Fatalf("bad: %#v", val) 147 } 148 } 149 150 func TestComparedValueClose_existingBlocked(t *testing.T) { 151 var v ComparedValue 152 153 // Start reading this should be blocking 154 valueCh := make(chan interface{}) 155 go func() { 156 valueCh <- v.Value("foo") 157 }() 158 159 // Wait 160 time.Sleep(10 * time.Millisecond) 161 162 // Set a value 163 v.SetValue("foo") 164 165 // Close 166 v.Close() 167 168 // Try again 169 val, ok := v.ValueOk("foo") 170 if !ok { 171 t.Fatal("should be ok") 172 } 173 174 // Verify 175 if val != "foo" { 176 t.Fatalf("bad: %#v", val) 177 } 178 }