From 5a66ce21009fc36149412656684928730f15c6db Mon Sep 17 00:00:00 2001 From: Ivan Migalev Date: Fri, 3 Nov 2023 10:50:59 +0100 Subject: [PATCH 1/3] Lifetime.Eternal.KeepAlive should throw an assertion --- rd-net/Lifetimes/Lifetimes/Lifetime.cs | 2 +- rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/rd-net/Lifetimes/Lifetimes/Lifetime.cs b/rd-net/Lifetimes/Lifetimes/Lifetime.cs index 668b8022b..7ae868e64 100644 --- a/rd-net/Lifetimes/Lifetimes/Lifetime.cs +++ b/rd-net/Lifetimes/Lifetimes/Lifetime.cs @@ -7,7 +7,6 @@ using JetBrains.Core; using JetBrains.Diagnostics; using JetBrains.Threading; - #if !NET35 #endif @@ -612,6 +611,7 @@ public async Task UsingNestedAsync([InstantHandle] Func action) [PublicAPI] public Lifetime KeepAlive(object @object) { + Assertion.Assert(!IsEternal); if (@object == null) throw new ArgumentNullException(nameof(@object)); return OnTermination(() => GC.KeepAlive(@object)); diff --git a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs index e2e2a755e..bb3137ee6 100644 --- a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs +++ b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs @@ -7,8 +7,11 @@ using JetBrains.Diagnostics; using JetBrains.Diagnostics.Internal; using JetBrains.Lifetimes; -using JetBrains.Threading; using NUnit.Framework; +#if !NET35 +using JetBrains.Threading; +#endif + // ReSharper disable MethodSupportsCancellation namespace Test.Lifetimes.Lifetimes @@ -1414,5 +1417,15 @@ public void DefineLifetimeInheritTimeoutKindTest() Assert.AreEqual(LifetimeTerminationTimeoutKind.ExtraLong, OuterLifetime.Define(definition.Lifetime, "id", (ld, lf) => {}).TerminationTimeoutKind); } + + [Test] + public void EternalLifetimeKeepalive() + { + Assert.Throws(() => + { + var o = new object(); + Lifetime.Eternal.KeepAlive(o); + }); + } } } \ No newline at end of file From 7e001517e4479cacc6f0c34743964df104b5a3eb Mon Sep 17 00:00:00 2001 From: Ivan Migalev Date: Fri, 3 Nov 2023 11:18:30 +0100 Subject: [PATCH 2/3] Lifetime: clear an empty #if section --- rd-net/Lifetimes/Lifetimes/Lifetime.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/rd-net/Lifetimes/Lifetimes/Lifetime.cs b/rd-net/Lifetimes/Lifetimes/Lifetime.cs index 7ae868e64..b0421e374 100644 --- a/rd-net/Lifetimes/Lifetimes/Lifetime.cs +++ b/rd-net/Lifetimes/Lifetimes/Lifetime.cs @@ -7,9 +7,6 @@ using JetBrains.Core; using JetBrains.Diagnostics; using JetBrains.Threading; -#if !NET35 - -#endif namespace JetBrains.Lifetimes { From 243ec2d68dfbb7149a9c3b8f60c8af335962e02d Mon Sep 17 00:00:00 2001 From: Ivan Migalev Date: Fri, 3 Nov 2023 11:32:55 +0100 Subject: [PATCH 3/3] Lifetime.KeepAlive: log an assertion instead of throwing --- rd-net/Lifetimes/Lifetimes/Lifetime.cs | 2 +- rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/rd-net/Lifetimes/Lifetimes/Lifetime.cs b/rd-net/Lifetimes/Lifetimes/Lifetime.cs index b0421e374..187c0c002 100644 --- a/rd-net/Lifetimes/Lifetimes/Lifetime.cs +++ b/rd-net/Lifetimes/Lifetimes/Lifetime.cs @@ -608,7 +608,7 @@ public async Task UsingNestedAsync([InstantHandle] Func action) [PublicAPI] public Lifetime KeepAlive(object @object) { - Assertion.Assert(!IsEternal); + LifetimeDefinition.Log.Assert(!IsEternal); if (@object == null) throw new ArgumentNullException(nameof(@object)); return OnTermination(() => GC.KeepAlive(@object)); diff --git a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs index bb3137ee6..be4f3dc9c 100644 --- a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs +++ b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs @@ -1421,11 +1421,16 @@ public void DefineLifetimeInheritTimeoutKindTest() [Test] public void EternalLifetimeKeepalive() { - Assert.Throws(() => + Assert.DoesNotThrow(() => { var o = new object(); Lifetime.Eternal.KeepAlive(o); }); + var ex = Assert.Throws(() => + { + TestLogger.ExceptionLogger.ThrowLoggedExceptions(); + }); + Assert.True(ex.Message.Contains("!IsEternal")); } } } \ No newline at end of file