Skip to content

Commit

Permalink
fix: don't stringify errors and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 16, 2024
1 parent 8e090c9 commit 463fdea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/realtime_client/lib/src/realtime_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class RealtimeChannel {
socket.remove(this);
});

_onError((String? reason) {
_onError((reason) {
if (isLeaving || isClosed) {
return;
}
Expand Down Expand Up @@ -260,9 +260,9 @@ class RealtimeChannel {
}

/// Registers a callback that will be executed when the channel encounteres an error.
void _onError(void Function(String?) callback) {
void _onError(Function callback) {
onEvents(ChannelEvents.error.eventName(), ChannelFilter(),
(reason, [ref]) => callback(reason?.toString()));
(reason, [ref]) => callback(reason));
}

/// Sets up a listener on your Supabase database.
Expand Down
2 changes: 1 addition & 1 deletion packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RealtimeCloseEvent {

@override
String toString() {
return 'RealtimeCloseEvent{code: $code, reason: $reason}';
return 'RealtimeCloseEvent(code: $code, reason: $reason)';
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/realtime_client/test/mock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ void main() {
final subscribeCallback =
expectAsync2((RealtimeSubscribeStatus event, error) {
if (event == RealtimeSubscribeStatus.channelError) {
expect(error, isNull);
expect(error, isA<RealtimeCloseEvent>());
error as RealtimeCloseEvent;
expect(error.reason, "heartbeat timeout");
} else {
expect(event, RealtimeSubscribeStatus.closed);
}
Expand All @@ -285,6 +287,7 @@ void main() {

channel.subscribe(subscribeCallback);

await Future.delayed(Duration(milliseconds: 200));
await client.conn!.sink
.close(Constants.wsCloseNormal, "heartbeat timeout");
});
Expand Down
6 changes: 5 additions & 1 deletion packages/realtime_client/test/socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ void main() {
});

socket.connect();
await Future.delayed(const Duration(milliseconds: 200));
expect(opens, 1);

socket.sendHeartbeat();
Expand Down Expand Up @@ -229,7 +230,7 @@ void main() {
expect(closes, 1);
});

test('calls connection close callback', () {
test('calls connection close callback', () async {
final mockedSocketChannel = MockIOWebSocketChannel();
final mockedSocket = RealtimeClient(
socketEndpoint,
Expand All @@ -247,7 +248,10 @@ void main() {
const tReason = 'reason';

mockedSocket.connect();
mockedSocket.connState = SocketStates.open;
await Future.delayed(const Duration(milliseconds: 200));
mockedSocket.disconnect(code: tCode, reason: tReason);
await Future.delayed(const Duration(milliseconds: 200));

verify(
() => mockedSink.close(
Expand Down

0 comments on commit 463fdea

Please sign in to comment.