Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test concurrent writes to reporter #187

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion scope_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Uber Technologies, Inc.
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -578,6 +578,60 @@ func TestHistogramSharedBucketMetrics(t *testing.T) {
require.Equal(t, 0, len(unseen), fmt.Sprintf("%v", unseen))
}

func TestConcurrentUpdates(t *testing.T) {
var (
r = newTestStatsReporter()
wg = &sync.WaitGroup{}
workerCount = 20
scopeCount = 4
countersPerScope = 4
counterIncrs = 5000
rs = newRootScope(
ScopeOptions{
Prefix: "",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: just omit empty values; more idiomatic that way.

Tags: nil,
CachedReporter: r,
}, 0,
)
scopes = []Scope{rs}
counters []Counter
)

// Instantiate Subscopes.
for i := 1; i < scopeCount; i++ {
scopes = append(scopes, rs.SubScope(fmt.Sprintf("subscope_%d", i)))
}

// Instantiate Counters.
for sNum, s := range scopes {
for cNum := 0; cNum < countersPerScope; cNum++ {
counters = append(counters, s.Counter(fmt.Sprintf("scope_%d_counter_%d", sNum, cNum)))
}
}

// Instantiate workers.
r.cg.Add(scopeCount * countersPerScope)
for worker := 0; worker < workerCount; worker++ {
wg.Add(1)
go func() {
defer wg.Done()
// Counter should have counterIncrs * workerCount.
for i := 0; i < counterIncrs*len(counters); i++ {
counters[i%len(counters)].Inc(1)
}
}()
}

wg.Wait()
rs.reportRegistry()
r.WaitAll()

wantVal := int64(workerCount * counterIncrs)
for _, gotCounter := range r.getCounters() {
assert.Equal(t, gotCounter.val, wantVal)
}
}

func TestCounterSanitized(t *testing.T) {
r := newTestStatsReporter()

Expand Down