2022-11-12 20:49:13 +00:00
|
|
|
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
|
2022-11-12 20:49:13 +00:00
|
|
|
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(),
|
2023-06-25 10:47:47 +00:00
|
|
|
ClientToServerNegotiator(),
|
2023-09-29 18:53:29 +00:00
|
|
|
TCPSocketWrapper(true),
|
2023-06-25 10:47:47 +00:00
|
|
|
)..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++;
|
|
|
|
}
|
2022-11-16 19:48:18 +00:00
|
|
|
}
|
2023-03-30 14:15:44 +00:00
|
|
|
});
|
2022-11-16 19:48:18 +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);
|
2022-11-16 19:48:18 +00:00
|
|
|
|
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),
|
|
|
|
);
|
2022-11-16 19:48:18 +00:00
|
|
|
|
2023-03-30 14:15:44 +00:00
|
|
|
test(
|
|
|
|
'Failing an awaited connection with ExponentialBackoffReconnectionPolicy',
|
|
|
|
() async {
|
|
|
|
var errors = 0;
|
|
|
|
final connection = XmppConnection(
|
|
|
|
TestingReconnectionPolicy(),
|
|
|
|
AlwaysConnectedConnectivityManager(),
|
2023-06-25 10:47:47 +00:00
|
|
|
ClientToServerNegotiator(),
|
2023-09-29 18:53:29 +00:00
|
|
|
TCPSocketWrapper(true),
|
2023-06-25 10:47:47 +00:00
|
|
|
)..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++;
|
|
|
|
}
|
2022-11-12 20:49:13 +00:00
|
|
|
}
|
2023-03-30 14:15:44 +00:00
|
|
|
});
|
2022-11-12 20:49:13 +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);
|
2022-11-12 20:49:13 +00:00
|
|
|
|
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),
|
|
|
|
);
|
2022-11-12 20:49:13 +00:00
|
|
|
}
|