Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime.Eternal.KeepAlive should throw an assertion #446

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
}
Loading