Skip to content

Commit

Permalink
Merge pull request #446 from JetBrains/im/keepalive-assert
Browse files Browse the repository at this point in the history
Lifetime.Eternal.KeepAlive should throw an assertion
  • Loading branch information
Iliya-usov authored Nov 6, 2023
2 parents d5dbf03 + 243ec2d commit 571a9c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 1 addition & 4 deletions rd-net/Lifetimes/Lifetimes/Lifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
using JetBrains.Diagnostics;
using JetBrains.Threading;

#if !NET35

#endif

namespace JetBrains.Lifetimes
{
/// <summary>
Expand Down Expand Up @@ -612,6 +608,7 @@ public async Task UsingNestedAsync([InstantHandle] Func<Lifetime, Task> action)
[PublicAPI]
public Lifetime KeepAlive(object @object)
{
LifetimeDefinition.Log.Assert(!IsEternal);
if (@object == null) throw new ArgumentNullException(nameof(@object));

return OnTermination(() => GC.KeepAlive(@object));
Expand Down
20 changes: 19 additions & 1 deletion rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Exception>(() =>
{
TestLogger.ExceptionLogger.ThrowLoggedExceptions();
});
Assert.True(ex.Message.Contains("!IsEternal"));
}
}
}

0 comments on commit 571a9c4

Please sign in to comment.