-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-20.0] VTTablet: smartconnpool: notify all expired waiters (#…
…16897) (#16902) Signed-off-by: Brendan Dougherty <[email protected]> Signed-off-by: Matt Lord <[email protected]> Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com> Co-authored-by: Matt Lord <[email protected]>
- Loading branch information
1 parent
f3313b1
commit 28cbdba
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package smartconnpool | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWaitlistExpireWithMultipleWaiters(t *testing.T) { | ||
wait := waitlist[*TestConn]{} | ||
wait.init() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
defer cancel() | ||
|
||
waiterCount := 2 | ||
expireCount := atomic.Int32{} | ||
|
||
for i := 0; i < waiterCount; i++ { | ||
go func() { | ||
_, err := wait.waitForConn(ctx, nil) | ||
if err != nil { | ||
expireCount.Add(1) | ||
} | ||
}() | ||
} | ||
|
||
// Wait for the context to expire | ||
<-ctx.Done() | ||
|
||
// Expire the waiters | ||
wait.expire(false) | ||
|
||
// Wait for the notified goroutines to finish | ||
timeout := time.After(1 * time.Second) | ||
ticker := time.NewTicker(10 * time.Millisecond) | ||
defer ticker.Stop() | ||
for expireCount.Load() != int32(waiterCount) { | ||
select { | ||
case <-timeout: | ||
require.Failf(t, "Timed out waiting for all waiters to expire", "Wanted %d, got %d", waiterCount, expireCount.Load()) | ||
case <-ticker.C: | ||
// try again | ||
} | ||
} | ||
|
||
assert.Equal(t, int32(waiterCount), expireCount.Load()) | ||
} |