Compare commits
4 Commits
85995d51e4
...
b49658784b
Author | SHA1 | Date | |
---|---|---|---|
b49658784b | |||
d4a972e073 | |||
1009a2f967 | |||
f355f01fc8 |
@ -5,7 +5,7 @@ import 'package:moxxmpp/moxxmpp.dart';
|
|||||||
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
|
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
|
||||||
|
|
||||||
class ExampleTcpSocketWrapper extends TCPSocketWrapper {
|
class ExampleTcpSocketWrapper extends TCPSocketWrapper {
|
||||||
ExampleTcpSocketWrapper() : super(false);
|
ExampleTcpSocketWrapper() : super();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async {
|
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.
|
// The below causes the app to crash.
|
||||||
//ExampleTcpSocketWrapper(),
|
//ExampleTcpSocketWrapper(),
|
||||||
// In a production app, the below should be false.
|
// In a production app, the below should be false.
|
||||||
TCPSocketWrapper(true),
|
TCPSocketWrapper(),
|
||||||
);
|
);
|
||||||
TextEditingController jidController = TextEditingController();
|
TextEditingController jidController = TextEditingController();
|
||||||
TextEditingController passwordController = TextEditingController();
|
TextEditingController passwordController = TextEditingController();
|
||||||
|
@ -397,6 +397,9 @@ class XmppConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the socket
|
||||||
|
_socket.close();
|
||||||
|
|
||||||
if (!error.isRecoverable()) {
|
if (!error.isRecoverable()) {
|
||||||
// We cannot recover this error
|
// We cannot recover this error
|
||||||
_log.severe(
|
_log.severe(
|
||||||
|
@ -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(),
|
||||||
]);
|
]);
|
||||||
connection.registerManagers([
|
await connection.registerManagers([
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
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}');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -6,25 +6,27 @@ 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(
|
||||||
|
'Failing an awaited connection with TestingSleepReconnectionPolicy',
|
||||||
|
() async {
|
||||||
var errors = 0;
|
var errors = 0;
|
||||||
final connection = XmppConnection(
|
final connection = XmppConnection(
|
||||||
TestingSleepReconnectionPolicy(10),
|
TestingSleepReconnectionPolicy(10),
|
||||||
TCPSocketWrapper(false),
|
AlwaysConnectedConnectivityManager(),
|
||||||
);
|
TCPSocketWrapper(),
|
||||||
connection.registerFeatureNegotiators([
|
)..registerFeatureNegotiators([
|
||||||
StartTlsNegotiator(),
|
StartTlsNegotiator(),
|
||||||
]);
|
]);
|
||||||
connection.registerManagers([
|
await connection.registerManagers([
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
PingManager(),
|
|
||||||
MessageManager(),
|
MessageManager(),
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
]);
|
]);
|
||||||
connection.asBroadcastStream().listen((event) {
|
connection.asBroadcastStream().listen((event) {
|
||||||
if (event is ConnectionStateChangedEvent) {
|
if (event is ConnectionStateChangedEvent) {
|
||||||
@ -39,35 +41,41 @@ void main() {
|
|||||||
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(
|
||||||
|
shouldReconnect: false,
|
||||||
|
waitUntilLogin: true,
|
||||||
|
enableReconnectOnSuccess: false,
|
||||||
|
);
|
||||||
log.info('Connection failed as expected');
|
log.info('Connection failed as expected');
|
||||||
expect(result.success, false);
|
expect(result.isType<XmppError>(), false);
|
||||||
expect(errors, 1);
|
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(
|
||||||
|
'Failing an awaited connection with ExponentialBackoffReconnectionPolicy',
|
||||||
|
() async {
|
||||||
var errors = 0;
|
var errors = 0;
|
||||||
final connection = XmppConnection(
|
final connection = XmppConnection(
|
||||||
ExponentialBackoffReconnectionPolicy(1),
|
TestingReconnectionPolicy(),
|
||||||
TCPSocketWrapper(false),
|
AlwaysConnectedConnectivityManager(),
|
||||||
);
|
TCPSocketWrapper(),
|
||||||
connection.registerFeatureNegotiators([
|
)..registerFeatureNegotiators([
|
||||||
StartTlsNegotiator(),
|
StartTlsNegotiator(),
|
||||||
]);
|
]);
|
||||||
connection.registerManagers([
|
await connection.registerManagers([
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
PingManager(),
|
|
||||||
MessageManager(),
|
MessageManager(),
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
]);
|
]);
|
||||||
connection.asBroadcastStream().listen((event) {
|
connection.asBroadcastStream().listen((event) {
|
||||||
if (event is ConnectionStateChangedEvent) {
|
if (event is ConnectionStateChangedEvent) {
|
||||||
@ -82,17 +90,22 @@ void main() {
|
|||||||
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(
|
||||||
|
shouldReconnect: false,
|
||||||
|
waitUntilLogin: true,
|
||||||
|
enableReconnectOnSuccess: false,
|
||||||
|
);
|
||||||
log.info('Connection failed as expected');
|
log.info('Connection failed as expected');
|
||||||
expect(result.success, false);
|
expect(result.isType<XmppError>(), false);
|
||||||
expect(errors, 1);
|
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),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,6 @@ import 'package:moxxmpp_socket_tcp/src/rfc_2782.dart';
|
|||||||
|
|
||||||
/// TCP socket implementation for XmppConnection
|
/// TCP socket implementation for XmppConnection
|
||||||
class TCPSocketWrapper extends BaseSocketWrapper {
|
class TCPSocketWrapper extends BaseSocketWrapper {
|
||||||
TCPSocketWrapper(this._logData);
|
|
||||||
|
|
||||||
/// The underlying Socket/SecureSocket instance.
|
/// The underlying Socket/SecureSocket instance.
|
||||||
Socket? _socket;
|
Socket? _socket;
|
||||||
|
|
||||||
@ -31,9 +29,6 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
/// Logger
|
/// Logger
|
||||||
final Logger _log = Logger('TCPSocketWrapper');
|
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.
|
/// Indiacted whether the connection is secure.
|
||||||
bool _secure = false;
|
bool _secure = false;
|
||||||
|
|
||||||
@ -217,9 +212,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
_socketSubscription = _socket!.listen(
|
_socketSubscription = _socket!.listen(
|
||||||
(List<int> event) {
|
(List<int> event) {
|
||||||
final data = utf8.decode(event);
|
final data = utf8.decode(event);
|
||||||
if (_logData) {
|
|
||||||
_log.finest('<== $data');
|
_log.finest('<== $data');
|
||||||
}
|
|
||||||
_dataStream.add(data);
|
_dataStream.add(data);
|
||||||
},
|
},
|
||||||
onError: (Object error) {
|
onError: (Object error) {
|
||||||
@ -296,19 +289,17 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
_eventStream.stream.asBroadcastStream();
|
_eventStream.stream.asBroadcastStream();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void write(Object? data, {String? redact}) {
|
void write(String data, {String? redact}) {
|
||||||
if (_socket == null) {
|
if (_socket == null) {
|
||||||
_log.severe('Failed to write to socket as _socket is null');
|
_log.severe('Failed to write to socket as _socket is null');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data != null && data is String && _logData) {
|
|
||||||
if (redact != null) {
|
if (redact != null) {
|
||||||
_log.finest('**> $redact');
|
_log.finest('**> $redact');
|
||||||
} else {
|
} else {
|
||||||
_log.finest('==> $data');
|
_log.finest('==> $data');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_socket!.write(data);
|
_socket!.write(data);
|
||||||
|
Loading…
Reference in New Issue
Block a user