Skip to content

Commit

Permalink
provide context to delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Nov 6, 2024
1 parent b82af41 commit 5e1cae5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions commonspace/deletionmanager/deleteloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ type deleteLoop struct {
deleteCtx context.Context
deleteCancel context.CancelFunc
deleteChan chan struct{}
deleteFunc func()
deleteFunc func(ctx context.Context)
loopDone chan struct{}
}

func newDeleteLoop(deleteFunc func()) *deleteLoop {
func newDeleteLoop(deleteFunc func(ctx context.Context)) *deleteLoop {
ctx, cancel := context.WithCancel(context.Background())
return &deleteLoop{
deleteCtx: ctx,
Expand All @@ -32,18 +32,18 @@ func (dl *deleteLoop) Run() {

func (dl *deleteLoop) loop() {
defer close(dl.loopDone)
dl.deleteFunc()
dl.deleteFunc(dl.deleteCtx)
ticker := time.NewTicker(deleteLoopInterval)
defer ticker.Stop()
for {
select {
case <-dl.deleteCtx.Done():
return
case <-dl.deleteChan:
dl.deleteFunc()
dl.deleteFunc(dl.deleteCtx)
ticker.Reset(deleteLoopInterval)
case <-ticker.C:
dl.deleteFunc()
dl.deleteFunc(dl.deleteCtx)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions commonspace/deletionmanager/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package deletionmanager

import (
"context"

"go.uber.org/zap"

"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonspace/deletionstate"
"github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
"github.com/anyproto/any-sync/commonspace/object/treemanager"
"github.com/anyproto/any-sync/commonspace/spacestorage"
"go.uber.org/zap"
)

type Deleter interface {
Delete()
Delete(ctx context.Context)
}

type deleter struct {
Expand All @@ -25,7 +27,7 @@ func newDeleter(st spacestorage.SpaceStorage, state deletionstate.ObjectDeletion
return &deleter{st, state, getter, log}
}

func (d *deleter) Delete() {
func (d *deleter) Delete(ctx context.Context) {
var (
allQueued = d.state.GetQueued()
spaceId = d.st.Id()
Expand All @@ -39,7 +41,7 @@ func (d *deleter) Delete() {
continue
}
} else {
err = d.getter.DeleteTree(context.Background(), spaceId, id)
err = d.getter.DeleteTree(ctx, spaceId, id)
if err != nil && err != spacestorage.ErrTreeStorageAlreadyDeleted {
log.Error("failed to delete object", zap.Error(err))
continue
Expand Down
17 changes: 10 additions & 7 deletions commonspace/deletionmanager/deleter_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package deletionmanager

import (
"context"
"fmt"
"testing"

"go.uber.org/mock/gomock"

"github.com/anyproto/any-sync/commonspace/deletionstate/mock_deletionstate"
"github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
"github.com/anyproto/any-sync/commonspace/object/treemanager/mock_treemanager"
"github.com/anyproto/any-sync/commonspace/spacestorage/mock_spacestorage"
"go.uber.org/mock/gomock"
"testing"
)

func TestDeleter_Delete(t *testing.T) {
Expand All @@ -27,7 +30,7 @@ func TestDeleter_Delete(t *testing.T) {
treeManager.EXPECT().MarkTreeDeleted(gomock.Any(), spaceId, id).Return(nil)
delState.EXPECT().Delete(id).Return(nil)

deleter.Delete()
deleter.Delete(context.TODO())
})

t.Run("deleter delete mark deleted other error", func(t *testing.T) {
Expand All @@ -37,7 +40,7 @@ func TestDeleter_Delete(t *testing.T) {
st.EXPECT().Id().Return(spaceId)
st.EXPECT().TreeStorage(id).Return(nil, fmt.Errorf("unknown error"))

deleter.Delete()
deleter.Delete(context.TODO())
})

t.Run("deleter delete mark deleted fail", func(t *testing.T) {
Expand All @@ -48,7 +51,7 @@ func TestDeleter_Delete(t *testing.T) {
st.EXPECT().TreeStorage(id).Return(nil, treestorage.ErrUnknownTreeId)
treeManager.EXPECT().MarkTreeDeleted(gomock.Any(), spaceId, id).Return(fmt.Errorf("mark error"))

deleter.Delete()
deleter.Delete(context.TODO())
})

t.Run("deleter delete success", func(t *testing.T) {
Expand All @@ -60,7 +63,7 @@ func TestDeleter_Delete(t *testing.T) {
treeManager.EXPECT().DeleteTree(gomock.Any(), spaceId, id).Return(nil)
delState.EXPECT().Delete(id).Return(nil)

deleter.Delete()
deleter.Delete(context.TODO())
})

t.Run("deleter delete error", func(t *testing.T) {
Expand All @@ -71,6 +74,6 @@ func TestDeleter_Delete(t *testing.T) {
st.EXPECT().TreeStorage(id).Return(nil, nil)
treeManager.EXPECT().DeleteTree(gomock.Any(), spaceId, id).Return(fmt.Errorf("some error"))

deleter.Delete()
deleter.Delete(context.TODO())
})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e1cae5

Please sign in to comment.