diff --git a/rd-net/Lifetimes/Lifetimes/Lifetime.cs b/rd-net/Lifetimes/Lifetimes/Lifetime.cs index 668b8022b..187c0c002 100644 --- a/rd-net/Lifetimes/Lifetimes/Lifetime.cs +++ b/rd-net/Lifetimes/Lifetimes/Lifetime.cs @@ -8,10 +8,6 @@ using JetBrains.Diagnostics; using JetBrains.Threading; -#if !NET35 - -#endif - namespace JetBrains.Lifetimes { /// @@ -612,6 +608,7 @@ public async Task UsingNestedAsync([InstantHandle] Func action) [PublicAPI] public Lifetime KeepAlive(object @object) { + 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 e2e2a755e..be4f3dc9c 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,20 @@ public void DefineLifetimeInheritTimeoutKindTest() Assert.AreEqual(LifetimeTerminationTimeoutKind.ExtraLong, OuterLifetime.Define(definition.Lifetime, "id", (ld, lf) => {}).TerminationTimeoutKind); } + + [Test] + public void EternalLifetimeKeepalive() + { + 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