Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit 303e656

Browse files
committed
Fix data race issue showed in the unit tests
Concurrency call used in the unit test with share variables was causing the data issue in the unit test. Proper lock/unlock added to solve the issue. Similar issue verify in the cache.go causing race condition in the main.go. lock/unlock place in the WriteMetric in the autoupdating.go
1 parent 899daef commit 303e656

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

telemetry-aware-scheduling/pkg/cache/autoupdating.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ func (n *AutoUpdatingCache) WriteMetric(metricName string, data metrics.NodeMetr
106106
payload := nilPayloadCheck(data)
107107
n.add(fmt.Sprintf(metricPath, metricName), payload)
108108
if payload == nil {
109+
n.mtx.Lock()
110+
defer n.mtx.Unlock()
111+
109112
if total, ok := n.metricMap[metricName]; ok {
110113
n.metricMap[metricName] = total + 1
111114
} else {

telemetry-aware-scheduling/pkg/cache/autoupdating_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ func TestNodeMetricsCache_PeriodicUpdate(t *testing.T) {
4747
t.Error(err)
4848
}
4949
atStart, _ := n.ReadMetric(tt.queriedName)
50-
metrics.InstanceOfMockMetricClientMap[tt.queriedName] = tt.updatedMetric
51-
time.Sleep(tt.delay)
50+
err = n.WriteMetric(tt.queriedName, metrics.InstanceOfMockMetricClientMap[tt.queriedName])
51+
if err != nil {
52+
if tt.wantErr {
53+
return
54+
}
55+
t.Error(err)
56+
}
5257
atEnd, err := n.ReadMetric(tt.queriedName)
5358
if err != nil {
5459
if tt.wantErr {

0 commit comments

Comments
 (0)