2023-10-31 02:30:16 +08:00
|
|
|
package syncmgr
|
|
|
|
|
|
|
|
import (
|
2024-06-25 14:22:04 +08:00
|
|
|
"context"
|
2023-10-31 02:30:16 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
)
|
|
|
|
|
2024-02-05 11:33:43 +08:00
|
|
|
/*
|
2023-10-31 02:30:16 +08:00
|
|
|
type mockTask struct {
|
2024-02-05 11:33:43 +08:00
|
|
|
targetID int64
|
|
|
|
ch chan struct{}
|
|
|
|
err error
|
2023-10-31 02:30:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *mockTask) done() {
|
|
|
|
close(t.ch)
|
|
|
|
}
|
|
|
|
|
2023-11-23 17:26:24 +08:00
|
|
|
func (t *mockTask) SegmentID() int64 { panic("no implementation") }
|
|
|
|
func (t *mockTask) Checkpoint() *msgpb.MsgPosition { panic("no implementation") }
|
|
|
|
func (t *mockTask) StartPosition() *msgpb.MsgPosition { panic("no implementation") }
|
|
|
|
func (t *mockTask) ChannelName() string { panic("no implementation") }
|
|
|
|
|
2023-10-31 02:30:16 +08:00
|
|
|
func (t *mockTask) Run() error {
|
|
|
|
<-t.ch
|
|
|
|
return t.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockTask(err error) *mockTask {
|
|
|
|
return &mockTask{
|
|
|
|
err: err,
|
|
|
|
ch: make(chan struct{}),
|
|
|
|
}
|
2024-02-05 11:33:43 +08:00
|
|
|
}*/
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
type KeyLockDispatcherSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *KeyLockDispatcherSuite) TestKeyLock() {
|
2024-06-25 14:22:04 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-10-31 02:30:16 +08:00
|
|
|
d := newKeyLockDispatcher[int64](2)
|
|
|
|
|
2024-02-05 11:33:43 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
t1 := NewMockTask(s.T())
|
2024-06-25 14:22:04 +08:00
|
|
|
t1.EXPECT().Run(ctx).Run(func(_ context.Context) {
|
2024-02-05 11:33:43 +08:00
|
|
|
<-done
|
|
|
|
}).Return(nil)
|
|
|
|
t2 := NewMockTask(s.T())
|
2024-06-25 14:22:04 +08:00
|
|
|
t2.EXPECT().Run(ctx).Return(nil)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
sig := atomic.NewBool(false)
|
|
|
|
|
2024-06-25 14:22:04 +08:00
|
|
|
d.Submit(ctx, 1, t1)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
go func() {
|
2024-06-25 14:22:04 +08:00
|
|
|
d.Submit(ctx, 1, t2)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
sig.Store(true)
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.False(sig.Load(), "task 2 will never be submit before task 1 done")
|
|
|
|
|
2024-02-05 11:33:43 +08:00
|
|
|
close(done)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
s.Eventually(sig.Load, time.Second, time.Millisecond*100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *KeyLockDispatcherSuite) TestCap() {
|
2024-06-25 14:22:04 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-10-31 02:30:16 +08:00
|
|
|
d := newKeyLockDispatcher[int64](1)
|
|
|
|
|
2024-02-05 11:33:43 +08:00
|
|
|
t1 := NewMockTask(s.T())
|
|
|
|
t2 := NewMockTask(s.T())
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
2024-06-25 14:22:04 +08:00
|
|
|
t1.EXPECT().Run(ctx).Run(func(_ context.Context) {
|
2024-02-05 11:33:43 +08:00
|
|
|
<-done
|
|
|
|
}).Return(nil)
|
2024-06-25 14:22:04 +08:00
|
|
|
t2.EXPECT().Run(ctx).Return(nil)
|
2023-10-31 02:30:16 +08:00
|
|
|
sig := atomic.NewBool(false)
|
|
|
|
|
2024-06-25 14:22:04 +08:00
|
|
|
d.Submit(ctx, 1, t1)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
go func() {
|
2024-02-05 11:33:43 +08:00
|
|
|
// defer t2.done()
|
2024-06-25 14:22:04 +08:00
|
|
|
d.Submit(ctx, 2, t2)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
sig.Store(true)
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.False(sig.Load(), "task 2 will never be submit before task 1 done")
|
|
|
|
|
2024-02-05 11:33:43 +08:00
|
|
|
close(done)
|
2023-10-31 02:30:16 +08:00
|
|
|
|
|
|
|
s.Eventually(sig.Load, time.Second, time.Millisecond*100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyLockDispatcher(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeyLockDispatcherSuite))
|
|
|
|
}
|