Skip to content

Commit

Permalink
testing scheduler behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
colombod committed Feb 17, 2021
1 parent 1d1be09 commit 00eaa50
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Xunit;
using Xunit.Abstractions;


namespace Microsoft.DotNet.Interactive.Tests
{
public class CancelCommandTests : LanguageKernelTestBase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Pocket;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.DotNet.Interactive.Tests
{
public class KernelCommandSchedulerTests
{
private readonly CompositeDisposable _disposables = new();

public KernelCommandSchedulerTests(ITestOutputHelper output)
{
DisposeAfterTest(output.SubscribeToPocketLogger());
}

public void Dispose()
{
try
{
_disposables?.Dispose();
}
catch (Exception ex)
{
Logger<KernelCommandSchedulerTests>.Log.Error(exception: ex);
}
}

private void DisposeAfterTest(IDisposable disposable)
{
_disposables.Add(disposable);
}

private void DisposeAfterTest(Action action)
{
_disposables.Add(action);
}

[Fact]
public async Task command_execute_on_kernel_specified_at_scheduling_time()
{
var commandsHandledOnKernel1 = new List<KernelCommand>();
var commandsHandledOnKernel2 = new List<KernelCommand>();

var scheduler = new KernelCommandScheduler();

var kernel1 = new FakeKernel("kernel1")
{
Handle = (command, context) =>
{
commandsHandledOnKernel1.Add(command);
return Task.CompletedTask;
}
};
var kernel2 = new FakeKernel("kernel2")
{
Handle = (command, context) =>
{
commandsHandledOnKernel2.Add(command);
return Task.CompletedTask;
}
};

var command1 = new SubmitCode("for kernel 1");
var command2 = new SubmitCode("for kernel 2");

await scheduler.Schedule(command1, kernel1);
await scheduler.Schedule(command2, kernel2);

commandsHandledOnKernel1.Should().ContainSingle().Which.Should().Be(command1);
commandsHandledOnKernel2.Should().ContainSingle().Which.Should().Be(command2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Microsoft.DotNet.Interactive.Tests
[LogTestNamesToPocketLogger]
public abstract class LanguageKernelTestBase : IDisposable
{
private readonly CompositeDisposable _disposables = new CompositeDisposable();
private readonly CompositeDisposable _disposables = new();

protected LanguageKernelTestBase(ITestOutputHelper output)
{
Expand All @@ -42,7 +42,7 @@ public void Dispose()
}
catch (Exception ex)
{
Log.Error(exception: ex);
Log.Error(ex);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/Microsoft.DotNet.Interactive/KernelCommandScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Utility;

Expand All @@ -18,7 +19,14 @@ public class KernelCommandScheduler
private readonly ConcurrentQueue<(KernelCommand command, Kernel kernel)> _deferredCommands = new();

private readonly ConcurrentQueue<KernelOperation> _commandQueue = new();
public Task<KernelCommandResult> Schedule(KernelCommand command, Kernel kernel, CancellationToken cancellationToken, Action onDone)

public Task<KernelCommandResult> Schedule(KernelCommand command, Kernel kernel)
{
return Schedule(command, kernel, CancellationToken.None, () => { });
}


public Task<KernelCommandResult> Schedule(KernelCommand command, Kernel kernel, CancellationToken cancellationToken, Action onDone)
{


Expand All @@ -34,9 +42,9 @@ public Task<KernelCommandResult> Schedule(KernelCommand command, Kernel kernel,

var kernelCommandResultSource = new TaskCompletionSource<KernelCommandResult>();

var operation = new KernelOperation(command, kernelCommandResultSource, kernel,false);
var operation = new KernelOperation(command, kernelCommandResultSource, kernel, false);
_commandQueue.Enqueue(operation);

ProcessCommandQueue(_commandQueue, cancellationToken, onDone);

return kernelCommandResultSource.Task;
Expand Down Expand Up @@ -127,7 +135,7 @@ public KernelOperation(

public void DeferCommand(KernelCommand command, Kernel kernel)
{
_deferredCommands.Enqueue((command,kernel));
_deferredCommands.Enqueue((command, kernel));
}

internal Task RunDeferredCommandsAsync()
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.DotNet.Interactive.App
{
public static class KernelExtensions
{
public static T UseAbout<T>(this T kernel)
public static T UseAboutMagicCommand<T>(this T kernel)
where T : Kernel
{
var about = new Command("#!about", "Show version and build information")
Expand Down

0 comments on commit 00eaa50

Please sign in to comment.