Skip to content

Commit

Permalink
Improve api for awaiting kernel idle (dotnet#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur authored Sep 13, 2019
1 parent 3345e53 commit 1ca9851
Show file tree
Hide file tree
Showing 34 changed files with 420 additions and 362 deletions.
1 change: 0 additions & 1 deletion MLS.Agent.Tools/Dotnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down
42 changes: 28 additions & 14 deletions MLS.Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public static X509Certificate2 ParseKey(string base64EncodedKey)
typeof(Shell).Assembly
};

private static void StartLogging(CompositeDisposable disposables, StartupOptions options)
private static IDisposable StartAppInsightsLogging(StartupOptions options)
{

var disposables = new CompositeDisposable();

if (options.Production)
{
var applicationVersion = VersionSensor.Version().AssemblyInformationalVersion;
Expand All @@ -66,6 +69,24 @@ private static void StartLogging(CompositeDisposable disposables, StartupOptions
}));
}

if (options.ApplicationInsightsKey != null)
{
var telemetryClient = new TelemetryClient(new TelemetryConfiguration(options.ApplicationInsightsKey))
{
InstrumentationKey = options.ApplicationInsightsKey
};
disposables.Add(telemetryClient.SubscribeToPocketLogger(_assembliesEmittingPocketLoggerLogs));
}

Log.Event("AgentStarting");

return disposables;
}

