moxxmpp/packages/moxxmpp_socket_tcp/integration_test/failure_reconnection_test.dart

94 lines
2.9 KiB
Dart
Raw Normal View History

import 'package:logging/logging.dart';
import 'package:moxxmpp/moxxmpp.dart';
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
import 'package:test/test.dart';
void main() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
2023-03-30 14:15:44 +00:00
// ignore: avoid_print
print('${record.level.name}: ${record.time}: ${record.message}');
});
final log = Logger('FailureReconnectionTest');
2023-03-30 14:15:44 +00:00
test(
'Failing an awaited connection with TestingSleepReconnectionPolicy',
() async {
var errors = 0;
final connection = XmppConnection(
TestingSleepReconnectionPolicy(10),
AlwaysConnectedConnectivityManager(),
ClientToServerNegotiator(),
2023-03-30 14:15:44 +00:00
TCPSocketWrapper(),
)..connectionSettings = ConnectionSettings(
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
password: 'abc123',
);
2023-04-02 12:38:32 +00:00
await connection.registerFeatureNegotiators([
StartTlsNegotiator(),
]);
2023-03-30 14:15:44 +00:00
connection.asBroadcastStream().listen((event) {
if (event is ConnectionStateChangedEvent) {
if (event.state == XmppConnectionState.error) {
errors++;
}
}
2023-03-30 14:15:44 +00:00
});
2023-03-30 14:15:44 +00:00
final result = await connection.connect(
shouldReconnect: false,
waitUntilLogin: true,
enableReconnectOnSuccess: false,
);
log.info('Connection failed as expected');
expect(result.isType<XmppError>(), false);
expect(errors, 1);
2023-03-30 14:15:44 +00:00
log.info('Waiting 20 seconds for unexpected reconnections');
await Future<void>.delayed(const Duration(seconds: 20));
expect(errors, 1);
},
timeout: const Timeout.factor(2),
);
2023-03-30 14:15:44 +00:00
test(
'Failing an awaited connection with ExponentialBackoffReconnectionPolicy',
() async {
var errors = 0;
final connection = XmppConnection(
TestingReconnectionPolicy(),
AlwaysConnectedConnectivityManager(),
ClientToServerNegotiator(),
2023-03-30 14:15:44 +00:00
TCPSocketWrapper(),
)..connectionSettings = ConnectionSettings(
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
password: 'abc123',
);
2023-04-02 12:38:32 +00:00
await connection.registerFeatureNegotiators([
StartTlsNegotiator(),
]);
2023-03-30 14:15:44 +00:00
connection.asBroadcastStream().listen((event) {
if (event is ConnectionStateChangedEvent) {
if (event.state == XmppConnectionState.error) {
errors++;
}
}
2023-03-30 14:15:44 +00:00
});
2023-03-30 14:15:44 +00:00
final result = await connection.connect(
shouldReconnect: false,
waitUntilLogin: true,
enableReconnectOnSuccess: false,
);
log.info('Connection failed as expected');
expect(result.isType<XmppError>(), false);
expect(errors, 1);
2023-03-30 14:15:44 +00:00
log.info('Waiting 20 seconds for unexpected reconnections');
await Future<void>.delayed(const Duration(seconds: 20));
expect(errors, 1);
},
timeout: const Timeout.factor(2),
);
}