Skip to content

Commit 7ae6665

Browse files
authored
Fixes and logging (#1771)
[fix] fixes and logging - fixed working directory of V# process - fixed #1611 - implemented #1475
1 parent bd31da7 commit 7ae6665

File tree

5 files changed

+75
-32
lines changed

5 files changed

+75
-32
lines changed

utbot-rd/src/main/rdgen/org/utbot/rd/models/CSharpModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ object VSharpModel: Ext(CSharpRoot) {
3030
init {
3131
call("generate", generateArguments, generateResults).async
3232
signal("ping", PredefinedType.string).async
33+
signal("log", PredefinedType.string).async
3334
}
3435
}

utbot-rider/src/dotnet/UtBot/UtBot.Rd/Generated/VSharpModel.Generated.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,47 @@ public class VSharpModel : RdExtBase
4444
//public fields
4545
[NotNull] public RdCall<GenerateArguments, GenerateResults> Generate => _Generate;
4646
[NotNull] public ISignal<string> Ping => _Ping;
47+
[NotNull] public ISignal<string> Log => _Log;
4748

4849
//private fields
4950
[NotNull] private readonly RdCall<GenerateArguments, GenerateResults> _Generate;
5051
[NotNull] private readonly RdSignal<string> _Ping;
52+
[NotNull] private readonly RdSignal<string> _Log;
5153

5254
//primary constructor
5355
private VSharpModel(
5456
[NotNull] RdCall<GenerateArguments, GenerateResults> generate,
55-
[NotNull] RdSignal<string> ping
57+
[NotNull] RdSignal<string> ping,
58+
[NotNull] RdSignal<string> log
5659
)
5760
{
5861
if (generate == null) throw new ArgumentNullException("generate");
5962
if (ping == null) throw new ArgumentNullException("ping");
63+
if (log == null) throw new ArgumentNullException("log");
6064

6165
_Generate = generate;
6266
_Ping = ping;
67+
_Log = log;
6368
_Generate.Async = true;
6469
_Ping.Async = true;
70+
_Log.Async = true;
6571
BindableChildren.Add(new KeyValuePair<string, object>("generate", _Generate));
6672
BindableChildren.Add(new KeyValuePair<string, object>("ping", _Ping));
73+
BindableChildren.Add(new KeyValuePair<string, object>("log", _Log));
6774
}
6875
//secondary constructor
6976
private VSharpModel (
7077
) : this (
7178
new RdCall<GenerateArguments, GenerateResults>(GenerateArguments.Read, GenerateArguments.Write, GenerateResults.Read, GenerateResults.Write),
79+
new RdSignal<string>(JetBrains.Rd.Impl.Serializers.ReadString, JetBrains.Rd.Impl.Serializers.WriteString),
7280
new RdSignal<string>(JetBrains.Rd.Impl.Serializers.ReadString, JetBrains.Rd.Impl.Serializers.WriteString)
7381
) {}
7482
//deconstruct trait
7583
//statics
7684

7785

7886

79-
protected override long SerializationHash => -2362293640438957269L;
87+
protected override long SerializationHash => -5982678710127900748L;
8088

8189
protected override Action<ISerializers> Register => RegisterDeclaredTypesSerializers;
8290
public static void RegisterDeclaredTypesSerializers(ISerializers serializers)
@@ -104,6 +112,7 @@ public override void Print(PrettyPrinter printer)
104112
using (printer.IndentCookie()) {
105113
printer.Print("generate = "); _Generate.PrintEx(printer); printer.Println();
106114
printer.Print("ping = "); _Ping.PrintEx(printer); printer.Println();
115+
printer.Print("log = "); _Log.PrintEx(printer); printer.Println();
107116
}
108117
printer.Print(")");
109118
}

utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.Runtime.Loader;
7+
using System.Text;
78
using JetBrains.Collections.Viewable;
89
using JetBrains.Lifetimes;
910
using JetBrains.Rd;
@@ -13,16 +14,35 @@
1314
using UtBot.Rd;
1415
using UtBot.Rd.Generated;
1516
using VSharp;
16-
using VSharp.System;
1717
using VSharp.TestRenderer;
18-
using Thread = System.Threading.Thread;
1918

