From 827f62b00262ee9edabf3320f5ee80fc71602fbb Mon Sep 17 00:00:00 2001 From: Leonardo Chaia Date: Fri, 29 Nov 2024 19:49:32 -0300 Subject: [PATCH] refactor: Copy events to include sync and async variants. Signed-off-by: Leonardo Chaia --- src/OrasProject.Oras/CopyGraphOptions.cs | 31 +++++++++++++++++++----- tests/OrasProject.Oras.Tests/CopyTest.cs | 24 +++++++++++++----- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/OrasProject.Oras/CopyGraphOptions.cs b/src/OrasProject.Oras/CopyGraphOptions.cs index c368c6c..8aeac87 100644 --- a/src/OrasProject.Oras/CopyGraphOptions.cs +++ b/src/OrasProject.Oras/CopyGraphOptions.cs @@ -22,34 +22,53 @@ namespace OrasProject.Oras; /// public struct CopyGraphOptions { + /// + /// PreCopyAsync handles the current descriptor before it is copied. + /// + public event Func? PreCopyAsync; + /// /// PreCopy handles the current descriptor before it is copied. /// - public event Func? PreCopy; + public event Action? PreCopy; + + /// + /// PostCopyAsync handles the current descriptor after it is copied. + /// + public event Func? PostCopyAsync; /// /// PostCopy handles the current descriptor after it is copied. /// - public event Func? PostCopy; + public event Action? PostCopy; + + /// + /// CopySkippedAsync will be called when the sub-DAG rooted by the current node + /// is skipped. + /// + public event Func? CopySkippedAsync; /// /// CopySkipped will be called when the sub-DAG rooted by the current node /// is skipped. /// - public event Func? CopySkipped; + public event Action? CopySkipped; internal Task OnPreCopyAsync(Descriptor descriptor) { - return PreCopy?.InvokeAsync(descriptor) ?? Task.CompletedTask; + PreCopy?.Invoke(descriptor); + return PreCopyAsync?.InvokeAsync(descriptor) ?? Task.CompletedTask; } internal Task OnPostCopyAsync(Descriptor descriptor) { - return PostCopy?.InvokeAsync(descriptor) ?? Task.CompletedTask; + PostCopy?.Invoke(descriptor); + return PostCopyAsync?.InvokeAsync(descriptor) ?? Task.CompletedTask; } internal Task OnCopySkippedAsync(Descriptor descriptor) { - return CopySkipped?.Invoke(descriptor) ?? Task.CompletedTask; + CopySkipped?.Invoke(descriptor); + return CopySkippedAsync?.Invoke(descriptor) ?? Task.CompletedTask; } } diff --git a/tests/OrasProject.Oras.Tests/CopyTest.cs b/tests/OrasProject.Oras.Tests/CopyTest.cs index 5b2d9a2..c27f45b 100644 --- a/tests/OrasProject.Oras.Tests/CopyTest.cs +++ b/tests/OrasProject.Oras.Tests/CopyTest.cs @@ -198,15 +198,19 @@ void GenerateManifest(Descriptor config, params Descriptor[] layers) // Prepare copy options with OnCopySkippedAsync var skippedCount = 0; + var skippedAsyncCount = 0; var copyOptions = new CopyOptions { CopyGraphOptions = new CopyGraphOptions() { } }; - copyOptions.CopyGraphOptions.CopySkipped += d => + + copyOptions.CopyGraphOptions.CopySkipped += _ => skippedCount++; + + copyOptions.CopyGraphOptions.CopySkippedAsync += _ => { - skippedCount++; + skippedAsyncCount++; return Task.CompletedTask; }; @@ -235,6 +239,7 @@ void GenerateManifest(Descriptor config, params Descriptor[] layers) // Verify the OnCopySkippedAsync invocation count Assert.Equal(1, skippedCount); + Assert.Equal(1, skippedAsyncCount); } [Fact] @@ -416,6 +421,8 @@ void GenerateIndex(params Descriptor[] manifests) var dst = new MemoryStore(); var preCopyCount = 0; var postCopyCount = 0; + var preCopyAsyncCount = 0; + var postCopyAsyncCount = 0; var copyOptions = new CopyOptions { CopyGraphOptions = new CopyGraphOptions @@ -423,14 +430,17 @@ void GenerateIndex(params Descriptor[] manifests) } }; - copyOptions.CopyGraphOptions.PreCopy += d => + copyOptions.CopyGraphOptions.PreCopy += _ => preCopyCount++; + copyOptions.CopyGraphOptions.PreCopyAsync += d => { - preCopyCount++; + preCopyAsyncCount++; return Task.CompletedTask; }; - copyOptions.CopyGraphOptions.PostCopy += d => + + copyOptions.CopyGraphOptions.PostCopy += _ => postCopyCount++; + copyOptions.CopyGraphOptions.PostCopyAsync += d => { - postCopyCount++; + postCopyAsyncCount++; return Task.CompletedTask; }; @@ -447,7 +457,9 @@ void GenerateIndex(params Descriptor[] manifests) // Verify API counts Assert.Equal(7, preCopyCount); + Assert.Equal(7, preCopyAsyncCount); Assert.Equal(7, postCopyCount); + Assert.Equal(7, postCopyAsyncCount); } private class StorageTracker : ITarget