Skip to content

Commit

Permalink
feat: 更改版本号后缀为rc0
Browse files Browse the repository at this point in the history
fix: 启用虚拟终端的服务器在进程退出后未释放相关资源
  • Loading branch information
Zaitonn committed Dec 20, 2024
1 parent e3d7088 commit 9941e7d
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.0.0.0-beta</Version>
<Version>2.0.0.0-rc0</Version>
<Author>Zaitonn</Author>
<Copyright>Copyright (C) 2022 Zaitonn</Copyright>
<Product>Serein</Product>
Expand Down
14 changes: 6 additions & 8 deletions src/Serein.Cli/Services/Loggers/PluginLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ public sealed class PluginLogger(ILogger<PluginLogger> logger) : IPluginLogger

public void Log(LogLevel level, string name, string message)
{
switch (level)
if (level == LogLevel.Trace)
{
case LogLevel.Trace:
_logger.LogInformation(message);
break;

default:
CliConsole.WriteLine(level, $"[{name}] {message}");
break;
_logger.LogInformation(message);
}
else
{
CliConsole.WriteLine(level, $"[{name}] {message}");
}
}
}
8 changes: 7 additions & 1 deletion src/Serein.Core/Services/Commands/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HardwareInfoProvider hardwareInfoProvider
[GeneratedRegex(@"^\[(?<name>[a-zA-Z]+)(:(?<argument>[\w\-\s]+))?\](?<body>.+)$")]
private static partial Regex GetGeneralCommandRegex();

[GeneratedRegex(@"\{([a-zA-Z][a-zA-Z0-9\.]+?(@\w+)?)\}")]
[GeneratedRegex(@"\{([a-zA-Z][a-zA-Z0-9\.]*(@\w+)?)\}")]
private static partial Regex GetVariableRegex();

