Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions logs/log1755877694.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log

- **src/Proto.TestKit/ITestProbe.cs**: Removed public `Context` property and added `Watch`/`Unwatch` methods to limit external access to actor context.
- **src/Proto.TestKit/TestProbe.cs**: Made `Context` property private and added `Watch`/`Unwatch` wrappers so probes no longer expose internal context.
- **src/Proto.TestKit/PropsTestProbeExtensions.cs**: Updated middleware to use new `Send` API instead of accessing probe context directly.
- **src/Proto.TestKit/ProbeMailboxStatistics.cs**: Forward mailbox messages through probe `Send` method, avoiding direct context exposure.
- **tests/Proto.Remote.Tests/RemoteTests.cs**: Replaced `probe.Context.Watch/Unwatch` with new probe methods.
- **tests/Proto.TestKit.Tests/TestProbeAsyncTests.cs**: Removed direct context check, relying on probe methods for mailbox validation.
- **tests/Proto.Actor.Tests/Router/BroadcastPoolRouterTests.cs**: Used implicit PID conversion instead of context access when matching routees.
17 changes: 12 additions & 5 deletions src/Proto.TestKit/ITestProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public interface ITestProbe
/// </summary>
PID? Sender { get; }

/// <summary>
/// the context of the test probe
/// </summary>
IContext? Context { get; }

/// <summary>
/// asynchronously checks that no message arrives within the time allowed
/// </summary>
Expand Down Expand Up @@ -183,6 +178,18 @@ Task<T> FishForMessageAsync<T>(Func<T, bool> when, TimeSpan? timeAllowed = null,
/// <param name="message"></param>
void Respond(object message);

/// <summary>
/// starts watching the target actor
/// </summary>
/// <param name="pid"></param>
void Watch(PID pid);

/// <summary>
/// stops watching the target actor
/// </summary>
/// <param name="pid"></param>
void Unwatch(PID pid);

/// <summary>
/// sends a request message from the test probe to the target
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Proto.TestKit/ProbeMailboxStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void MessagePosted(object message)
}

public void MessageReceived(object message) =>
_probe.Context.Send(_probe.Context.Self, message);
_probe.Send(_probe, message);

public void MailboxEmpty()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Proto.TestKit/PropsTestProbeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Props WithReceiveProbe(this Props props, TestProbe probe) =>
props.WithReceiverMiddleware(next => async (ctx, env) =>
{
await next(ctx, env);
probe.Context.Send(probe.Context.Self, new MessageEnvelope(env.Message, env.Sender));
probe.Send(probe, new MessageEnvelope(env.Message, env.Sender));
});

/// <summary>
Expand All @@ -24,7 +24,7 @@ public static Props WithReceiveProbe(this Props props, TestProbe probe) =>
public static Props WithSendProbe(this Props props, TestProbe probe) =>
props.WithSenderMiddleware(next => async (ctx, target, env) =>
{
probe.Context.Send(probe.Context.Self, new MessageEnvelope(env.Message, target));
probe.Send(probe, new MessageEnvelope(env.Message, target));
await next(ctx, target, env);
});

Expand Down
9 changes: 6 additions & 3 deletions src/Proto.TestKit/TestProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public Task ReceiveAsync(IContext context)
/// <inheritdoc />
public PID? Sender { get; private set; }

/// <inheritdoc />
public IContext Context
private IContext Context
{
get
{
Expand All @@ -59,7 +58,7 @@ public IContext Context

return _context!;
}
private set => _context = value;
set => _context = value;
}

