Skip to content

Commit 3c65d8a

Browse files
committed
unstable test
Signed-off-by: bufferflies <1045931706@qq.com>
1 parent a262d63 commit 3c65d8a

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

pkg/tso/tso_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ import (
2727
"github.com/tikv/pd/pkg/election"
2828
)
2929

30-
type MockElection struct{}
30+
type mockElection struct{}
3131

32-
func (*MockElection) ID() uint64 { return 0 }
33-
func (*MockElection) Name() string { return "" }
34-
func (*MockElection) MemberValue() string { return "" }
35-
func (*MockElection) Client() *clientv3.Client { return nil }
36-
func (*MockElection) IsServing() bool { return true }
37-
func (*MockElection) PromoteSelf() {}
38-
func (*MockElection) Campaign(_ context.Context, _ int64) error {
32+
func (*mockElection) ID() uint64 { return 0 }
33+
func (*mockElection) Name() string { return "" }
34+
func (*mockElection) MemberValue() string { return "" }
35+
func (*mockElection) Client() *clientv3.Client { return nil }
36+
func (*mockElection) IsServing() bool { return true }
37+
func (*mockElection) PromoteSelf() {}
38+
func (*mockElection) Campaign(_ context.Context, _ int64) error {
3939
return nil
4040
}
41-
func (*MockElection) Resign() {}
42-
func (*MockElection) GetServingUrls() []string { return nil }
43-
func (*MockElection) GetElectionPath() string { return "" }
44-
func (*MockElection) GetLeadership() *election.Leadership { return nil }
41+
func (*mockElection) Resign() {}
42+
func (*mockElection) GetServingUrls() []string { return nil }
43+
func (*mockElection) GetElectionPath() string { return "" }
44+
func (*mockElection) GetLeadership() *election.Leadership { return nil }
4545

4646
func TestGenerateTSO(t *testing.T) {
4747
re := require.New(t)
@@ -57,7 +57,7 @@ func TestGenerateTSO(t *testing.T) {
5757
updatePhysicalInterval: 5 * time.Second,
5858
maxResetTSGap: func() time.Duration { return time.Hour },
5959
metrics: newTSOMetrics("test"),
60-
member: &MockElection{},
60+
member: &mockElection{},
6161
}
6262

6363
// update physical time interval failed due to reach the lastSavedTime, it needs to save storage first, but this behavior is not allowed.
@@ -86,20 +86,23 @@ func TestCurrentGetTSO(t *testing.T) {
8686
updatePhysicalInterval: 5 * time.Second,
8787
maxResetTSGap: func() time.Duration { return time.Hour },
8888
metrics: newTSOMetrics("test"),
89-
member: &MockElection{},
89+
member: &mockElection{},
9090
}
91+
runDuration := 10 * time.Second
92+
runCtx, runCancel := context.WithTimeout(ctx, runDuration-2*time.Second)
93+
defer runCancel()
9194

92-
runDuration := 5 * time.Second
9395
timestampOracle.lastSavedTime.Store(current.Add(runDuration))
94-
runCtx, runCancel := context.WithTimeout(ctx, runDuration-time.Second)
95-
defer runCancel()
96+
9697
wg := sync.WaitGroup{}
97-
wg.Add(10)
98+
concurrency := 20
99+
wg.Add(concurrency)
98100
changes := atomic.Int32{}
99101
totalTso := atomic.Int32{}
100-
for i := range 10 {
102+
for i := range concurrency {
101103
go func(i int) {
102104
pre, _ := timestampOracle.getTSO()
105+
103106
defer wg.Done()
104107
for {
105108
select {
@@ -122,5 +125,5 @@ func TestCurrentGetTSO(t *testing.T) {
122125
}
123126

124127
wg.Wait()
125-
re.Equal(totalTso.Load()/int32(maxLogical)+1, changes.Load())
128+
re.LessOrEqual(changes.Load(), totalTso.Load()/int32(maxLogical)+1)
126129
}

0 commit comments

Comments
 (0)