Skip to content

Commit

Permalink
await all setAuth calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dshukertjr committed Dec 15, 2024
1 parent 9b323b9 commit 4205d8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions packages/realtime_client/lib/src/realtime_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ class RealtimeChannel {

joinPush.receive(
'ok',
(response) {
(response) async {
final serverPostgresFilters = response['postgres_changes'];
if (socket.accessToken != null) socket.setAuth(socket.accessToken);
if (socket.accessToken != null) {
await socket.setAuth(socket.accessToken);
}

if (serverPostgresFilters == null) {
if (callback != null) {
Expand Down
6 changes: 3 additions & 3 deletions packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class RealtimeClient {
if (heartbeatTimer != null) heartbeatTimer!.cancel();
heartbeatTimer = Timer.periodic(
Duration(milliseconds: heartbeatIntervalMs),
(Timer t) => sendHeartbeat(),
(Timer t) async => await sendHeartbeat(),
);
for (final callback in stateChangeCallbacks['open']!) {
callback();
Expand Down Expand Up @@ -533,7 +533,7 @@ class RealtimeClient {
}

@internal
void sendHeartbeat() {
Future<void> sendHeartbeat() async {
if (!isConnected) {
return;
}
Expand All @@ -555,6 +555,6 @@ class RealtimeClient {
payload: {},
ref: pendingHeartbeatRef!,
));
setAuth(accessToken);
await setAuth(accessToken);
}
}
18 changes: 9 additions & 9 deletions packages/realtime_client/test/socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void main() {
await Future.delayed(const Duration(milliseconds: 200));
expect(opens, 1);

socket.sendHeartbeat();
await socket.sendHeartbeat();
// need to wait for event to trigger
await Future.delayed(const Duration(seconds: 1));
expect(lastMsg['event'], 'heartbeat');
Expand Down Expand Up @@ -459,7 +459,7 @@ void main() {

test(
"sets access token, updates channels' join payload, and pushes token to channels",
() {
() async {
final mockedChannel1 = MockChannel();
when(() => mockedChannel1.joinedOnce).thenReturn(true);
when(() => mockedChannel1.isJoined).thenReturn(true);
Expand All @@ -484,7 +484,7 @@ void main() {
final channel1 = mockedSocket.channel(tTopic1);
final channel2 = mockedSocket.channel(tTopic2);

mockedSocket.setAuth(token);
await mockedSocket.setAuth(token);

expect(mockedSocket.accessToken, token);

Expand All @@ -498,7 +498,7 @@ void main() {

test(
"sets access token, updates channels' join payload, and pushes token to channels if is not a jwt",
() {
() async {
final mockedChannel1 = MockChannel();
final mockedChannel2 = MockChannel();
final mockedChannel3 = MockChannel();
Expand Down Expand Up @@ -537,7 +537,7 @@ void main() {
final pushPayload = {'access_token': token};
final updateJoinPayload = {'access_token': token};

mockedSocket.setAuth(token);
await mockedSocket.setAuth(token);

expect(mockedSocket.accessToken, token);

Expand Down Expand Up @@ -581,18 +581,18 @@ void main() {

//! Unimplemented Test: closes socket when heartbeat is not ack'd within heartbeat window

test('pushes heartbeat data when connected', () {
test('pushes heartbeat data when connected', () async {
mockedSocket.connState = SocketStates.open;

mockedSocket.sendHeartbeat();
await mockedSocket.sendHeartbeat();

verify(() => mockedSink.add(captureAny(that: equals(data)))).called(1);
});

test('no ops when not connected', () {
test('no ops when not connected', () async {
mockedSocket.connState = SocketStates.connecting;

mockedSocket.sendHeartbeat();
await mockedSocket.sendHeartbeat();
verifyNever(() => mockedSink.add(any()));
});
});
Expand Down

0 comments on commit 4205d8d

Please sign in to comment.