Repository navigation
Log adapter stderr lines that have no log marker #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using SW.PrimitiveTypes; | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SW.Serverless.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// An adapter dotnet will not start - built for a runtime that is not installed, or packaged | ||
| /// without its files. dotnet says why on stderr and exits before the SDK runs, so the line | ||
| /// has no marker. The host used to drop it, which left the call failing with "Received null | ||
| /// data." and the reason nowhere at all. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class AdapterStderrTests | ||
| { | ||
| [TestMethod] | ||
| public async Task What_dotnet_prints_about_an_adapter_it_cannot_start_is_logged() | ||
| { | ||
| var directory = Directory.CreateTempSubdirectory(); | ||
| var adapterPath = Path.Combine(directory.FullName, "broken.adapter.dll"); | ||
| await File.WriteAllTextAsync(adapterPath, "not an assembly"); | ||
|
|
||
| var log = new WarningLog(); | ||
| using var loggerFactory = LoggerFactory.Create(b => b.AddProvider(log)); | ||
| var serverless = new ServerlessService(new ServerlessOptions(), new MemoryCache(new MemoryCacheOptions()), | ||
| loggerFactory, new ServiceCollection().BuildServiceProvider()); | ||
|
|
||
| try | ||
| { | ||
| await serverless.StartAsync("broken.adapter", Guid.NewGuid().ToString("N"), adapterPath); | ||
| await Assert.ThrowsExceptionAsync<Exception>(() => serverless.InvokeAsync("Handle", null)); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // Writing the command can also fail outright, when the process is already gone. | ||
| } | ||
| finally | ||
| { | ||
| ((IDisposable)serverless).Dispose(); | ||
| } | ||
|
|
||
| // stderr is read on its own thread, so the line can land after the call has failed. | ||
| for (var i = 0; i < 50 && log.Lines.IsEmpty; i++) await Task.Delay(100); | ||
|
|
||
| Assert.IsTrue(log.Lines.Any(l => l.Category == "serverless.adapters.broken.adapter"), | ||
| "dotnet's reason for not starting the adapter was not logged"); | ||
| directory.Delete(true); | ||
| } | ||
|
|
||
| sealed class WarningLog : ILoggerProvider | ||
| { | ||
| public ConcurrentQueue<(string Category, string Message)> Lines { get; } = new(); | ||
|
|
||
| public ILogger CreateLogger(string categoryName) => new Logger(this, categoryName); | ||
| public void Dispose() { } | ||
|
|
||
| sealed class Logger(WarningLog log, string category) : ILogger | ||
| { | ||
| public IDisposable BeginScope<TState>(TState state) where TState : notnull => null; | ||
| public bool IsEnabled(LogLevel logLevel) => true; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, | ||
| Func<TState, Exception, string> formatter) | ||
| { | ||
| if (logLevel == LogLevel.Warning) log.Lines.Enqueue((category, formatter(state, exception))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Assert that the warning contains the startup diagnostic.
The test checks only the category. A warning with no diagnostic message would still pass, although preserving dotnet's reason is the purpose of this change. Assert a stable part of the emitted message as well.
🤖 Prompt for AI Agents