Compare commits
No commits in common. "b49658784b2ce27dae209ad55079d3f802014cac" and "85995d51e4d9e1518ee8647ea684631a5d927129" have entirely different histories.
b49658784b
...
85995d51e4
@ -5,7 +5,7 @@ import 'package:moxxmpp/moxxmpp.dart';
|
||||
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
|
||||
|
||||
class ExampleTcpSocketWrapper extends TCPSocketWrapper {
|
||||
ExampleTcpSocketWrapper() : super();
|
||||
ExampleTcpSocketWrapper() : super(false);
|
||||
|
||||
@override
|
||||
Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async {
|
||||
@ -64,7 +64,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
// The below causes the app to crash.
|
||||
//ExampleTcpSocketWrapper(),
|
||||
// In a production app, the below should be false.
|
||||
TCPSocketWrapper(),
|
||||
TCPSocketWrapper(true),
|
||||
);
|
||||
TextEditingController jidController = TextEditingController();
|
||||
TextEditingController passwordController = TextEditingController();
|
||||
|
@ -397,9 +397,6 @@ class XmppConnection {
|
||||
return;
|
||||
}
|
||||
|
||||
// Close the socket
|
||||
_socket.close();
|
||||
|
||||
if (!error.isRecoverable()) {
|
||||
// We cannot recover this error
|
||||
_log.severe(
|
||||
|
@ -5,7 +5,7 @@ import 'package:test/test.dart';
|
||||
|
||||
Future<void> _runTest(String domain) async {
|
||||
var gotTLSException = false;
|
||||
final socket = TCPSocketWrapper();
|
||||
final socket = TCPSocketWrapper(false);
|
||||
final log = Logger('TestLogger');
|
||||
socket.getEventStream().listen((event) {
|
||||
if (event is XmppSocketTLSFailedEvent) {
|
||||
@ -15,17 +15,18 @@ Future<void> _runTest(String domain) async {
|
||||
});
|
||||
|
||||
final connection = XmppConnection(
|
||||
TestingReconnectionPolicy(),
|
||||
AlwaysConnectedConnectivityManager(),
|
||||
ExponentialBackoffReconnectionPolicy(),
|
||||
socket,
|
||||
)..registerFeatureNegotiators([
|
||||
StartTlsNegotiator(),
|
||||
]);
|
||||
await connection.registerManagers([
|
||||
DiscoManager([]),
|
||||
RosterManager(TestingRosterStateManager('', [])),
|
||||
);
|
||||
connection.registerFeatureNegotiators([
|
||||
StartTlsNegotiator(),
|
||||
]);
|
||||
connection.registerManagers([
|
||||
DiscoManager(),
|
||||
RosterManager(),
|
||||
PingManager(),
|
||||
MessageManager(),
|
||||
PresenceManager(),
|
||||
PresenceManager('http://moxxmpp.example'),
|
||||
]);
|
||||
|
||||
connection.setConnectionSettings(
|
||||
@ -33,22 +34,18 @@ Future<void> _runTest(String domain) async {
|
||||
jid: JID.fromString('testuser@$domain'),
|
||||
password: 'abc123',
|
||||
useDirectTLS: true,
|
||||
allowPlainAuth: true,
|
||||
),
|
||||
);
|
||||
|
||||
final result = await connection.connect(
|
||||
shouldReconnect: false,
|
||||
waitUntilLogin: true,
|
||||
enableReconnectOnSuccess: false,
|
||||
);
|
||||
expect(result.isType<XmppError>(), false);
|
||||
final result = await connection.connectAwaitable();
|
||||
expect(result.success, false);
|
||||
expect(gotTLSException, true);
|
||||
}
|
||||
|
||||
void main() {
|
||||
Logger.root.level = Level.ALL;
|
||||
Logger.root.onRecord.listen((record) {
|
||||
// ignore: avoid_print
|
||||
print('${record.level.name}: ${record.time}: ${record.message}');
|
||||
});
|
||||
|
||||
|
@ -6,106 +6,93 @@ import 'package:test/test.dart';
|
||||
void main() {
|
||||
Logger.root.level = Level.ALL;
|
||||
Logger.root.onRecord.listen((record) {
|
||||
// ignore: avoid_print
|
||||
print('${record.level.name}: ${record.time}: ${record.message}');
|
||||
});
|
||||
final log = Logger('FailureReconnectionTest');
|
||||
|
||||
test(
|
||||
'Failing an awaited connection with TestingSleepReconnectionPolicy',
|
||||
() async {
|
||||
var errors = 0;
|
||||
final connection = XmppConnection(
|
||||
TestingSleepReconnectionPolicy(10),
|
||||
AlwaysConnectedConnectivityManager(),
|
||||
TCPSocketWrapper(),
|
||||
)..registerFeatureNegotiators([
|
||||
StartTlsNegotiator(),
|
||||
]);
|
||||
await connection.registerManagers([
|
||||
DiscoManager([]),
|
||||
RosterManager(TestingRosterStateManager('', [])),
|
||||
MessageManager(),
|
||||
PresenceManager(),
|
||||
]);
|
||||
connection.asBroadcastStream().listen((event) {
|
||||
if (event is ConnectionStateChangedEvent) {
|
||||
if (event.state == XmppConnectionState.error) {
|
||||
errors++;
|
||||
}
|
||||
test('Failing an awaited connection with TestingSleepReconnectionPolicy', () async {
|
||||
var errors = 0;
|
||||
final connection = XmppConnection(
|
||||
TestingSleepReconnectionPolicy(10),
|
||||
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,
|
||||
),
|
||||
);
|
||||
connection.setConnectionSettings(
|
||||
ConnectionSettings(
|
||||
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
|
||||
password: 'abc123',
|
||||
useDirectTLS: true,
|
||||
allowPlainAuth: true,
|
||||
),
|
||||
);
|
||||
|
||||
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);
|
||||
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<void>.delayed(const Duration(seconds: 20));
|
||||
expect(errors, 1);
|
||||
},
|
||||
timeout: const Timeout.factor(2),
|
||||
);
|
||||
log.info('Waiting 20 seconds for unexpected reconnections');
|
||||
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(
|
||||
TestingReconnectionPolicy(),
|
||||
AlwaysConnectedConnectivityManager(),
|
||||
TCPSocketWrapper(),
|
||||
)..registerFeatureNegotiators([
|
||||
StartTlsNegotiator(),
|
||||
]);
|
||||
await connection.registerManagers([
|
||||
DiscoManager([]),
|
||||
RosterManager(TestingRosterStateManager('', [])),
|
||||
MessageManager(),
|
||||
PresenceManager(),
|
||||
]);
|
||||
connection.asBroadcastStream().listen((event) {
|
||||
if (event is ConnectionStateChangedEvent) {
|
||||
if (event.state == XmppConnectionState.error) {
|
||||
errors++;
|
||||
}
|
||||
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,
|
||||
),
|
||||
);
|
||||
connection.setConnectionSettings(
|
||||
ConnectionSettings(
|
||||
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
|
||||
password: 'abc123',
|
||||
useDirectTLS: true,
|
||||
allowPlainAuth: true,
|
||||
),
|
||||
);
|
||||
|
||||
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);
|
||||
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<void>.delayed(const Duration(seconds: 20));
|
||||
expect(errors, 1);
|
||||
},
|
||||
timeout: const Timeout.factor(2),
|
||||
);
|
||||
log.info('Waiting 20 seconds for unexpected reconnections');
|
||||
await Future.delayed(const Duration(seconds: 20));
|
||||
expect(errors, 1);
|
||||
}, timeout: Timeout.factor(2));
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import 'package:moxxmpp_socket_tcp/src/rfc_2782.dart';
|
||||
|
||||
/// TCP socket implementation for XmppConnection
|
||||
class TCPSocketWrapper extends BaseSocketWrapper {
|
||||
TCPSocketWrapper(this._logData);
|
||||
|
||||
/// The underlying Socket/SecureSocket instance.
|
||||
Socket? _socket;
|
||||
|
||||
@ -29,6 +31,9 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
||||
/// Logger
|
||||
final Logger _log = Logger('TCPSocketWrapper');
|
||||
|
||||
/// Flag to indicate if incoming and outgoing data should get logged.
|
||||
final bool _logData;
|
||||
|
||||
/// Indiacted whether the connection is secure.
|
||||
bool _secure = false;
|
||||
|
||||
@ -212,7 +217,9 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
||||
_socketSubscription = _socket!.listen(
|
||||
(List<int> event) {
|
||||
final data = utf8.decode(event);
|
||||
_log.finest('<== $data');
|
||||
if (_logData) {
|
||||
_log.finest('<== $data');
|
||||
}
|
||||
_dataStream.add(data);
|
||||
},
|
||||
onError: (Object error) {
|
||||
@ -289,16 +296,18 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
||||
_eventStream.stream.asBroadcastStream();
|
||||
|
||||
@override
|
||||
void write(String data, {String? redact}) {
|
||||
void write(Object? data, {String? redact}) {
|
||||
if (_socket == null) {
|
||||
_log.severe('Failed to write to socket as _socket is null');
|
||||
return;
|
||||
}
|
||||
|
||||
if (redact != null) {
|
||||
_log.finest('**> $redact');
|
||||
} else {
|
||||
_log.finest('==> $data');
|
||||
if (data != null && data is String && _logData) {
|
||||
if (redact != null) {
|
||||
_log.finest('**> $redact');
|
||||
} else {
|
||||
_log.finest('==> $data');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user