From 5a66ce21009fc36149412656684928730f15c6db Mon Sep 17 00:00:00 2001 From: Ivan Migalev Date: Fri, 3 Nov 2023 10:50:59 +0100 Subject: [PATCH] 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