test: Add integration test for ExponentialBackoffReconnectionPolicy

This commit is contained in:
PapaTutuWawa 2022-11-16 20:48:18 +01:00
parent bfd28c281e
commit 6b106fe365
2 changed files with 47 additions and 3 deletions

View File

@ -10,7 +10,7 @@ void main() {
});
final log = Logger('FailureReconnectionTest');
test('Failing an awaited connection', () async {
test('Failing an awaited connection with TestingSleepReconnectionPolicy', () async {
var errors = 0;
final connection = XmppConnection(
TestingSleepReconnectionPolicy(10),
@ -52,4 +52,47 @@ void main() {
await Future.delayed(const Duration(seconds: 20));
expect(errors, 1);
}, timeout: Timeout.factor(2));
test('Failing an awaited connection with ExponentialBackoffReconnectionPolicy', () async {
var errors = 0;
final connection = XmppConnection(
ExponentialBackoffReconnectionPolicy(1),
TCPSocketWrapper(false),
);
connection.registerFeatureNegotiators([
StartTlsNegotiator(),
]);
connection.registerManagers([
DiscoManager(),
RosterManager(),
PingManager(),
MessageManager(),
PresenceManager('http://moxxmpp.example'),
]);
connection.asBroadcastStream().listen((event) {
if (event is ConnectionStateChangedEvent) {
if (event.state == XmppConnectionState.error) {
errors++;
}
}
});
connection.setConnectionSettings(
ConnectionSettings(
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
password: 'abc123',
useDirectTLS: true,
allowPlainAuth: true,
),
);
final result = await connection.connectAwaitable();
log.info('Connection failed as expected');
expect(result.success, false);
expect(errors, 1);
log.info('Waiting 20 seconds for unexpected reconnections');
await Future.delayed(const Duration(seconds: 20));
expect(errors, 1);
}, timeout: Timeout.factor(2));
}

View File

@ -80,10 +80,11 @@ abstract class ReconnectionPolicy {
/// for every failed attempt.
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
ExponentialBackoffReconnectionPolicy()
ExponentialBackoffReconnectionPolicy(this._maxBackoffTime)
: _counter = 0,
_log = Logger('ExponentialBackoffReconnectionPolicy'),
super();
final int _maxBackoffTime;
int _counter;
Timer? _timer;
final Logger _log;
@ -124,7 +125,7 @@ class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
}
// Wait at max 80 seconds.
final seconds = min(pow(2, _counter).toInt(), 80);
final seconds = min(min(pow(2, _counter).toInt(), 80), _maxBackoffTime);
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
}