mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Change periodic sync init
This commit is contained in:
parent
10687d2dc1
commit
cdacae232e
3 changed files with 20 additions and 8 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
"github.com/anyproto/any-sync/app/logger"
|
||||
"go.uber.org/zap"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -39,9 +40,11 @@ type periodicCall struct {
|
|||
loopDone chan struct{}
|
||||
periodSeconds int
|
||||
timeout time.Duration
|
||||
isRunning atomic.Bool
|
||||
}
|
||||
|
||||
func (p *periodicCall) Run() {
|
||||
p.isRunning.Store(true)
|
||||
go p.loop(p.periodSeconds)
|
||||
}
|
||||
|
||||
|
@ -75,6 +78,9 @@ func (p *periodicCall) loop(periodSeconds int) {
|
|||
}
|
||||
|
||||
func (p *periodicCall) Close() {
|
||||
if !p.isRunning.Load() {
|
||||
return
|
||||
}
|
||||
p.loopCancel()
|
||||
<-p.loopDone
|
||||
}
|
||||
|
|
|
@ -45,4 +45,13 @@ func TestPeriodicSync_Run(t *testing.T) {
|
|||
pSync.Close()
|
||||
require.Equal(t, 2, times)
|
||||
})
|
||||
|
||||
t.Run("loop close not running", func(t *testing.T) {
|
||||
secs := 0
|
||||
diffSyncer := func(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
pSync := NewPeriodicSync(secs, 0, diffSyncer, l)
|
||||
pSync.Close()
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue