fix(tests): Fix integration tests

This commit is contained in:
PapaTutuWawa 2023-03-30 16:15:44 +02:00
parent 85995d51e4
commit f355f01fc8
3 changed files with 107 additions and 90 deletions

View File

@ -5,7 +5,7 @@ import 'package:test/test.dart';
Future<void> _runTest(String domain) async { Future<void> _runTest(String domain) async {
var gotTLSException = false; var gotTLSException = false;
final socket = TCPSocketWrapper(false); final socket = TCPSocketWrapper();
final log = Logger('TestLogger'); final log = Logger('TestLogger');
socket.getEventStream().listen((event) { socket.getEventStream().listen((event) {
if (event is XmppSocketTLSFailedEvent) { if (event is XmppSocketTLSFailedEvent) {
@ -15,18 +15,17 @@ Future<void> _runTest(String domain) async {
}); });
final connection = XmppConnection( final connection = XmppConnection(
ExponentialBackoffReconnectionPolicy(), TestingReconnectionPolicy(),
AlwaysConnectedConnectivityManager(),
socket, socket,
); )..registerFeatureNegotiators([
connection.registerFeatureNegotiators([ StartTlsNegotiator(),
StartTlsNegotiator(), ]);
]); await connection.registerManagers([
connection.registerManagers([ DiscoManager([]),
DiscoManager(), RosterManager(TestingRosterStateManager('', [])),
RosterManager(),
PingManager(),
MessageManager(), MessageManager(),
PresenceManager('http://moxxmpp.example'), PresenceManager(),
]); ]);
connection.setConnectionSettings( connection.setConnectionSettings(
@ -34,18 +33,22 @@ Future<void> _runTest(String domain) async {
jid: JID.fromString('testuser@$domain'), jid: JID.fromString('testuser@$domain'),
password: 'abc123', password: 'abc123',
useDirectTLS: true, useDirectTLS: true,
allowPlainAuth: true,
), ),
); );
final result = await connection.connectAwaitable(); final result = await connection.connect(
expect(result.success, false); shouldReconnect: false,
waitUntilLogin: true,
enableReconnectOnSuccess: false,
);
expect(result.isType<XmppError>(), false);
expect(gotTLSException, true); expect(gotTLSException, true);
} }
void main() { void main() {
Logger.root.level = Level.ALL; Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
// ignore: avoid_print
print('${record.level.name}: ${record.time}: ${record.message}'); print('${record.level.name}: ${record.time}: ${record.message}');
}); });

View File

@ -6,93 +6,106 @@ import 'package:test/test.dart';
void main() { void main() {
Logger.root.level = Level.ALL; Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
// ignore: avoid_print
print('${record.level.name}: ${record.time}: ${record.message}'); print('${record.level.name}: ${record.time}: ${record.message}');
}); });
final log = Logger('FailureReconnectionTest'); final log = Logger('FailureReconnectionTest');
test('Failing an awaited connection with TestingSleepReconnectionPolicy', () async { test(
var errors = 0; 'Failing an awaited connection with TestingSleepReconnectionPolicy',
final connection = XmppConnection( () async {
TestingSleepReconnectionPolicy(10), var errors = 0;
TCPSocketWrapper(false), final connection = XmppConnection(
); TestingSleepReconnectionPolicy(10),
connection.registerFeatureNegotiators([ AlwaysConnectedConnectivityManager(),
StartTlsNegotiator(), TCPSocketWrapper(),
]); )..registerFeatureNegotiators([
connection.registerManagers([ StartTlsNegotiator(),
DiscoManager(), ]);
RosterManager(), await connection.registerManagers([
PingManager(), DiscoManager([]),
MessageManager(), RosterManager(TestingRosterStateManager('', [])),
PresenceManager('http://moxxmpp.example'), MessageManager(),
]); PresenceManager(),
connection.asBroadcastStream().listen((event) { ]);
if (event is ConnectionStateChangedEvent) { connection.asBroadcastStream().listen((event) {
if (event.state == XmppConnectionState.error) { if (event is ConnectionStateChangedEvent) {
errors++; if (event.state == XmppConnectionState.error) {
errors++;
}
} }
} });
});
connection.setConnectionSettings( connection.setConnectionSettings(
ConnectionSettings( ConnectionSettings(
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'), jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
password: 'abc123', password: 'abc123',
useDirectTLS: true, useDirectTLS: true,
allowPlainAuth: true, ),
), );
);
final result = await connection.connectAwaitable(); final result = await connection.connect(
log.info('Connection failed as expected'); shouldReconnect: false,
expect(result.success, false); waitUntilLogin: true,
expect(errors, 1); enableReconnectOnSuccess: false,
);
log.info('Connection failed as expected');
expect(result.isType<XmppError>(), false);
expect(errors, 1);
log.info('Waiting 20 seconds for unexpected reconnections'); log.info('Waiting 20 seconds for unexpected reconnections');
await Future.delayed(const Duration(seconds: 20)); await Future<void>.delayed(const Duration(seconds: 20));
expect(errors, 1); expect(errors, 1);
}, timeout: Timeout.factor(2)); },
timeout: const Timeout.factor(2),
);
test('Failing an awaited connection with ExponentialBackoffReconnectionPolicy', () async { test(
var errors = 0; 'Failing an awaited connection with ExponentialBackoffReconnectionPolicy',
final connection = XmppConnection( () async {
ExponentialBackoffReconnectionPolicy(1), var errors = 0;
TCPSocketWrapper(false), final connection = XmppConnection(
); TestingReconnectionPolicy(),
connection.registerFeatureNegotiators([ AlwaysConnectedConnectivityManager(),
StartTlsNegotiator(), TCPSocketWrapper(),
]); )..registerFeatureNegotiators([
connection.registerManagers([ StartTlsNegotiator(),
DiscoManager(), ]);
RosterManager(), await connection.registerManagers([
PingManager(), DiscoManager([]),
MessageManager(), RosterManager(TestingRosterStateManager('', [])),
PresenceManager('http://moxxmpp.example'), MessageManager(),
]); PresenceManager(),
connection.asBroadcastStream().listen((event) { ]);
if (event is ConnectionStateChangedEvent) { connection.asBroadcastStream().listen((event) {
if (event.state == XmppConnectionState.error) { if (event is ConnectionStateChangedEvent) {
errors++; if (event.state == XmppConnectionState.error) {
errors++;
}
} }
} });
});
connection.setConnectionSettings( connection.setConnectionSettings(
ConnectionSettings( ConnectionSettings(
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'), jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
password: 'abc123', password: 'abc123',
useDirectTLS: true, useDirectTLS: true,
allowPlainAuth: true, ),
), );
);
final result = await connection.connectAwaitable(); final result = await connection.connect(
log.info('Connection failed as expected'); shouldReconnect: false,
expect(result.success, false); waitUntilLogin: true,
expect(errors, 1); enableReconnectOnSuccess: false,
);
log.info('Connection failed as expected');
expect(result.isType<XmppError>(), false);
expect(errors, 1);
log.info('Waiting 20 seconds for unexpected reconnections'); log.info('Waiting 20 seconds for unexpected reconnections');
await Future.delayed(const Duration(seconds: 20)); await Future<void>.delayed(const Duration(seconds: 20));
expect(errors, 1); expect(errors, 1);
}, timeout: Timeout.factor(2)); },
timeout: const Timeout.factor(2),
);
} }