Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client Error Stream #3

Merged
merged 3 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ abstract class Client {
* the new [Channel]
*/
Future<Channel> channel();

/**
* Register listener for errors
*/
StreamSubscription<Exception> errorListener(void onData(Exception error), { Function onError, void onDone(), bool cancelOnError });
}
11 changes: 11 additions & 0 deletions lib/src/client/impl/client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class _ClientImpl implements Client {
Completer _connected;
Completer _clientClosed;

//Error Stream
StreamController _error = new StreamController.broadcast();

_ClientImpl({ConnectionSettings settings}) {

// Use defaults if no settings specified
Expand Down Expand Up @@ -166,6 +169,10 @@ class _ClientImpl implements Client {
return;
}

if (_error != null && _error.hasListener && !_error.isClosed) {
_error.add(ex);
}

switch (ex.runtimeType) {
case FatalException:
case ConnectionException:
Expand Down Expand Up @@ -236,6 +243,7 @@ class _ClientImpl implements Client {
_socket.destroy();
_socket = null;
_connected = null;
_error.close();
_clientClosed.complete();
});

Expand Down Expand Up @@ -270,4 +278,7 @@ class _ClientImpl implements Client {
return userChannel._channelOpened.future;
});
}

StreamSubscription<Exception> errorListener(void onData(Exception error), { Function onError, void onDone(), bool cancelOnError}) => _error.stream.listen(onData, onError : onError, onDone : onDone, cancelOnError : cancelOnError);

}
23 changes: 22 additions & 1 deletion test/lib/exception_handling_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library dart_amqp.test.exceptions;

import "dart:typed_data";
import "dart:async";

import "../packages/unittest/unittest.dart";
import "../packages/mock/mock.dart";
Expand Down Expand Up @@ -350,6 +351,26 @@ main({bool enableLogger : true}) {
}).catchError(expectAsync(handleError));
});
});

group("error stream:", () {
test("fatal exception", () async {
void handleError(ex) {
expect(ex, new isInstanceOf<FatalException>());
}
server.shutdown()
.then((_) => server.listen(client.settings.host, client.settings.port))
.then((_) {
generateHandshakeMessages(frameWriter, server);
return client
.connect()
.then((_) {
client.errorListener((ex) => handleError(ex));
return server.shutdown()
.then((_) => new Future.delayed(
new Duration(seconds: 5) + server.responseDelay))
.then((_) => fail("Expected an exception to be thrown"));
});
});
});
});
});
}