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

Commit

Permalink
Fix data race issue showed in the unit tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
togashidm committed Jul 27, 2022
1 parent 899daef commit 303e656
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions telemetry-aware-scheduling/pkg/cache/autoupdating.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (n *AutoUpdatingCache) WriteMetric(metricName string, data metrics.NodeMetr
payload := nilPayloadCheck(data)
n.add(fmt.Sprintf(metricPath, metricName), payload)
if payload == nil {
n.mtx.Lock()
defer n.mtx.Unlock()

if total, ok := n.metricMap[metricName]; ok {
n.metricMap[metricName] = total + 1
} else {
Expand Down
9 changes: 7 additions & 2 deletions telemetry-aware-scheduling/pkg/cache/autoupdating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ func TestNodeMetricsCache_PeriodicUpdate(t *testing.T) {
t.Error(err)
}
atStart, _ := n.ReadMetric(tt.queriedName)
metrics.InstanceOfMockMetricClientMap[tt.queriedName] = tt.updatedMetric
time.Sleep(tt.delay)
err = n.WriteMetric(tt.queriedName, metrics.InstanceOfMockMetricClientMap[tt.queriedName])
if err != nil {
if tt.wantErr {
return
}
t.Error(err)
}
atEnd, err := n.ReadMetric(tt.queriedName)
if err != nil {
if tt.wantErr {
Expand Down

0 comments on commit 303e656

Please sign in to comment.