/// <inheritdoc />
Expand Down Expand Up @@ -183,6 +182,10 @@ public Task<T> RequestAsync<T>(PID target, object message, CancellationToken can
public Task<T> RequestAsync<T>(PID target, object message, TimeSpan timeAllowed) =>
Context.RequestAsync<T>(target, message, timeAllowed);

public void Watch(PID pid) => Context.Watch(pid);

public void Unwatch(PID pid) => Context.Unwatch(pid);

public static implicit operator PID?(TestProbe tp) => tp.Context.Self;

private async Task<MessageAndSender> ReceiveNextAsync(TimeSpan? timeAllowed,
Expand Down
6 changes: 3 additions & 3 deletions tests/Proto.Actor.Tests/Router/BroadcastPoolRouterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public async Task BroadcastPoolRouter_RemovedRouteesDoNotReceiveMessages()
var routee2 = routees.Pids[1];
var routee3 = routees.Pids[2];

var probe1 = probes.Find(p => p.Context.Self == routee1)!;
var probe2 = probes.Find(p => p.Context.Self == routee2)!;
var probe3 = probes.Find(p => p.Context.Self == routee3)!;
var probe1 = probes.Find(p => (PID)p == routee1)!;
var probe2 = probes.Find(p => (PID)p == routee2)!;
var probe3 = probes.Find(p => (PID)p == routee3)!;

system.Root.Send(router, "first");
await probe1.ExpectNextUserMessageAsync<string>(x => x == "first");
Expand Down
18 changes: 9 additions & 9 deletions tests/Proto.Remote.Tests/RemoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public async Task CanWatchRemoteActor()
var remoteActor = await SpawnRemoteActor(_fixture.RemoteAddress);

var (probe, _) = System.CreateTestProbe();
probe.Context.Watch(remoteActor);
probe.Watch(remoteActor);

await System.Root.PoisonAsync(remoteActor);

Expand All @@ -180,8 +180,8 @@ public async Task CanWatchMultipleRemoteActors()
var remoteActor2 = await SpawnRemoteActor(_fixture.RemoteAddress);

var (probe, _) = System.CreateTestProbe();
probe.Context.Watch(remoteActor1);
probe.Context.Watch(remoteActor2);
probe.Watch(remoteActor1);
probe.Watch(remoteActor2);

await System.Root.PoisonAsync(remoteActor1);
await System.Root.PoisonAsync(remoteActor2);
Expand All @@ -201,8 +201,8 @@ public async Task MultipleLocalActorsCanWatchRemoteActor()

var (probe1, _) = System.CreateTestProbe();
var (probe2, _) = System.CreateTestProbe();
probe1.Context.Watch(remoteActor);
probe2.Context.Watch(remoteActor);
probe1.Watch(remoteActor);
probe2.Watch(remoteActor);

await System.Root.PoisonAsync(remoteActor);

Expand All @@ -218,10 +218,10 @@ public async Task CanUnwatchRemoteActor()

var (probe1, _) = System.CreateTestProbe();
var (probe2, _) = System.CreateTestProbe();
probe1.Context.Watch(remoteActor);
probe2.Context.Watch(remoteActor);
probe1.Watch(remoteActor);
probe2.Watch(remoteActor);

probe2.Context.Unwatch(remoteActor);
probe2.Unwatch(remoteActor);
await Task.Delay(TimeSpan.FromSeconds(3));

await System.Root.PoisonAsync(remoteActor);
Expand All @@ -238,7 +238,7 @@ public async Task WhenRemoteTerminated_LocalWatcherReceivesNotification()
var remoteActor = await SpawnRemoteActor(_fixture.RemoteAddress);

var (probe, _) = System.CreateTestProbe();
probe.Context.Watch(remoteActor);
probe.Watch(remoteActor);

System.Root.Send(remoteActor, new Die());

Expand Down
11 changes: 0 additions & 11 deletions tests/Proto.TestKit.Tests/TestProbeAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ public async Task ExpectEmptyMailboxAsync_throws_when_message_present()
var (probe, pid) = system.CreateTestProbe();

system.Root.Send(pid, "hello");
SpinWait.SpinUntil(() =>
{
try
{
return probe.Context != null;
}
catch
{
return false;
}
}, TimeSpan.FromSeconds(1));
await Assert.ThrowsAsync<TestKitException>(() => probe.ExpectEmptyMailboxAsync());
}
}
Expand Down
Loading