github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/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  }