Просмотр исходного кода

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 месяц назад
Родитель
Сommit
eedee63143

+ 2
- 2
mtglib/internal/doppel/scout_conn_collected.go Просмотреть файл

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

+ 2
- 2
mtglib/internal/doppel/scout_conn_collected_test.go Просмотреть файл

68
 		defer wg.Done()
68
 		defer wg.Done()
69
 
69
 
70
 		for i := 0; i < 1000; i++ {
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
 

Загрузка…
Отмена
Сохранить