Skip to content

Commit eebe998

Browse files
committed
wip
1 parent 8ca2e23 commit eebe998

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
import Standard.Base.Data.Boolean.Boolean
1+
import Standard.Base.Error.Error
22
import Standard.Base.Nothing.Nothing
33
import Standard.Base.Runtime.Managed_Resource.Managed_Resource
44
import Standard.Base.Runtime.Ref.Ref
5+
from Standard.Base.Data.Boolean import Boolean, True, False
6+
7+
## PRIVATE
8+
type Reload_Detector_Fake
9+
Value
510

611
## PRIVATE
712
This is used by ReloadDetector.java to create a `Managed_Resource` that is
8-
finalized when the reload button is pressed.
13+
garbage collected when the reload button is pressed.
914

10-
The `on_finalize` function and the `clear` method both write `Nothing` to the
11-
ref. This is a signal that a reload has happenend. `on_finalize` is called by
12-
the engine when a reload happens. `clear` is only for testing, to simulate a
13-
reload.
15+
The managed resource contains a Ref containing a 0 (the value is
16+
unimportant). When the reload button is pressed, the ref is removed and
17+
attempting to access it using `with` throws an `Uninitialized_State`. When
18+
the `Uninitialized_State` is detected, it indicates that the reload has been
19+
initiated.
1420

15-
The `0` value stored in the ref is not used; it just has to be something
16-
other than Nothing.
21+
For standard library testing, a special fake value is stored in the ref to
22+
indicate a reload.
1723
type Reload_Detector
1824
private Value mr:Managed_Resource
1925

2026
new -> Reload_Detector =
21-
ref = Ref.new 0
22-
on_finalize ref = ref.put Nothing
23-
mr = Managed_Resource.register ref on_finalize Boolean.True
27+
mr = Managed_Resource.register (Ref.new 0) (x-> Nothing) True
2428
Reload_Detector.Value mr
2529

26-
get self = self.mr.with .get
30+
has_reload_occurred self =
31+
self.mr.has_been_collected || self.mr.with (ref-> ref.get.is_a Reload_Detector_Fake)
2732

28-
clear self =
29-
self.mr.with (ref-> ref.put Nothing)
30-
Nothing
33+
simulate_reload_test_only self =
34+
self.mr.with ref-> ref.put Reload_Detector_Fake.Value
3135

3236
create_reload_detector = Reload_Detector.new

distribution/lib/Standard/Base/0.0.0-dev/src/Runtime/Managed_Resource.enso

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
## An API for manual resource management.
22

33
import project.Any.Any
4+
import project.Errors.Common.Uninitialized_State
45
import project.Nothing.Nothing
5-
from project.Data.Boolean import Boolean, False
6+
from project.Data.Boolean import Boolean, True, False
67

78
## Resource provides an API for manual management of computation resources.
89

@@ -90,5 +91,15 @@ type Managed_Resource
9091
take : Any
9192
take self = @Builtin_Method "Managed_Resource.take"
9293

94+
## PRIVATE
95+
ADVANCED
96+
97+
Returns true iff the resource has been collected by the engine, false
98+
otherwise. If `with` throws any other error, it is propagated.
99+
has_been_collected : Boolean
100+
has_been_collected self -> Boolean = self.with x->
101+
if x.is_error.not then False else
102+
if x.catch.is_a Uninitialized_State then True else x
103+
93104
register_builtin r fn sys:Boolean = @Builtin_Method "Managed_Resource.register_builtin"
94105
with_builtin r fn = @Builtin_Method "Managed_Resource.with_builtin"

std-bits/base/src/main/java/org/enso/base/cache/ReloadDetector.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ReloadDetector() {
2020
}
2121

2222
public boolean hasReloadOccurred() {
23-
var reloadHasOccurred = trigger.invokeMember("get").isNull();
23+
var reloadHasOccurred = trigger.invokeMember("has_reload_occurred").asBoolean();
2424
if (reloadHasOccurred) {
2525
resetTrigger();
2626
}
@@ -34,6 +34,6 @@ private void resetTrigger() {
3434
}
3535

3636
void simulateReloadTestOnly() {
37-
trigger.invokeMember("clear");
37+
trigger.invokeMember("simulate_reload_test_only");
3838
}
3939
}

test/Table_Tests/src/IO/Fetch_Spec.enso

+7-7
Original file line numberDiff line numberDiff line change
@@ -521,24 +521,24 @@ add_specs suite_builder =
521521
LRUCache.new . getSettings . getTotalCacheLimit . should_equal (TotalCacheLimit.Percentage.new 0.2)
522522

523523
group_builder.specify "Cache should be cleared when a reload is detected" <|
524-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=10'
525-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=11'
526-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=12'
524+
HTTP.fetch base_url_with_slash+'test_download?length=10'
525+
HTTP.fetch base_url_with_slash+'test_download?length=11'
526+
HTTP.fetch base_url_with_slash+'test_download?length=12'
527527
get_num_response_cache_entries . should_equal 3
528528

529529
fake_reload
530530

531531
get_num_response_cache_entries . should_equal 3 # Cleaning is not triggered until the next request
532-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=13'
532+
HTTP.fetch base_url_with_slash+'test_download?length=13'
533533
get_num_response_cache_entries . should_equal 1
534-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=14'
535-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=15'
534+
HTTP.fetch base_url_with_slash+'test_download?length=14'
535+
HTTP.fetch base_url_with_slash+'test_download?length=15'
536536
get_num_response_cache_entries . should_equal 3
537537

538538
fake_reload
539539

540540
get_num_response_cache_entries . should_equal 3 # Cleaning is not triggered until the next request
541-
HTTP.fetch base_url_with_slash+'test_download?max-age=16&length=16'
541+
HTTP.fetch base_url_with_slash+'test_download?length=16'
542542
get_num_response_cache_entries . should_equal 1
543543

544544
group_builder.specify "Reissues the request if the cache file disappears" pending=pending_has_url <| Test.with_retries <|

0 commit comments

Comments
 (0)