Skip to content

Commit

Permalink
[i118] Fix NPE in StreamWebSocket (onFailure) (#4943)
Browse files Browse the repository at this point in the history
* Fix NPE in StreamWebSocket (onFailure)

(cherry picked from commit db8eee8)

* Add test

(cherry picked from commit d16d02a)

* add CHANGELOG

---------

Co-authored-by: Daniel Novak <[email protected]>
  • Loading branch information
kanat and DanielNovak authored Sep 5, 2023
1 parent 70ed186 commit 6788ae1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

## stream-chat-android-client
### 🐞 Fixed
- Fixed NPE in `StreamWebSocket.onFailure`. [#4943](https://github.com/GetStream/stream-chat-android/pull/4943)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ internal class StreamWebSocket(
private val parser: ChatParser,
socketCreator: (WebSocketListener) -> WebSocket,
) {
private val webSocketListener = object : WebSocketListener() {
private val eventFlow = MutableSharedFlow<StreamWebSocketEvent>(extraBufferCapacity = EVENTS_BUFFER_SIZE)

private val webSocket = socketCreator(object : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
eventFlow.tryEmit(parseMessage(text))
}
Expand All @@ -57,9 +59,7 @@ internal class StreamWebSocket(
eventFlow.tryEmit(StreamWebSocketEvent.Error(ChatNetworkError.create(ChatErrorCode.SOCKET_CLOSED)))
}
}
}
private val webSocket = socketCreator(webSocketListener)
private val eventFlow = MutableSharedFlow<StreamWebSocketEvent>(extraBufferCapacity = EVENTS_BUFFER_SIZE)
})

fun send(chatEvent: ChatEvent): Boolean = webSocket.send(parser.toJson(chatEvent))
fun close(): Boolean = webSocket.close(CLOSE_SOCKET_CODE, CLOSE_SOCKET_REASON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ internal class StreamWebSocketTest {
webSocket
}

@Test
fun `Class can be created correctly with listener invoked during creation`() {
StreamWebSocket(parser, socketCreator = {
it.onFailure(webSocket, IllegalStateException(), null)
webSocket
})
}

@Test
fun `When a chatEvent is sent, websocket should send a parsed event`() {
val chatEvent = mock<ChatEvent>()
Expand Down

0 comments on commit 6788ae1

Please sign in to comment.