ivoleitao / stash

Key-value store abstraction with plain and cache driven semantics and a pluggable backend architecture.
MIT License
85 stars 16 forks source link

ExpiryPolicy.eternal duration causes overflow when compiled to Javascript #7

Closed ajrcarey closed 3 years ago

ajrcarey commented 3 years ago

Describe the bug

ExpiryPolicy.eternal sets a duration that overflows Javascript's number type. This is specific to compiling to web.

To Reproduce Steps to reproduce the behavior:

Nothing special required. Create a cache with Lfu eviction policy, and add any item to it. It'll work fine when running on the Dart VM, but throw an error when running in a web browser.

Expected behavior Consistent behaviour on every platform.

Version Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "linux_x64"

Additional context The problem can be fixed by reducing the value of the eternal constant in api/expiry/expiry_policy.dart:

abstract class ExpiryPolicy { /// The value used to represent a eternal duration static const Duration eternal = Duration(days: 999999);

...

It's presumably due to the difference in numeric precision between the Dart VM and Javascript, although that's just a guess; I didn't spend much time tracking it down other than the bare minimum necessary to fix my problem.

ajrcarey commented 3 years ago

Sample of error thrown:

Error: Invalid argument(s): DateTime is outside valid range: 8.640000000161878e+22 at Object.throw_ [as throw] (http://localhost:36401/dart_sdk.js:5334:11) at new core.DateTime._withValue (http://localhost:36401/dart_sdk.js:123247:17) at core.DateTime.now.add (http://localhost:36401/dart_sdk.js:123139:14) at default_cache.DefaultCache.new.[_putEntry] (http://localhost:36401/packages/stash/src/memory/memory_store.dart.lib.js:450:28) at http://localhost:36401/packages/stash/src/memory/memory_store.dart.lib.js:511:33 at _RootZone.runUnary (http://localhost:36401/dart_sdk.js:38889:58) at _FutureListener.then.handleValue (http://localhost:36401/dart_sdk.js:33875:29) at handleValueCallback (http://localhost:36401/dart_sdk.js:34435:49) at Function._propagateToListeners (http://localhost:36401/dart_sdk.js:34473:17) at _Future.immediate.[_completeWithValue] (http://localhost:36401/dart_sdk.js:34315:23) at async._AsyncCallbackEntry.new.callback (http://localhost:36401/dart_sdk.js:34338:35) at Object._microtaskLoop (http://localhost:36401/dart_sdk.js:39176:13) at _startMicrotaskLoop (http://localhost:36401/dart_sdk.js:39182:13) at http://localhost:36401/dart_sdk.js:34689:9

ajrcarey commented 3 years ago

Looks like it might be related to #5, I'm currently using package version 1.0.4.

ivoleitao commented 3 years ago

Yes, I've fixed this on the null version only. This is just the first problem there's a lot more as I haven't really tested this on dartjs before. It will be very hard to retrofit this as I had to fix a number of concurrency problems on the tests as well in order to have this fixed. Also I'm already moving to monorepo so the repo structure is completely changed. The best about this is that I will finally have a full encompassing CI process that will support dart js tests as well

ivoleitao commented 3 years ago

If you can move to the new published version (2.0.0-nullsafety.4) it would be ideal.

ajrcarey commented 3 years ago

Will do, many thanks.