-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix integration test
capture_suicide_while_balance_table
(#952)
ref #442
- Loading branch information
1 parent
f66770c
commit d9aed28
Showing
14 changed files
with
352 additions
and
94 deletions.
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
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
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,58 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
type numbers interface { | ||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | ||
} | ||
|
||
type genericAtomic[T numbers] interface { | ||
Load() T | ||
Store(T) | ||
CompareAndSwap(old, new T) bool | ||
} | ||
|
||
// CompareAndIncrease updates the target if the new value is larger than or equal to the old value. | ||
// It returns false if the new value is smaller than the old value. | ||
func CompareAndIncrease[T numbers](target genericAtomic[T], new T) bool { | ||
for { | ||
old := target.Load() | ||
if new < old { | ||
return false | ||
} | ||
if new == old || target.CompareAndSwap(old, new) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
// CompareAndMonotonicIncrease updates the target if the new value is larger than the old value. | ||
// It returns false if the new value is smaller than or equal to the old value. | ||
func CompareAndMonotonicIncrease[T numbers](target genericAtomic[T], new T) bool { | ||
for { | ||
old := target.Load() | ||
if new <= old { | ||
return false | ||
} | ||
if target.CompareAndSwap(old, new) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
// MustCompareAndMonotonicIncrease updates the target if the new value is larger than the old value. It do nothing | ||
// if the new value is smaller than or equal to the old value. | ||
func MustCompareAndMonotonicIncrease[T numbers](target genericAtomic[T], new T) { | ||
_ = CompareAndMonotonicIncrease(target, new) | ||
} |
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,106 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMustCompareAndIncrease(t *testing.T) { | ||
t.Parallel() | ||
|
||
var target atomic.Int64 | ||
target.Store(10) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
wg := sync.WaitGroup{} | ||
|
||
doIncrease := func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
delta := rand.Int63n(100) | ||
v := target.Load() + delta | ||
MustCompareAndMonotonicIncrease(&target, v) | ||
require.GreaterOrEqual(t, target.Load(), v) | ||
} | ||
} | ||
} | ||
|
||
// Test target increase. | ||
wg.Add(2) | ||
go func() { | ||
defer wg.Done() | ||
doIncrease() | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
doIncrease() | ||
}() | ||
|
||
wg.Add(1) | ||
// Test target never decrease. | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
v := target.Load() - 1 | ||
MustCompareAndMonotonicIncrease(&target, v) | ||
require.Greater(t, target.Load(), v) | ||
} | ||
} | ||
}() | ||
|
||
cancel() | ||
wg.Wait() | ||
} | ||
|
||
func TestCompareAndIncrease(t *testing.T) { | ||
t.Parallel() | ||
|
||
var target atomic.Int64 | ||
target.Store(10) | ||
require.True(t, CompareAndIncrease(&target, 10)) | ||
require.Equal(t, int64(10), target.Load()) | ||
|
||
require.True(t, CompareAndIncrease(&target, 20)) | ||
require.Equal(t, int64(20), target.Load()) | ||
require.False(t, CompareAndIncrease(&target, 19)) | ||
require.Equal(t, int64(20), target.Load()) | ||
} | ||
|
||
func TestCompareAndMonotonicIncrease(t *testing.T) { | ||
t.Parallel() | ||
|
||
var target atomic.Int64 | ||
target.Store(10) | ||
require.False(t, CompareAndMonotonicIncrease(&target, 10)) | ||
require.Equal(t, int64(10), target.Load()) | ||
|
||
require.True(t, CompareAndMonotonicIncrease(&target, 11)) | ||
require.Equal(t, int64(11), target.Load()) | ||
require.False(t, CompareAndMonotonicIncrease(&target, 10)) | ||
require.Equal(t, int64(11), target.Load()) | ||
} |
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
Oops, something went wrong.