Skip to content

Commit

Permalink
LogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Dec 11, 2024
1 parent 62eb10e commit 7fd7e25
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 13 deletions.
40 changes: 35 additions & 5 deletions samples/Sample.PocketIC/PocketIc.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Net;
using EdjCase.ICP.Agent.Agents;
using EdjCase.ICP.Agent.Responses;
Expand All @@ -18,7 +19,7 @@ public class PocketIcServerFixture : IDisposable
public PocketIcServerFixture()
{
// Start the server for all tests
this.Server = PocketIcServer.Start(showRuntimeLogs: true, showErrorLogs: true).GetAwaiter().GetResult();
this.Server = PocketIcServer.Start(runtimeLogLevel: LogLevel.Trace, showErrorLogs: true).GetAwaiter().GetResult();
}

public void Dispose()
Expand Down Expand Up @@ -107,6 +108,7 @@ public async Task HttpGateway_CounterWasm__Basic__Valid()

await pocketIc.StartCanisterAsync(canisterId);

await pocketIc.SetTimeAsync(ICTimestamp.Now());
// Let time progress so that update calls get processed
await using (await pocketIc.AutoProgressTimeAsync())
{
Expand All @@ -119,17 +121,45 @@ public async Task HttpGateway_CounterWasm__Basic__Valid()
Assert.Equal((UnboundedUInt)0, getResponseValue);


// Add this at the "Here" comment
var processInfo = new ProcessStartInfo
{
FileName = "dfx",
Arguments = $"canister call {canisterId} inc () --network {httpGateway.Url} --identity anonymous --verbose --async",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = "/home/gekctek/git/ICP.NET",
};
Debug.WriteLine("dfx " + processInfo.Arguments);
RequestId requestId;
using (var process = Process.Start(processInfo))
{
string output = process!.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();

// Optionally log or handle the output/error
Debug.WriteLine($"Output: {output}");
Debug.WriteLine($"Error: {error}");
requestId = new RequestId(Convert.FromHexString(output.Trim().Substring(2)));
}
Debug.WriteLine("---Start-----\n\n\n\n\n\n\n\n\n\n\n");
CancellationTokenSource cts = new(TimeSpan.FromSeconds(5));
CandidArg incResponseArg = await agent.CallAsync(canisterId, "inc", CandidArg.Empty(), cancellationToken: cts.Token);
Assert.Equal(CandidArg.Empty(), incResponseArg);

// CandidArg incResponseArg = await agent.CallAsync(canisterId, "inc", CandidArg.Empty(), cancellationToken: cts.Token);
// Assert.Equal(CandidArg.Empty(), incResponseArg);

// This alternative also doesnt work
// RequestId requestId = await agent.CallAsynchronousAsync(canisterId, "inc", CandidArg.Empty());
// ICTimestamp currentTime = await pocketIc.GetTimeAsync();
// await pocketIc.SetTimeAsync(currentTime + TimeSpan.FromSeconds(5));
// await pocketIc.TickAsync(5);
// CandidArg incResponseArg = await agent.WaitForRequestAsync(canisterId, requestId);
// Assert.Equal(CandidArg.Empty(), incResponseArg);


CandidArg incResponseArg = await agent.WaitForRequestAsync(canisterId, requestId, cts.Token); // Waits indefinitely here
Assert.Equal(CandidArg.Empty(), incResponseArg);

getResponse = await agent.QueryAsync(canisterId, "get", CandidArg.Empty());
getResponseArg = getResponse.ThrowOrGetReply();
Expand Down
34 changes: 32 additions & 2 deletions src/PocketIC/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 40 additions & 5 deletions src/PocketIC/PocketIcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public async ValueTask DisposeAsync()
/// <summary>
/// Starts the pocket-ic server process
/// </summary>
/// <param name="showRuntimeLogs">Outputs the runtime logs using Debug.WriteLine(...)</param>
/// <param name="runtimeLogLevel">Outputs the runtime logs using Debug.WriteLine(...) with the specified log level. Null value disables the logging</param>
/// <param name="showErrorLogs">Outputs the error logs using Debug.WriteLine(...)</param>
/// <returns>The instance of the PocketIcServer with the running process</returns>
public static async Task<PocketIcServer> Start(
bool showRuntimeLogs = false,
LogLevel? runtimeLogLevel = null,
bool showErrorLogs = true
)
{
Expand All @@ -70,18 +70,22 @@ public static async Task<PocketIcServer> Start(
{
FileName = binPath,
Arguments = $"--port-file {portFilePath}",
RedirectStandardOutput = showRuntimeLogs,
RedirectStandardOutput = runtimeLogLevel != null,
RedirectStandardError = showErrorLogs,
UseShellExecute = false,
UseShellExecute = false
};
if (runtimeLogLevel != null)
{
startInfo.EnvironmentVariables["RUST_LOG"] = runtimeLogLevel.Value.ToString().ToLower();
}

Process? serverProcess = Process.Start(startInfo);

if (serverProcess == null)
{
throw new Exception("Failed to start PocketIC server process");
}
if (showRuntimeLogs)
if (runtimeLogLevel != null)
{
serverProcess.OutputDataReceived += (sender, e) =>
{
Expand Down Expand Up @@ -221,4 +225,35 @@ private static void EnsureExecutablePermission(string filePath)
}
}
}

/// <summary>
/// Specifies the level of logging.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Error level, used for logging error messages.
/// </summary>
Error,

/// <summary>
/// Warn level, used for logging warning messages.
/// </summary>
Warn,

/// <summary>
/// Info level, used for logging informational messages.
/// </summary>
Info,

/// <summary>
/// Debug level, used for logging debug messages.
/// </summary>
Debug,

/// <summary>
/// Trace level, used for logging trace messages.
/// </summary>
Trace,
}
}
2 changes: 1 addition & 1 deletion test/PocketIC.Tests/PocketIcServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PocketIcServerFixture : IDisposable
public PocketIcServerFixture()
{
// Start the server for all tests
this.Server = PocketIcServer.Start(showRuntimeLogs: true).GetAwaiter().GetResult();
this.Server = PocketIcServer.Start(runtimeLogLevel: LogLevel.Debug).GetAwaiter().GetResult();
}

public void Dispose()
Expand Down

0 comments on commit 7fd7e25

Please sign in to comment.