Skip to content

Commit

Permalink
simulate semaphore contention, test
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Dec 16, 2024
1 parent 2f31a57 commit 176111c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions go/vt/topo/stats_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,33 @@ func (st *fakeConn) IsReadOnly() bool {
return st.readOnly
}

// createSemaphoreContention simulates semaphore contention on the test read semaphore.
func createSemaphoreContention(ctx context.Context, duration time.Duration) {
if err := testStatsConnReadSem.Acquire(ctx, 1); err != nil {
panic(err)
}
defer testStatsConnReadSem.Release(1)
time.Sleep(duration)
}

// TestStatsConnTopoListDir emits stats on ListDir
func TestStatsConnTopoListDir(t *testing.T) {
conn := &fakeConn{}
statsConn := NewStatsConn("global", conn, testStatsConnReadSem)
ctx := context.Background()

go createSemaphoreContention(ctx, 100*time.Millisecond)
statsConn.ListDir(ctx, "", true)
timingCounts := topoStatsConnTimings.Counts()["ListDir.global"]
if got, want := timingCounts, int64(1); got != want {
t.Errorf("stats were not properly recorded: got = %d, want = %d", got, want)
}

waitTimingsCounts := topoStatsConnReadWaitTimings.Counts()["ListDir.global"]
if got := waitTimingsCounts; got != 1 {
t.Errorf("stats were not properly recorded: got = %d, want = 1", got)
}

// error is zero before getting an error
errorCount := topoStatsConnErrors.Counts()["ListDir.global"]
if got, want := errorCount, int64(0); got != want {
Expand Down Expand Up @@ -271,12 +286,18 @@ func TestStatsConnTopoGet(t *testing.T) {
statsConn := NewStatsConn("global", conn, testStatsConnReadSem)
ctx := context.Background()

go createSemaphoreContention(ctx, time.Millisecond*100)
statsConn.Get(ctx, "")
timingCounts := topoStatsConnTimings.Counts()["Get.global"]
if got, want := timingCounts, int64(1); got != want {
t.Errorf("stats were not properly recorded: got = %d, want = %d", got, want)
}

waitTimingsCounts := topoStatsConnReadWaitTimings.Counts()["Get.global"]
if got := waitTimingsCounts; got != 1 {
t.Errorf("stats were not properly recorded: got = %d, want = 1", got)
}

// error is zero before getting an error
errorCount := topoStatsConnErrors.Counts()["Get.global"]
if got, want := errorCount, int64(0); got != want {
Expand Down

0 comments on commit 176111c

Please sign in to comment.