Skip to content

Commit

Permalink
refactor: Copy events to include sync and async variants.
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Chaia <[email protected]>
  • Loading branch information
leonardochaia committed Nov 29, 2024
1 parent e35fc7e commit 827f62b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
31 changes: 25 additions & 6 deletions src/OrasProject.Oras/CopyGraphOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,53 @@ namespace OrasProject.Oras;
/// </summary>
public struct CopyGraphOptions
{
/// <summary>
/// PreCopyAsync handles the current descriptor before it is copied.
/// </summary>
public event Func<Descriptor, Task>? PreCopyAsync;

/// <summary>
/// PreCopy handles the current descriptor before it is copied.
/// </summary>
public event Func<Descriptor, Task>? PreCopy;
public event Action<Descriptor>? PreCopy;

/// <summary>
/// PostCopyAsync handles the current descriptor after it is copied.
/// </summary>
public event Func<Descriptor, Task>? PostCopyAsync;

/// <summary>
/// PostCopy handles the current descriptor after it is copied.
/// </summary>
public event Func<Descriptor, Task>? PostCopy;
public event Action<Descriptor>? PostCopy;

/// <summary>
/// CopySkippedAsync will be called when the sub-DAG rooted by the current node
/// is skipped.
/// </summary>
public event Func<Descriptor, Task>? CopySkippedAsync;

/// <summary>
/// CopySkipped will be called when the sub-DAG rooted by the current node
/// is skipped.
/// </summary>
public event Func<Descriptor, Task>? CopySkipped;
public event Action<Descriptor>? 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;
}
}
24 changes: 18 additions & 6 deletions tests/OrasProject.Oras.Tests/CopyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -416,21 +421,26 @@ 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
{
}
};

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

Expand All @@ -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
Expand Down

0 comments on commit 827f62b

Please sign in to comment.