Skip to content

Commit 5151b13

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

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

pkg/tso/tso.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const (
4949
// and trigger unnecessary warnings about clock offset.
5050
// It's an empirical value.
5151
jetLagWarningThreshold = 150 * time.Millisecond
52-
waitUpdateTSOInterval = 50 * time.Millisecond
5352
)
5453

5554
// tsoObject is used to store the current TSO in memory with a RWMutex lock.
@@ -406,7 +405,7 @@ func (t *timestampOracle) getTS(ctx context.Context, count uint32) (pdpb.Timesta
406405
if err := t.updateTimestamp(physical, false); err != nil && !errs.ErrUpdateTimestamp.Equal(err) {
407406
log.Warn("update timestamp failed", logutil.CondUint32("keyspace-group-id", t.keyspaceGroupID, t.keyspaceGroupID > 0), zap.Error(err))
408407
}
409-
time.Sleep(waitUpdateTSOInterval)
408+
time.Sleep(t.updatePhysicalInterval)
410409
continue
411410
}
412411
// In case lease expired after the first check.

pkg/tso/tso_test.go

Lines changed: 32 additions & 27 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)
@@ -53,23 +53,25 @@ func TestGenerateTSO(t *testing.T) {
5353
physical: current,
5454
logical: maxLogical - 1,
5555
},
56-
saveInterval: 50 * time.Millisecond,
57-
updatePhysicalInterval: 5 * time.Second,
56+
saveInterval: 5 * time.Second,
57+
updatePhysicalInterval: 50 * time.Millisecond,
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.
6464
_, err := timestampOracle.getTS(ctx, 2)
6565
re.Error(err)
66-
re.Equal(current, timestampOracle.tsoMux.physical)
66+
physical, _ := timestampOracle.getTSO()
67+
re.Equal(current, physical)
6768

6869
// simulate the save to storage operation is done.
69-
timestampOracle.lastSavedTime.Store(current.Add(5 * time.Second))
70+
timestampOracle.lastSavedTime.Store(time.Now().Add(5 * time.Second))
7071
_, err = timestampOracle.getTS(ctx, 2)
7172
re.NoError(err)
72-
re.NotEqual(current, timestampOracle.tsoMux.physical)
73+
physical, _ = timestampOracle.getTSO()
74+
re.NotEqual(current, physical)
7375
}
7476

7577
func TestCurrentGetTSO(t *testing.T) {
@@ -82,24 +84,27 @@ func TestCurrentGetTSO(t *testing.T) {
8284
physical: current,
8385
logical: maxLogical - 1,
8486
},
85-
saveInterval: 50 * time.Millisecond,
86-
updatePhysicalInterval: 5 * time.Second,
87+
saveInterval: 5 * time.Second,
88+
updatePhysicalInterval: 50 * time.Millisecond,
8789
maxResetTSGap: func() time.Duration { return time.Hour },
8890
metrics: newTSOMetrics("test"),
89-
member: &MockElection{},
91+
member: &mockElection{},
9092
}
93+
runDuration := 10 * time.Second
94+
runCtx, runCancel := context.WithTimeout(ctx, runDuration-2*time.Second)
95+
defer runCancel()
9196

92-
runDuration := 5 * time.Second
9397
timestampOracle.lastSavedTime.Store(current.Add(runDuration))
94-
runCtx, runCancel := context.WithTimeout(ctx, runDuration-time.Second)
95-
defer runCancel()
98+
9699
wg := sync.WaitGroup{}
97-
wg.Add(10)
100+
concurrency := 20
101+
wg.Add(concurrency)
98102
changes := atomic.Int32{}
99103
totalTso := atomic.Int32{}
100-
for i := range 10 {
104+
for i := range concurrency {
101105
go func(i int) {
102106
pre, _ := timestampOracle.getTSO()
107+
103108
defer wg.Done()
104109
for {
105110
select {
@@ -122,5 +127,5 @@ func TestCurrentGetTSO(t *testing.T) {
122127
}
123128

124129
wg.Wait()
125-
re.Equal(totalTso.Load()/int32(maxLogical)+1, changes.Load())
130+
re.LessOrEqual(changes.Load(), totalTso.Load()/int32(maxLogical)+1)
126131
}

0 commit comments

Comments
 (0)