From 83ebe58c476dffd1c6589e055e512ae1029fe944 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Wed, 24 May 2023 22:03:17 +0200 Subject: [PATCH] feat(tests): Test emptying stanza queue on connect --- packages/moxxmpp/lib/src/connection.dart | 5 +- packages/moxxmpp/test/xmpp_test.dart | 174 +++++++++++++++++++++++ 2 files changed, 177 insertions(+), 2 deletions(-) diff --git a/packages/moxxmpp/lib/src/connection.dart b/packages/moxxmpp/lib/src/connection.dart index af00139..872a985 100644 --- a/packages/moxxmpp/lib/src/connection.dart +++ b/packages/moxxmpp/lib/src/connection.dart @@ -80,7 +80,7 @@ class XmppConnection { _socketStream = _socket.getDataStream(); // TODO(Unknown): Handle on done _socketStream.transform(_streamParser).forEach(handleXmlStream); - _socket.getEventStream().listen(_handleSocketEvent); + _socket.getEventStream().listen(handleSocketEvent); _stanzaQueue = AsyncStanzaQueue( _sendStanzaImpl, @@ -362,7 +362,8 @@ class XmppConnection { } /// Called whenever the socket creates an event - Future _handleSocketEvent(XmppSocketEvent event) async { + @visibleForTesting + Future handleSocketEvent(XmppSocketEvent event) async { if (event is XmppSocketErrorEvent) { await handleError(SocketError(event)); } else if (event is XmppSocketClosureEvent) { diff --git a/packages/moxxmpp/test/xmpp_test.dart b/packages/moxxmpp/test/xmpp_test.dart index 1f2277e..50675f4 100644 --- a/packages/moxxmpp/test/xmpp_test.dart +++ b/packages/moxxmpp/test/xmpp_test.dart @@ -4,6 +4,32 @@ import 'package:test/test.dart'; import 'helpers/logging.dart'; import 'helpers/xmpp.dart'; +class StubConnectivityManager extends ConnectivityManager { + bool _hasConnection = true; + + Completer _goingOnlineCompleter = Completer(); + + @override + Future hasConnection() async => _hasConnection; + + @override + Future waitForConnection() async { + if (!_hasConnection) { + await _goingOnlineCompleter.future; + } + } + + void goOffline() { + _hasConnection = false; + } + + void goOnline() { + _hasConnection = true; + _goingOnlineCompleter.complete(); + _goingOnlineCompleter = Completer(); + } +} + /// Returns true if the roster manager triggeres an event for a given stanza Future testRosterManager( String bareJid, @@ -619,4 +645,152 @@ void main() { true, ); }); + + test('Test sending stanzas while offline', () async { + final fakeSocket = StubTCPSocket( + [ + StringExpectation( + "", + ''' + + + + PLAIN + + ''', + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' + + + + + + + + + + + +''', + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StringExpectation( + "", + ''' + + + + PLAIN + + ''', + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' + + + + + + + + + + + +''', + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StanzaExpectation( + '', + '', + ignoreId: true, + ), + ], + ); + final connectivity = StubConnectivityManager(); + final conn = XmppConnection( + TestingReconnectionPolicy(), + connectivity, + ClientToServerNegotiator(), + fakeSocket, + )..connectionSettings = ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + ); + await conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + SaslScramNegotiator(10, '', '', ScramHashType.sha512), + ResourceBindingNegotiator(), + ]); + + await conn.connect( + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), 4); + + // Fake going offline + connectivity.goOffline(); + await conn.handleSocketEvent( + XmppSocketClosureEvent(false), + ); + + // Send a stanza while offline + final stanzaFuture = conn.sendStanza( + StanzaDetails( + Stanza.iq( + id: 'abc123', + type: 'get', + ), + ), + ); + + // Come online again + connectivity.goOnline(); + await conn.connect( + waitUntilLogin: true, + ); + await Future.delayed(const Duration(seconds: 6)); + + expect(fakeSocket.getState(), 9); + expect(await stanzaFuture != null, true); + }); }