Skip to content

Commit

Permalink
Invoke the Managed_Resource.with an error if GCed
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Dec 3, 2024
1 parent 7daf941 commit ca7592b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ type Managed_Resource
ADVANCED

Executes the provided action on the resource managed by the managed
resource object. The action is only invoked if the managed resource
has not yet been finalized. If the resource has already been finalized
then `Error` representing `Uninitialized_State` is returned.
resource object. The action is invoked with the managed resource only if
it has not yet been finalized. If the resource has already been finalized
then `Error` with `Uninitialized_State` payload is passed into the
action instead of the resource.

Arguments:
- action: The action that will be applied to the resource managed by
resource.
the `Managed_Resource` (or to `Uninitialized_State` error).
Returns:
Value returned from the `action` or `Uninitialized_State` `Error`
if the managed resource was already finalized
Value returned from the `action`

with : (Any -> Any) -> Any -> Any
with self ~action = @Tail_Call with_builtin self action

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ type Input_Stream
- f: Applies a function over the internal java stream.
with_java_stream : (Java_Input_Stream -> Any) -> Any
with_java_stream self f =
r = self.stream_resource . with java_like_stream->
self.stream_resource . with java_like_stream->
java_like_stream.catch Uninitialized_State _->
Panic.throw <| IOException.new "Stream closed"
java_stream = Stream_Utils.asInputStream java_like_stream
self.error_handler <| f java_stream
r.catch Uninitialized_State _->
Panic.throw <| IOException.new "Stream closed"

## PRIVATE
Runs an action with a `ReportingStreamDecoder` decoding data from the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.runtime;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -13,6 +14,7 @@
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -97,6 +99,13 @@ public void explicitlyReclaimableReference() throws Exception {
assertTrue("Value was GCed", none.isException());
assertEquals(
"It is an error", "Standard.Base.Error.Error", none.getMetaObject().getMetaQualifiedName());
assertThat(
"Contains Uninitialized_State as payload",
none.toString(),
Matchers.allOf(
Matchers.containsString("Uninitialized_State"),
Matchers.containsString("Error"),
Matchers.containsString("Managed_Resource")));
}

private static void assertGC(String msg, boolean expectGC, Reference<?> ref) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ Object execute(State state, VirtualFrame frame, ManagedResource mr, Object actio
resourceManager.unpark(mr);
}
} else {
var err = ctx.getBuiltins().error().makeUninitializedStateError(mr);
return DataflowError.withDefaultTrace(err, this);
var payload = ctx.getBuiltins().error().makeUninitializedStateError(mr);
var err = DataflowError.withDefaultTrace(payload, this);
return invokeCallableNode.execute(action, frame, state, new Object[] {err});
}
}
}
9 changes: 6 additions & 3 deletions test/Base_Tests/src/Runtime/Managed_Resource_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ add_specs suite_builder = suite_builder.group "Managed_Resource" group_builder->

# operation on finalized resource
none = mr.with v->
# should never be called
builder.append " empty :"+v.to_text
"Don't call me!"
builder.append " is_error:"+v.is_error.to_text
v.if_not_error <|
# should never be called
builder.append " empty :"+v.to_text
"Don't call me!"

builder.append none.to_text
none.is_error . should_be_true
Expand All @@ -114,6 +116,7 @@ add_specs suite_builder = suite_builder.group "Managed_Resource" group_builder->
With finished:6
finalizing:-1
Finalized:Managed_Resource -1
is_error:True
(Error: (Uninitialized_State.Error Managed_Resource -1))

msg_text = messages.join '\n'
Expand Down

0 comments on commit ca7592b

Please sign in to comment.