github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/runtime/race/testdata/map_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package race_test 6 7 import ( 8 "testing" 9 ) 10 11 func TestRaceMapRW(t *testing.T) { 12 m := make(map[int]int) 13 ch := make(chan bool, 1) 14 go func() { 15 _ = m[1] 16 ch <- true 17 }() 18 m[1] = 1 19 <-ch 20 } 21 22 func TestRaceMapRW2(t *testing.T) { 23 m := make(map[int]int) 24 ch := make(chan bool, 1) 25 go func() { 26 _, _ = m[1] 27 ch <- true 28 }() 29 m[1] = 1 30 <-ch 31 } 32 33 func TestRaceMapRWArray(t *testing.T) { 34 // Check instrumentation of unaddressable arrays (issue 4578). 35 m := make(map[int][2]int) 36 ch := make(chan bool, 1) 37 go func() { 38 _ = m[1][1] 39 ch <- true 40 }() 41 m[2] = [2]int{1, 2} 42 <-ch 43 } 44 45 func TestNoRaceMapRR(t *testing.T) { 46 m := make(map[int]int) 47 ch := make(chan bool, 1) 48 go func() { 49 _, _ = m[1] 50 ch <- true 51 }() 52 _ = m[1] 53 <-ch 54 } 55 56 func TestRaceMapRange(t *testing.T) { 57 m := make(map[int]int) 58 ch := make(chan bool, 1) 59 go func() { 60 for range m { 61 } 62 ch <- true 63 }() 64 m[1] = 1 65 <-ch 66 } 67 68 func TestRaceMapRange2(t *testing.T) { 69 m := make(map[int]int) 70 ch := make(chan bool, 1) 71 go func() { 72 for range m { 73 } 74 ch <- true 75 }() 76 m[1] = 1 77 <-ch 78 } 79 80 func TestNoRaceMapRangeRange(t *testing.T) { 81 m := make(map[int]int) 82 // now the map is not empty and range triggers an event 83 // should work without this (as in other tests) 84 // so it is suspicious if this test passes and others don't 85 m[0] = 0 86 ch := make(chan bool, 1) 87 go func() { 88 for range m { 89 } 90 ch <- true 91 }() 92 for range m { 93 } 94 <-ch 95 } 96 97 func TestRaceMapLen(t *testing.T) { 98 m := make(map[string]bool) 99 ch := make(chan bool, 1) 100 go func() { 101 _ = len(m) 102 ch <- true 103 }() 104 m[""] = true 105 <-ch 106 } 107 108 func TestRaceMapDelete(t *testing.T) { 109 m := make(map[string]bool) 110 ch := make(chan bool, 1) 111 go func() { 112 delete(m, "") 113 ch <- true 114 }() 115 m[""] = true 116 <-ch 117 } 118 119 func TestRaceMapLenDelete(t *testing.T) { 120 m := make(map[string]bool) 121 ch := make(chan bool, 1) 122 go func() { 123 delete(m, "a") 124 ch <- true 125 }() 126 _ = len(m) 127 <-ch 128 } 129 130 func TestRaceMapVariable(t *testing.T) { 131 ch := make(chan bool, 1) 132 m := make(map[int]int) 133 go func() { 134 m = make(map[int]int) 135 ch <- true 136 }() 137 m = make(map[int]int) 138 <-ch 139 } 140 141 func TestRaceMapVariable2(t *testing.T) { 142 ch := make(chan bool, 1) 143 m := make(map[int]int) 144 go func() { 145 m[1] = 1 146 ch <- true 147 }() 148 m = make(map[int]int) 149 <-ch 150 } 151 152 func TestRaceMapVariable3(t *testing.T) { 153 ch := make(chan bool, 1) 154 m := make(map[int]int) 155 go func() { 156 _ = m[1] 157 ch <- true 158 }() 159 m = make(map[int]int) 160 <-ch 161 } 162 163 type Big struct { 164 x [17]int32 165 } 166 167 func TestRaceMapLookupPartKey(t *testing.T) { 168 k := &Big{} 169 m := make(map[Big]bool) 170 ch := make(chan bool, 1) 171 go func() { 172 k.x[8] = 1 173 ch <- true 174 }() 175 _ = m[*k] 176 <-ch 177 } 178 179 func TestRaceMapLookupPartKey2(t *testing.T) { 180 k := &Big{} 181 m := make(map[Big]bool) 182 ch := make(chan bool, 1) 183 go func() { 184 k.x[8] = 1 185 ch <- true 186 }() 187 _, _ = m[*k] 188 <-ch 189 } 190 func TestRaceMapDeletePartKey(t *testing.T) { 191 k := &Big{} 192 m := make(map[Big]bool) 193 ch := make(chan bool, 1) 194 go func() { 195 k.x[8] = 1 196 ch <- true 197 }() 198 delete(m, *k) 199 <-ch 200 } 201 202 func TestRaceMapInsertPartKey(t *testing.T) { 203 k := &Big{} 204 m := make(map[Big]bool) 205 ch := make(chan bool, 1) 206 go func() { 207 k.x[8] = 1 208 ch <- true 209 }() 210 m[*k] = true 211 <-ch 212 } 213 214 func TestRaceMapInsertPartVal(t *testing.T) { 215 v := &Big{} 216 m := make(map[int]Big) 217 ch := make(chan bool, 1) 218 go func() { 219 v.x[8] = 1 220 ch <- true 221 }() 222 m[1] = *v 223 <-ch 224 } 225 226 // Test for issue 7561. 227 func TestRaceMapAssignMultipleReturn(t *testing.T) { 228 connect := func() (int, error) { return 42, nil } 229 conns := make(map[int][]int) 230 conns[1] = []int{0} 231 ch := make(chan bool, 1) 232 var err error 233 go func() { 234 conns[1][0], err = connect() 235 ch <- true 236 }() 237 x := conns[1][0] 238 _ = x 239 <-ch 240 } 241 242 // BigKey and BigVal must be larger than 256 bytes, 243 // so that compiler sets KindGCProg for them. 244 type BigKey [1000]*int 245 246 type BigVal struct { 247 x int 248 y [1000]*int 249 } 250 251 func TestRaceMapBigKeyAccess1(t *testing.T) { 252 m := make(map[BigKey]int) 253 var k BigKey 254 ch := make(chan bool, 1) 255 go func() { 256 _ = m[k] 257 ch <- true 258 }() 259 k[30] = new(int) 260 <-ch 261 } 262 263 func TestRaceMapBigKeyAccess2(t *testing.T) { 264 m := make(map[BigKey]int) 265 var k BigKey 266 ch := make(chan bool, 1) 267 go func() { 268 _, _ = m[k] 269 ch <- true 270 }() 271 k[30] = new(int) 272 <-ch 273 } 274 275 func TestRaceMapBigKeyInsert(t *testing.T) { 276 m := make(map[BigKey]int) 277 var k BigKey 278 ch := make(chan bool, 1) 279 go func() { 280 m[k] = 1 281 ch <- true 282 }() 283 k[30] = new(int) 284 <-ch 285 } 286 287 func TestRaceMapBigKeyDelete(t *testing.T) { 288 m := make(map[BigKey]int) 289 var k BigKey 290 ch := make(chan bool, 1) 291 go func() { 292 delete(m, k) 293 ch <- true 294 }() 295 k[30] = new(int) 296 <-ch 297 } 298 299 func TestRaceMapBigValInsert(t *testing.T) { 300 m := make(map[int]BigVal) 301 var v BigVal 302 ch := make(chan bool, 1) 303 go func() { 304 m[1] = v 305 ch <- true 306 }() 307 v.y[30] = new(int) 308 <-ch 309 } 310 311 func TestRaceMapBigValAccess1(t *testing.T) { 312 m := make(map[int]BigVal) 313 var v BigVal 314 ch := make(chan bool, 1) 315 go func() { 316 v = m[1] 317 ch <- true 318 }() 319 v.y[30] = new(int) 320 <-ch 321 } 322 323 func TestRaceMapBigValAccess2(t *testing.T) { 324 m := make(map[int]BigVal) 325 var v BigVal 326 ch := make(chan bool, 1) 327 go func() { 328 v, _ = m[1] 329 ch <- true 330 }() 331 v.y[30] = new(int) 332 <-ch 333 }