internal static IDisposable StartToolLogging(StartupOptions options)
{
var disposables = new CompositeDisposable();

if (options.LogPath != null)
{
var log = new SerilogLoggerConfiguration()
Expand Down Expand Up @@ -94,22 +115,16 @@ private static void StartLogging(CompositeDisposable disposables, StartupOptions
args.SetObserved();
};

if (options.ApplicationInsightsKey != null)
{
var telemetryClient = new TelemetryClient(new TelemetryConfiguration(options.ApplicationInsightsKey))
{
InstrumentationKey = options.ApplicationInsightsKey
};
disposables.Add(telemetryClient.SubscribeToPocketLogger(_assembliesEmittingPocketLoggerLogs));
}

Log.Event("AgentStarting");
return disposables;
}

public static IWebHost ConstructWebHost(StartupOptions options)
{
var disposables = new CompositeDisposable();
StartLogging(disposables, options);
var disposables = new CompositeDisposable
{
StartAppInsightsLogging(options),
StartToolLogging(options)
};

if (options.Key is null)
{
Expand All @@ -120,7 +135,6 @@ public static IWebHost ConstructWebHost(StartupOptions options)
Log.Trace("Received Key: {key}", options.Key);
}


var webHost = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Path.GetDirectoryName(typeof(Program).Assembly.Location))
Expand Down
Empty file added MLS.Blazor/App.g.cs
Empty file.
Empty file added MLS.Blazor/Pages/Index.g.cs
Empty file.
Empty file added MLS.Blazor/Pages/_Imports.g.cs
Empty file.
Empty file added MLS.Blazor/_Imports.g.cs
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Threading.Tasks;
using Clockwise;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Recipes;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -21,9 +23,11 @@ public async Task send_completeReply_on_CompleteRequest()
{
var scheduler = CreateScheduler();
var request = Message.Create(new CompleteRequest("System.Console.", 15), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);

await KernelStatus.Idle();
await scheduler.Schedule(context);

await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// 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.Threading.Tasks;
using Clockwise;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Recipes;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -22,9 +23,10 @@ public async Task sends_ExecuteInput_when_ExecuteRequest_is_handled()
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("var a =12;"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should().Contain(message =>
Expand All @@ -40,9 +42,10 @@ public async Task sends_ExecuteReply_message_on_when_code_submission_is_handled(
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("var a =12;"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should().Contain(message =>
Expand All @@ -54,9 +57,10 @@ public async Task sends_ExecuteReply_with_error_message_on_when_code_submission_
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("asdes"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should()
Expand All @@ -74,9 +78,10 @@ public async Task sends_DisplayData_message_on_ValueProduced()
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("Console.WriteLine(2+2);"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(10.Seconds());

ServerRecordingSocket.DecodedMessages
.Should().Contain(message =>
Expand All @@ -92,9 +97,10 @@ public async Task sends_ExecuteReply_message_on_ReturnValueProduced()
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("2+2"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should().Contain(message =>
Expand All @@ -110,9 +116,10 @@ public async Task sends_ExecuteReply_message_when_submission_contains_only_a_dir
{
var scheduler = CreateScheduler();
var request = Message.Create(new ExecuteRequest("%%csharp"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);
await scheduler.Schedule(context);

await KernelStatus.Idle();
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.Should().Contain(message =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// 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 Clockwise;
using FluentAssertions;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions.Extensions;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Recipes;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -23,9 +24,11 @@ public async Task sends_InterruptReply()
{
var scheduler = CreateScheduler();
var request = Message.Create(new InterruptRequest(), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);

await KernelStatus.Idle();
await scheduler.Schedule(context);

await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages
.SingleOrDefault(message =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions.Extensions;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Pocket;
using Recipes;
Expand All @@ -24,9 +25,10 @@ public async Task sends_isCompleteReply_with_complete_if_the_code_is_a_complete_
{
var scheduler = CreateScheduler();
var request = Message.Create(new IsCompleteRequest("var a = 12;"), null);
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);

await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
await KernelStatus.Idle();
await scheduler.Schedule(context);
await context.Done().Timeout(5.Seconds());

Logger.Log.Info("DecodedMessages: {messages}", ServerRecordingSocket.DecodedMessages);

Expand All @@ -46,8 +48,11 @@ public async Task sends_isCompleteReply_with_incomplete_and_indent_if_the_code_i
{
var scheduler = CreateScheduler();
var request = Message.Create(new IsCompleteRequest("var a = 12"), null);
await scheduler.Schedule(new JupyterRequestContext(ServerChannel, IoPubChannel, request, KernelStatus));
await KernelStatus.Idle();
var context = new JupyterRequestContext(ServerChannel, IoPubChannel, request);

await scheduler.Schedule(context);
await context.Done().Timeout(5.Seconds());

ServerRecordingSocket.DecodedMessages.SingleOrDefault(message =>
message.Contains(JupyterMessageContentTypes.IsCompleteReply))
.Should()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,36 @@ namespace Microsoft.DotNet.Interactive.Jupyter.Tests
public abstract class JupyterRequestHandlerTestBase<T> : IDisposable
where T : JupyterMessageContent
{
private readonly CompositeDisposable _disposables =new CompositeDisposable();
protected MessageSender IoPubChannel { get; }
protected MessageSender ServerChannel { get; }
protected RecordingSocket ServerRecordingSocket { get; }
protected RecordingSocket IoRecordingSocket { get; }
protected KernelStatus KernelStatus { get; }
private readonly CompositeDisposable _disposables = new CompositeDisposable();
protected readonly MessageSender IoPubChannel;
protected readonly MessageSender ServerChannel;
protected readonly RecordingSocket ServerRecordingSocket;
protected readonly RecordingSocket IoRecordingSocket;
protected readonly IKernel Kernel;

protected JupyterRequestHandlerTestBase(ITestOutputHelper output)
{
_disposables.Add(output.SubscribeToPocketLogger());

Kernel = new CompositeKernel
{
new CSharpKernel()
}.UseDefaultMagicCommands();

_disposables.Add(Kernel.LogEventsToPocketLogger());

var signatureValidator = new SignatureValidator("key", "HMACSHA256");
ServerRecordingSocket = new RecordingSocket();
ServerChannel = new MessageSender(ServerRecordingSocket, signatureValidator);
IoRecordingSocket = new RecordingSocket();
IoPubChannel = new MessageSender(IoRecordingSocket, signatureValidator);
KernelStatus = new KernelStatus(
Header.Create<T>("test"),
ServerChannel);
}

public void Dispose() => _disposables.Dispose();

protected ICommandScheduler<JupyterRequestContext> CreateScheduler()
{
var handler = new JupyterRequestContextHandler(new CompositeKernel
{
new CSharpKernel()
}.UseDefaultMagicCommands());
var handler = new JupyterRequestContextHandler(Kernel);

return CommandScheduler.Create<JupyterRequestContext>(handler.Handle).Trace();
}
Expand Down
71 changes: 0 additions & 71 deletions Microsoft.DotNet.Interactive.Jupyter.Tests/KernelStatusTests.cs

This file was deleted.

Loading

0 comments on commit 1ca9851

Please sign in to comment.