fix a bug with the throttling algorithm (#157)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 408a819..87671bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,6 @@
+## 4.0.2
+- Fix a bug with the analytics ping throttling algorithm.
+
## 4.0.1
- Force close the http client from `IOAnalytics.close()`.
This prevents lingering requests from making the application hang.
diff --git a/lib/src/usage_impl.dart b/lib/src/usage_impl.dart
index c774c6f..7fae01a 100644
--- a/lib/src/usage_impl.dart
+++ b/lib/src/usage_impl.dart
@@ -43,10 +43,10 @@
}
void _checkReplenish() {
- var now = DateTime.now().millisecondsSinceEpoch;
+ final now = DateTime.now().millisecondsSinceEpoch;
- if (_lastReplenish + 1000 >= now) {
- var inc = (now - _lastReplenish) ~/ 1000;
+ if (_lastReplenish + 1000 < now) {
+ final inc = (now - _lastReplenish) ~/ 1000;
drops = math.min(drops + inc, startingCount);
_lastReplenish += (1000 * inc);
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 36738fd..a70557a 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.
name: usage
-version: 4.0.1
+version: 4.0.2
description: A Google Analytics wrapper for command-line, web, and Flutter apps.
homepage: https://github.com/dart-lang/usage
@@ -15,4 +15,4 @@
dev_dependencies:
pedantic: ^1.9.0
- test: ^1.16.0-nullsafety
+ test: ^1.16.0
diff --git a/test/usage_impl_test.dart b/test/usage_impl_test.dart
index ae71261..8373d96 100644
--- a/test/usage_impl_test.dart
+++ b/test/usage_impl_test.dart
@@ -25,6 +25,18 @@
}
expect(bucket.removeDrop(), false);
});
+
+ test('does re-send after throttling', () async {
+ var bucket = ThrottlingBucket(20);
+ for (var i = 0; i < 20; i++) {
+ expect(bucket.removeDrop(), true);
+ }
+ expect(bucket.removeDrop(), false);
+
+ // TODO: Re-write to use package:fake_async.
+ await Future.delayed(Duration(milliseconds: 1500));
+ expect(bucket.removeDrop(), true);
+ });
});
group('AnalyticsImpl', () {