(t *testing.T)
| 145 | } |
| 146 | |
| 147 | func TestMap_LoadOrStoreConcurrent(t *testing.T) { |
| 148 | t.Parallel() |
| 149 | // Concurrent LoadOrStore calls for the same key must all return the |
| 150 | // same value, with exactly one of them reporting loaded == false. |
| 151 | m := NewMap[int, int]() |
| 152 | var wg sync.WaitGroup |
| 153 | const writers = 100 |
| 154 | |
| 155 | values := make([]int, writers) |
| 156 | loadedFlags := make([]bool, writers) |
| 157 | for i := range writers { |
| 158 | wg.Go(func() { |
| 159 | val, loaded := m.LoadOrStore(0, i) |
| 160 | values[i] = val |
| 161 | loadedFlags[i] = loaded |
| 162 | }) |
| 163 | } |
| 164 | wg.Wait() |
| 165 | |
| 166 | first := values[0] |
| 167 | newCount := 0 |
| 168 | for i := range writers { |
| 169 | require.Equal(t, first, values[i]) |
| 170 | if !loadedFlags[i] { |
| 171 | newCount++ |
| 172 | } |
| 173 | } |
| 174 | require.Equal(t, 1, newCount, "exactly one caller should report loaded == false") |
| 175 | } |
| 176 | |
| 177 | func TestMap_Concurrent(t *testing.T) { |
| 178 | t.Parallel() |
nothing calls this directly
no test coverage detected