diff --git a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt index 150eb14ff6..29795d2cfb 100644 --- a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt +++ b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt @@ -267,6 +267,7 @@ Microsoft.DotNet.Interactive public System.Threading.Tasks.Task Invoke(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context) public class KernelScheduler, IKernelScheduler, System.IDisposable .ctor() + public T CurrentValue { get;} public System.Void CancelCurrentOperation() public System.Void Dispose() protected System.Boolean IsChildOperation(T current, T incoming) diff --git a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs index 82e5d08418..92013bc961 100644 --- a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs +++ b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs @@ -4,10 +4,11 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using FluentAssertions; using System.Linq; using System.Threading; using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; using Microsoft.DotNet.Interactive.Tests.Utility; using Microsoft.DotNet.Interactive.Utility; using Pocket; @@ -90,7 +91,7 @@ public async Task top_level_scheduled_work_does_not_execute_in_parallel() maxObservedParallelism.Should().Be(1); } - + public class TestKernelScheduler : KernelScheduler { private readonly Func _isChildOperation; @@ -496,6 +497,108 @@ await scheduler.RunAsync("inner", async _ => "outer 2"); } + [Fact] + public async Task CurrentValue_reflects_the_work_that_is_in_progress() + { + using var scheduler = new KernelScheduler(); + + var work = 1000; + await scheduler.RunAsync(work, async _ => + { + scheduler.CurrentValue.Should().Be(work); + return 2000; + }); + } + + [Fact] + public async Task CurrentValue_reflects_the_child_operation_when_called_from_within_a_child_operation() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + scheduler.CurrentValue.Should().Be("inner"); + return default; + }); + + return default; + }); + } + + [Fact] + public async Task CurrentValue_reflects_the_parent_operation_when_called_from_within_a_parent_operation() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + return default; + }); + + scheduler.CurrentValue.Should().Be("outer"); + return default; + }); + } + + [Fact] + public async Task CurrentValue_is_reset_once_all_work_is_complete() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + return default; + }); + + return default; + }); + + scheduler.CurrentValue.Should().BeNull(); + } + + [Fact(Skip = "Disabled pending https://github.com/dotnet/interactive/issues/3236")] + public async Task CurrentValue_reflects_correct_value_within_parent_child_as_well_as_grand_child_operations() + { + using var scheduler = + new TestKernelScheduler( + (o, i) => o == "parent" && i == "child" || o == "child" && i == "grandchild" || o == "parent" && i == "grandchild"); + + using var _ = new AssertionScope(); + + scheduler.CurrentValue.Should().BeNull(); + + await scheduler.RunAsync("parent", async _ => + { + scheduler.CurrentValue.Should().Be("parent"); + + await scheduler.RunAsync("child", async _ => + { + scheduler.CurrentValue.Should().Be("child"); + + await scheduler.RunAsync("grandchild", async _ => + { + scheduler.CurrentValue.Should().Be("grandchild"); + return default; + }); + + // See https://github.com/dotnet/interactive/issues/3236 + scheduler.CurrentValue.Should().Be("child"); + return default; + }); + + scheduler.CurrentValue.Should().Be("parent"); + return default; + }); + + scheduler.CurrentValue.Should().BeNull(); + } + [Fact] public async Task concurrent_schedulers_do_not_interfere_with_one_another() { diff --git a/src/Microsoft.DotNet.Interactive/KernelScheduler.cs b/src/Microsoft.DotNet.Interactive/KernelScheduler.cs index 9f3378eae9..fa8dffd6e5 100644 --- a/src/Microsoft.DotNet.Interactive/KernelScheduler.cs +++ b/src/Microsoft.DotNet.Interactive/KernelScheduler.cs @@ -44,14 +44,15 @@ public void CancelCurrentOperation() { if (_currentlyRunningTopLevelOperation is { } operation) { - operation.TaskCompletionSource.TrySetCanceled(_schedulerDisposalSource.Token); _currentlyRunningTopLevelOperation = null; + operation.TaskCompletionSource.TrySetCanceled(_schedulerDisposalSource.Token); } } - internal T CurrentValue => (_currentlyRunningOperation ?? _currentlyRunningTopLevelOperation) is { } currentOperation - ? currentOperation.Value - : default; + public T CurrentValue => + (_currentlyRunningOperation ?? _currentlyRunningTopLevelOperation) is { } currentOperation + ? currentOperation.Value + : default; public Task RunAsync( T value, @@ -182,7 +183,7 @@ void CompleteWithException(Exception exception) void ResetCurrentlyRunning() { - if (ReferenceEquals(_currentlyRunningTopLevelOperation, _currentlyRunningOperation)) + if (ReferenceEquals(_currentlyRunningTopLevelOperation, operation)) { _currentlyRunningTopLevelOperation = null; }