MCPcopy Index your code
hub / github.com/coder/coder / TestGraphThreadSafety

Function TestGraphThreadSafety

agent/unit/graph_test.go:231–410  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

229}
230
231func TestGraphThreadSafety(t *testing.T) {
232 t.Parallel()
233
234 t.Run("ConcurrentReadWrite", func(t *testing.T) {
235 t.Parallel()
236
237 graph := &testGraph{}
238 var wg sync.WaitGroup
239 const numWriters = 50
240 const numReaders = 100
241 const operationsPerWriter = 1000
242 const operationsPerReader = 2000
243
244 barrier := make(chan struct{})
245 // Launch writers
246 for i := 0; i < numWriters; i++ {
247 wg.Add(1)
248 go func(writerID int) {
249 defer wg.Done()
250 <-barrier
251 for j := 0; j < operationsPerWriter; j++ {
252 from := &testGraphVertex{Name: fmt.Sprintf("writer-%d-%d", writerID, j)}
253 to := &testGraphVertex{Name: fmt.Sprintf("writer-%d-%d", writerID, j+1)}
254 graph.AddEdge(from, to, testEdgeCompleted)
255 }
256 }(i)
257 }
258
259 // Launch readers
260 readerResults := make([]struct {
261 panicked bool
262 readCount int
263 }, numReaders)
264
265 for i := 0; i < numReaders; i++ {
266 wg.Add(1)
267 go func(readerID int) {
268 defer wg.Done()
269 <-barrier
270 defer func() {
271 if r := recover(); r != nil {
272 readerResults[readerID].panicked = true
273 }
274 }()
275
276 readCount := 0
277 for j := 0; j < operationsPerReader; j++ {
278 // Create a test vertex and read
279 testUnit := &testGraphVertex{Name: fmt.Sprintf("test-reader-%d-%d", readerID, j)}
280 forwardEdges := graph.GetForwardAdjacentVertices(testUnit)
281 reverseEdges := graph.GetReverseAdjacentVertices(testUnit)
282
283 // Just verify no panics (results may be nil for non-existent vertices)
284 _ = forwardEdges
285 _ = reverseEdges
286 readCount++
287 }
288 readerResults[readerID].readCount = readCount

Callers

nothing calls this directly

Calls 11

AddEdgeMethod · 0.80
ToDOTMethod · 0.80
NotEmptyMethod · 0.80
RunMethod · 0.65
AddMethod · 0.65
WaitMethod · 0.65
DoneMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected