Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ Microsoft.DotNet.Interactive
public System.Threading.Tasks.Task Invoke(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context)
public class KernelScheduler<T,TResult>, IKernelScheduler<T,TResult>, System.IDisposable
.ctor()
public T CurrentValue { get;}
public System.Void CancelCurrentOperation()
public System.Void Dispose()
protected System.Boolean IsChildOperation(T current, T incoming)
Expand Down
107 changes: 105 additions & 2 deletions src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +91,7 @@ public async Task top_level_scheduled_work_does_not_execute_in_parallel()

maxObservedParallelism.Should().Be(1);
}

public class TestKernelScheduler<T> : KernelScheduler<T, T>
{
private readonly Func<T, T, bool> _isChildOperation;
Expand Down Expand Up @@ -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<int, int>();

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<string>((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<string>((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<string>((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<string>(
(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()
{
Expand Down
11 changes: 6 additions & 5 deletions src/Microsoft.DotNet.Interactive/KernelScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResult> RunAsync(
T value,
Expand Down Expand Up @@ -182,7 +183,7 @@ void CompleteWithException(Exception exception)

void ResetCurrentlyRunning()
{
if (ReferenceEquals(_currentlyRunningTopLevelOperation, _currentlyRunningOperation))
if (ReferenceEquals(_currentlyRunningTopLevelOperation, operation))
{
_currentlyRunningTopLevelOperation = null;
}
Expand Down