public static readonly Regex Variable = GetVariableRegex();
Expand Down Expand Up @@ -312,7 +312,13 @@ public string ApplyVariables(

private object? GetServerVariables(string input, string? id = null)
{
if (!input.StartsWith("server.", StringComparison.InvariantCultureIgnoreCase))
{
return null;
}

var i = input.IndexOf('@');

ServerBase? server;

if (i < 0)
Expand Down
8 changes: 4 additions & 4 deletions src/Serein.Core/Services/Commands/HardwareInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public sealed class HardwareInfoProvider
public HardwareInfo? Info { get; private set; }

private readonly object _lock;
private readonly Timer _timer;
private readonly ILogger _logger;
private bool _isLoading;

Expand All @@ -27,9 +26,10 @@ public HardwareInfoProvider(ILogger<HardwareInfoProvider> logger)
Task.Run(Update);
_lock = new();
_logger = logger;
_timer = new(5000);
_timer.Elapsed += (_, _) => Update();
_timer.Start();

var timer = new Timer(5000);
timer.Elapsed += (_, _) => Update();
timer.Start();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static void WriteFileSync(string path, byte[] data, JsValue? options = de

public static int WriteSync(int fd, byte[] buffer)
{
return WriteSync(fd, buffer, 0, buffer.Length, 0);
return WriteSync(fd, buffer, length: buffer.Length);
}

public static int WriteSync(
Expand Down
2 changes: 2 additions & 0 deletions src/Serein.Core/Services/Servers/ServerWithPty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected override void StartProcess()
{
if (_cancellationTokenSource.IsCancellationRequested)
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new();
}

Expand Down Expand Up @@ -101,6 +102,7 @@ protected override void StartProcess()
_process = Process.GetProcessById(_ptyConnection.Pid);
_ptyConnection.ProcessExited += (_, e) =>
{
_ptyConnection.Dispose();
_ptyConnection = null;
_process = null;
_streamReader?.Close();
Expand Down
2 changes: 1 addition & 1 deletion src/Serein.Lite/ResourcesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ResourcesManager(ILogger<ResourcesManager> logger)

private const string IndexHtml = "index.html";
private const string CustomCss = "custom.css";
public const string IndexPath = "Serein/console/index.html";
public static readonly string IndexPath = "Serein/console/index.html";

private readonly object _lock = new();

Expand Down
8 changes: 4 additions & 4 deletions src/Serein.Tests/Services/CommandParser/VariableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void ShouldApplyCustomVariables()
[InlineData("{_}", false)]
[InlineData("{\\}", false)]
[InlineData("{a}", true)]
[InlineData("{a.}", false)]
[InlineData("{a.}", true)]
[InlineData("{a.b}", true)]
[InlineData("{a.111}", false)]
[InlineData("{a.b.}", false)]
[InlineData("{a.b.c}", false)]
[InlineData("{a.111}", true)]
[InlineData("{a.b.}", true)]
[InlineData("{a.b.c}", true)]
[InlineData("{a.b@}", false)]
[InlineData("{a.b@c}", true)]
public static void ShouldAnalyzeWhetherToBeVariablePattern(string input, bool result)
Expand Down
8 changes: 3 additions & 5 deletions src/Serein.Tests/Services/CommandRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serein.Core.Models.Commands;
using Xunit;
using Parser = Serein.Core.Services.Commands.CommandParser;
Expand All @@ -12,15 +11,14 @@ namespace Serein.Tests.Services;
[Collection(nameof(Serein))]
public sealed class CommandTests
{
private readonly IHost _app;
private readonly Runner _commandRunner;

public CommandTests()
{
_app = HostFactory.BuildNew();
_app.StartAsync();
var app = HostFactory.BuildNew();
app.StartAsync();

_commandRunner = _app.Services.GetRequiredService<Runner>();
_commandRunner = app.Services.GetRequiredService<Runner>();
}

[Fact]
Expand Down
44 changes: 30 additions & 14 deletions src/Serein.Tests/Services/JsPlugin/RunTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Serein.Tests.Services.JsPlugin;

[Collection(nameof(Serein))]
public sealed class RunTimeTests : IDisposable
public sealed partial class RunTimeTests : IDisposable
{
private readonly IHost _host;
private readonly JsPluginLoader _jsPluginLoader;
Expand Down Expand Up @@ -90,19 +90,6 @@ public void ShouldAccessToBuiltInModuleProcess()
Assert.Equal(Environment.CurrentDirectory, kv.Value.Engine.Evaluate("process.cwd()"));
}

[Fact]
public void ShouldAccessToBuiltInModuleFs()
{
var kv = _jsPluginLoader.Plugins.First();

kv.Value.Engine.Evaluate("fs.writeFileSync('test.txt', 'test')");
Assert.True(File.Exists("test.txt"));
Assert.Equal(
File.ReadAllText("test.txt"),
kv.Value.Engine.Evaluate("fs.readFileSync('test.txt')")
);
}

[Fact]
public void ShouldBeAbleToOutput()
{
Expand Down Expand Up @@ -157,4 +144,33 @@ public async Task ShouldBeAbleToSetInterval()
await Task.Delay(1000);
Assert.True(kv.Value.Engine.Evaluate("count").AsNumber() > 5);
}

[Fact]
public void ShouldAccessToBuiltInModuleFs()
{
var kv = _jsPluginLoader.Plugins.First();

Assert.NotEmpty(kv.Value.Engine.Evaluate("fs.globSync('*.*')").AsArray());

kv.Value.Engine.Evaluate("fs.writeFileSync('test.txt', 'test')");
Assert.True(File.Exists("test.txt"));
Assert.Equal(
File.ReadAllText("test.txt"),
kv.Value.Engine.Evaluate("fs.readFileSync('test.txt')")
);
}

[Fact]
public void ShouldBeAbleToResolveFile()
{
var kv = _jsPluginLoader.Plugins.First();
Assert.Equal(
Path.GetFullPath(Path.Join(PathConstants.PluginsDirectory, "111.txt")),
kv.Value.Engine.Evaluate("serein.resolve('111.txt')")
);
Assert.Equal(
Path.GetFullPath(Path.Join(PathConstants.PluginsDirectory, "a", "b", "111.txt")),
kv.Value.Engine.Evaluate("serein.resolve('a', 'b', '111.txt')")
);
}
}
10 changes: 4 additions & 6 deletions src/Serein.Tests/Services/PermissionGroup/DefaultValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serein.Core.Services.Data;
using Serein.Core.Services.Permissions;
using Xunit;
Expand All @@ -9,17 +8,16 @@ namespace Serein.Tests.Services.PermissionGroup;
[Collection(nameof(Serein))]
public sealed class DefaultValueTests
{
private readonly IHost _app;
private readonly GroupManager _groupManager;
private readonly PermissionGroupProvider _permissionGroupProvider;

public DefaultValueTests()
{
_app = HostFactory.BuildNew();
_app.StartAsync();
var app = HostFactory.BuildNew();
app.StartAsync();

_groupManager = _app.Services.GetRequiredService<GroupManager>();
_permissionGroupProvider = _app.Services.GetRequiredService<PermissionGroupProvider>();
_groupManager = app.Services.GetRequiredService<GroupManager>();
_permissionGroupProvider = app.Services.GetRequiredService<PermissionGroupProvider>();
}

[Fact]
Expand Down
6 changes: 2 additions & 4 deletions src/Serein.Tests/Services/PermissionGroup/NodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serein.Core.Services.Permissions;
using Xunit;

Expand All @@ -10,13 +9,12 @@ namespace Serein.Tests.Services.PermissionGroup;
[Collection(nameof(Serein))]
public sealed class NodeTests
{
private readonly IHost _app;
private readonly PermissionManager _permissionManager;

public NodeTests()
{
_app = HostFactory.BuildNew();
_permissionManager = _app.Services.GetRequiredService<PermissionManager>();
var app = HostFactory.BuildNew();
_permissionManager = app.Services.GetRequiredService<PermissionManager>();
}

[Theory]
Expand Down
7 changes: 5 additions & 2 deletions src/Serein.Tests/Services/Server/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ public async Task ShouldOutputToFile()
[Fact]
public void ShouldCreateCommonServer()
{
var server = _serverManager.Add("test", new() { FileName = "cmd", UsePty = false });
var server = _serverManager.Add("test", new() { FileName = "cmd" });

Assert.Equal(typeof(Core.Services.Servers.Server), server.GetType());
}

[Fact]
public void ShouldCreateServerWithPty()
{
var server = _serverManager.Add("test", new() { FileName = "cmd", UsePty = true });
var server = _serverManager.Add(
"test",
new() { FileName = "cmd", Pty = { IsEnabled = true } }
);

Assert.Equal(typeof(ServerWithPty), server.GetType());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Serein.Tests/Services/WebApi/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public class ApiTests : IDisposable
private readonly HttpClient _client;
private readonly IHost _app;
private readonly HttpServer _httpServer;
private readonly SettingProvider _settingProvider;

public ApiTests()
{
_app = HostFactory.BuildNew();
_settingProvider = _app.Services.GetRequiredService<SettingProvider>();
_settingProvider.Value.WebApi.IsEnabled = true;

var settingProvider = _app.Services.GetRequiredService<SettingProvider>();
settingProvider.Value.WebApi.IsEnabled = true;
_httpServer = _app.Services.GetRequiredService<HttpServer>();
_client = new() { BaseAddress = new(_settingProvider.Value.WebApi.UrlPrefixes.First()) };
_client = new() { BaseAddress = new(settingProvider.Value.WebApi.UrlPrefixes.First()) };
}

public void Dispose()
Expand Down

0 comments on commit 9941e7d

Please sign in to comment.