MCPcopy Index your code
hub / github.com/docker/docker-agent / TestMap_LoadOrStoreConcurrent

Function TestMap_LoadOrStoreConcurrent

pkg/concurrent/map_test.go:147–175  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

145}
146
147func 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
177func TestMap_Concurrent(t *testing.T) {
178 t.Parallel()

Callers

nothing calls this directly

Calls 3

NewMapFunction · 0.85
LoadOrStoreMethod · 0.80
WaitMethod · 0.65

Tested by

no test coverage detected