1
0
Fork 0
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:
mcrakhman 2023-08-08 11:13:15 +02:00
parent 10687d2dc1
commit cdacae232e
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 20 additions and 8 deletions

View file

@ -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
}

View file

@ -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()
})
}