diff --git a/test/StreamJsonRpc.Tests/MarshalableProxyTests.cs b/test/StreamJsonRpc.Tests/MarshalableProxyTests.cs index bff6acf6..e0930e53 100644 --- a/test/StreamJsonRpc.Tests/MarshalableProxyTests.cs +++ b/test/StreamJsonRpc.Tests/MarshalableProxyTests.cs @@ -579,6 +579,24 @@ public async Task IMarshalable_MarshaledBackAndForth() Assert.True(IsExceptionOrInnerOfType(ex)); } + [Fact] + public async Task OneObjectMarshalledTwiceHasIndependentLifetimes() + { + // The method we call twice returns the same object each time, + // but JsonRpc doesn't recognize this and assigns a unique token and proxy each time. + IMarshalable proxy1 = (await this.client.GetMarshalableAsync().WithCancellation(this.TimeoutToken))!; + IMarshalable proxy2 = (await this.client.GetMarshalableAsync().WithCancellation(this.TimeoutToken))!; + + await proxy1.DoSomethingAsync(); + await proxy2.DoSomethingAsync(); + + Assert.NotSame(proxy1, proxy2); + proxy1.Dispose(); + + await proxy2.DoSomethingAsync(); + proxy2.Dispose(); + } + [Fact] public async Task DisposeOnDisconnect() { @@ -892,9 +910,10 @@ public class Server : IServer public Task GetMarshalableAsync(bool returnNull) { - var marshalable = returnNull ? null : new Data(() => this.ReturnedMarshalableDisposed.Set()); + // The OneObjectMarshalledTwiceHasIndependentLifetimes test depends on us returning the same instance each time. + var marshalable = returnNull ? null : (this.ReturnedMarshalable ?? new Data(() => this.ReturnedMarshalableDisposed.Set())); this.ReturnedMarshalable = marshalable; - return Task.FromResult(marshalable); + return Task.FromResult(marshalable); } public Task GetMarshalableWithOptionalInterfacesAsync()