2019
namespace UtBot.VSharp;
2120

2221
public static class VSharpMain
2322
{
2423
public static readonly string VSharpProcessName = "VSharp";
2524

25+
private class SignalWriter : TextWriter
26+
{
27+
private readonly ISignal<string> _signal;
28+
public SignalWriter(ISignal<string> signal)
29+
{
30+
_signal = signal;
31+
}
32+
33+
public override void Write(string value)
34+
{
35+
_signal.Fire(value);
36+
}
37+
38+
public override void WriteLine(string value)
39+
{
40+
Write(value);
41+
}
42+
43+
public override Encoding Encoding => Encoding.Default;
44+
}
45+
2646
private static GenerateResults GenerateImpl(GenerateArguments arguments)
2747
{
2848
var (assemblyPath, projectCsprojPath, solutionFilePath,
@@ -40,15 +60,14 @@ private static GenerateResults GenerateImpl(GenerateArguments arguments)
4060
if (methodInfo?.Name != descriptor.MethodName)
4161
throw new InvalidDataException(
4262
$"cannot find method - ${descriptor.MethodName} for type - ${descriptor.TypeName}");
43-
var stat = TestGenerator.Cover(methodInfo, generationTimeout);
63+
var stat = TestGenerator.Cover(methodInfo, generationTimeout, verbosity:Verbosity.Info);
4464
var targetProject = new FileInfo(projectCsprojPath);
4565
var solution = new FileInfo(solutionFilePath);
4666
var declaringType = methodInfo.DeclaringType;
4767
Debug.Assert(declaringType != null);
4868
var (generatedProject, renderedFiles) =
4969
Renderer.Render(stat.Results(), targetProject, declaringType, assemblyLoadContext, solution, targetFramework);
5070
return new GenerateResults(true, generatedProject.FullName, renderedFiles.ToArray(), null);
51-
;
5271
}
5372

5473
public static void Main(string[] args)
@@ -65,6 +84,8 @@ public static void Main(string[] args)
6584
scheduler.Queue(() =>
6685
{
6786
var vSharpModel = new VSharpModel(ldef.Lifetime, protocol);
87+
// Configuring V# logger: messages will be send via RD to UTBot plugin process
88+
Logger.ConfigureWriter(new SignalWriter(vSharpModel.Log));
6889
vSharpModel.Generate.Set((_, arguments) =>
6990
{
7091
try
@@ -94,4 +115,4 @@ public static void Main(string[] args)
94115
// Thread.Sleep(1000);
95116
ldef.Terminate();
96117
}
97-
}
118+
}

utbot-rider/src/dotnet/UtBot/UtBot/ProcessWithRdServer.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Net;
6+
using JetBrains.Annotations;
67
using JetBrains.Application.Threading;
78
using JetBrains.Collections.Viewable;
89
using JetBrains.Lifetimes;
@@ -11,6 +12,7 @@
1112
using JetBrains.Threading;
1213
using JetBrains.Util;
1314
using JetBrains.Util.Logging;
15+
using UtBot.Rd;
1416
using UtBot.Rd.Generated;
1517

1618
namespace UtBot;
@@ -20,14 +22,14 @@ public class ProcessWithRdServer
2022
{
2123
public Lifetime Lifetime => _ldef.Lifetime;
2224
public Protocol Protocol;
23-
public VSharpModel VSharpModel { get; private set; }
24-
25+
[CanBeNull] public VSharpModel VSharpModel { get; private set; }
26+
2527
private readonly LifetimeDefinition _ldef;
26-
private Process _process;
27-
private ILogger _logger = Logger.GetLogger<ProcessWithRdServer>();
28+
[CanBeNull] private Process _process;
2829

29-
public ProcessWithRdServer(string name, string workingDir, int port, string exePath, IShellLocks shellLocks, Lifetime? parent = null)
30+
public ProcessWithRdServer(string name, string workingDir, int port, string exePath, IShellLocks shellLocks, Lifetime? parent = null, [CanBeNull] ILogger logger = null)
3031
{
32+
logger ??= Logger.GetLogger<ProcessWithRdServer>();
3133
using var blockingCollection = new BlockingCollection<String>(2);
3234
shellLocks.AssertNonMainThread();
3335
_ldef = (parent ?? Lifetime.Eternal).CreateNested();
@@ -41,9 +43,11 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
4143
var wire = new SocketWire.Server(Lifetime, scheduler, socket);
4244
var serializers = new Serializers();
4345
var identities = new Identities(IdKind.Server);
44-
var startInfo = new ProcessStartInfo("dotnet", $"\"{exePath}\" {port}");
45-
46-
startInfo.WorkingDirectory = workingDir;
46+
var startInfo = new ProcessStartInfo("dotnet", $"\"{exePath}\" {port}")
47+
{
48+
WorkingDirectory = workingDir
49+
};
50+
4751
Protocol = new Protocol(name, serializers, identities, scheduler, wire, Lifetime);
4852
scheduler.Queue(() =>
4953
{
@@ -55,6 +59,7 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
5559
blockingCollection.TryAdd(s);
5660
}
5761
});
62+
VSharpModel.Log.Advise(Lifetime, s => logger.Info($"V#: {s}"));
5863
});
5964
_process = new Process();
6065
_process.StartInfo = startInfo;
@@ -64,20 +69,26 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
6469
else
6570
_ldef.Terminate();
6671
});
67-
if (_process?.HasExited == false)
72+
73+
if (_process?.HasExited == true) return;
74+
75+
SpinWaitEx.SpinUntil(pingLdef.Lifetime, () =>
6876
{
69-
SpinWaitEx.SpinUntil(pingLdef.Lifetime, () =>
77+
if (_process?.HasExited == true)
7078
{
71-
VSharpModel?.Ping.Fire("UtBot");
72-
return blockingCollection.TryTake(out _);
73-
});
74-
pingLdef.Terminate();
75-
}
79+
VSharpModel = null;
80+
_ldef.Terminate();
81+
}
82+
83+
VSharpModel?.Ping.Fire(RdUtil.MainProcessName);
84+
return blockingCollection.TryTake(out _);
85+
});
86+
pingLdef.Terminate();
7687
}
7788
catch (Exception)
7889
{
7990
_ldef.Terminate();
8091
throw;
8192
}
8293
}
83-
}
94+
}

utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p
118118

119119
_logger.Catch(() =>
120120
{
121+
var name = VSharpMain.VSharpProcessName;
122+
var workingDir = project.ProjectFileLocation.Directory.FullPath;
121123
var port = NetworkUtil.GetFreePort();
122-
var proc = new ProcessWithRdServer(VSharpMain.VSharpProcessName, project.ProjectFileLocation.FullPath, port, vsharpRunner.FullPath,
123-
project.Locks,
124-
_lifetime);
124+
var runnerPath = vsharpRunner.FullPath;
125+
var proc = new ProcessWithRdServer(name, workingDir, port, runnerPath, project.Locks, _lifetime, _logger);
125126
var projectCsprojPath = project.ProjectFileLocation.FullPath;
126-
var vsharpProjectTarget = calculateTestProjectTarget(tfm);
127+
var vSharpProjectTarget = calculateTestProjectTarget(tfm);
127128
var args = new GenerateArguments(assemblyPath.FullPath, projectCsprojPath, solutionFilePath, descriptor,
128-
GenerationTimeout, vsharpProjectTarget);
129-
var result = proc.VSharpModel.Generate.Sync(args, RpcTimeouts.Maximal);
129+
GenerationTimeout, vSharpProjectTarget);
130+
var result = proc.VSharpModel?.Generate.Sync(args, RpcTimeouts.Maximal);
130131
_logger.Info("Result acquired");
131-
if (result.IsGenerated)
132+
if (result is { IsGenerated: true })
132133
{
133134
_shellLocks.ExecuteOrQueue(_lifetime, "UnitTestBuilder::Generate", () =>
134135
{
@@ -142,8 +143,8 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p
142143
}
143144
else
144145
{
145-
_logger.Info(
146-
$"Could not generate tests for ${currentGeneratedItem}, exception - ${result.ExceptionMessage}");
146+
var ex = result == null ? "Could not start V#" : result.ExceptionMessage;
147+
_logger.Info($"Could not generate tests for ${currentGeneratedItem}, exception - {ex}");
147148
}
148149
});
149150

0 commit comments

Comments
 (0)