Browse Source

Address review: use slices.Clone, simplify concurrent test

- Replace manual make+copy with slices.Clone in Snapshot()
- Remove redundant _ = len(data); Snapshot() call alone is
  sufficient to exercise the lock under -race
tags/v2.2.7^2^2
dolonet 1 month ago
parent
commit
eedee63143

+ 2
- 2
mtglib/internal/doppel/scout_conn_collected.go View File

@@ -1,6 +1,7 @@
1 1
 package doppel
2 2
 
3 3
 import (
4
+	"slices"
4 5
 	"sync"
5 6
 	"time"
6 7
 )
@@ -43,8 +44,7 @@ func (s *ScoutConnCollected) MarkWrite() {
43 44
 // Snapshot returns a copy of the collected data and the write index.
44 45
 func (s *ScoutConnCollected) Snapshot() ([]ScoutConnResult, int) {
45 46
 	s.mu.Lock()
46
-	snapshot := make([]ScoutConnResult, len(s.data))
47
-	copy(snapshot, s.data)
47
+	snapshot := slices.Clone(s.data)
48 48
 	writeIndex := s.writeIndex
49 49
 	s.mu.Unlock()
50 50
 

+ 2
- 2
mtglib/internal/doppel/scout_conn_collected_test.go View File

@@ -68,8 +68,8 @@ func (suite *ScoutConnCollectedTestSuite) TestConcurrentAddSnapshot() {
68 68
 		defer wg.Done()
69 69
 
70 70
 		for i := 0; i < 1000; i++ {
71
-			data, _ := collected.Snapshot()
72
-			_ = len(data)
71
+			// call Snapshot concurrently to exercise the lock under -race
72
+			collected.Snapshot() //nolint:errcheck
73 73
 		}
74 74
 	}()
75 75
 

Loading…
Cancel
Save