mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 11:54:01 +03:00
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
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package doppel
|
package doppel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -43,8 +44,7 @@ func (s *ScoutConnCollected) MarkWrite() {
|
|||||||
// Snapshot returns a copy of the collected data and the write index.
|
// Snapshot returns a copy of the collected data and the write index.
|
||||||
func (s *ScoutConnCollected) Snapshot() ([]ScoutConnResult, int) {
|
func (s *ScoutConnCollected) Snapshot() ([]ScoutConnResult, int) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
snapshot := make([]ScoutConnResult, len(s.data))
|
snapshot := slices.Clone(s.data)
|
||||||
copy(snapshot, s.data)
|
|
||||||
writeIndex := s.writeIndex
|
writeIndex := s.writeIndex
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ func (suite *ScoutConnCollectedTestSuite) TestConcurrentAddSnapshot() {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
data, _ := collected.Snapshot()
|
// call Snapshot concurrently to exercise the lock under -race
|
||||||
_ = len(data)
|
collected.Snapshot() //nolint:errcheck
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user