From 4a9469a3dc24ef88fd210248303152dbbf6f3662 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 19 Apr 2021 16:03:21 +0100 Subject: [PATCH] test changenotify: add command to help debugging changenotify --- cmd/all/all.go | 1 + cmd/test/changenotify/changenotify.go | 54 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 cmd/test/changenotify/changenotify.go diff --git a/cmd/all/all.go b/cmd/all/all.go index 3d3f09b3f..63442520f 100644 --- a/cmd/all/all.go +++ b/cmd/all/all.go @@ -54,6 +54,7 @@ import ( _ "github.com/rclone/rclone/cmd/size" _ "github.com/rclone/rclone/cmd/sync" _ "github.com/rclone/rclone/cmd/test" + _ "github.com/rclone/rclone/cmd/test/changenotify" _ "github.com/rclone/rclone/cmd/test/histogram" _ "github.com/rclone/rclone/cmd/test/info" _ "github.com/rclone/rclone/cmd/test/makefiles" diff --git a/cmd/test/changenotify/changenotify.go b/cmd/test/changenotify/changenotify.go new file mode 100644 index 000000000..7e79fa941 --- /dev/null +++ b/cmd/test/changenotify/changenotify.go @@ -0,0 +1,54 @@ +// Package changenotify tests rclone's changenotify support +package changenotify + +import ( + "context" + "errors" + "time" + + "github.com/rclone/rclone/cmd" + "github.com/rclone/rclone/cmd/test" + "github.com/rclone/rclone/fs" + "github.com/rclone/rclone/fs/config/flags" + "github.com/spf13/cobra" +) + +var ( + pollInterval = 10 * time.Second +) + +func init() { + test.Command.AddCommand(commandDefinition) + cmdFlags := commandDefinition.Flags() + flags.DurationVarP(cmdFlags, &pollInterval, "poll-interval", "", pollInterval, "Time to wait between polling for changes.") +} + +var commandDefinition = &cobra.Command{ + Use: "changenotify remote:", + Short: `Log any change notify requests for the remote passed in.`, + RunE: func(command *cobra.Command, args []string) error { + cmd.CheckArgs(1, 1, command, args) + f := cmd.NewFsSrc(args) + ctx := context.Background() + + // Start polling function + features := f.Features() + if do := features.ChangeNotify; do != nil { + pollChan := make(chan time.Duration) + do(ctx, changeNotify, pollChan) + pollChan <- pollInterval + fs.Logf(nil, "Waiting for changes, polling every %v", pollInterval) + } else { + return errors.New("poll-interval is not supported by this remote") + } + select {} + }, +} + +// changeNotify invalidates the directory cache for the relativePath +// passed in. +// +// if entryType is a directory it invalidates the parent of the directory too. +func changeNotify(relativePath string, entryType fs.EntryType) { + fs.Logf(nil, "%q: %v", relativePath, entryType) +}