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

Fix isConnected #39

Merged
merged 1 commit into from
Feb 21, 2019
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
10 changes: 6 additions & 4 deletions src/main/kotlin/org/phoenixframework/PhxSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ open class PhxSocket(
/// Timer to use when attempting to reconnect
private var reconnectTimer: PhxTimer? = null


private val gson: Gson = GsonBuilder()
.setLenient()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
Expand Down Expand Up @@ -154,8 +153,8 @@ open class PhxSocket(
// Public
//------------------------------------------------------------------------------
/** True if the Socket is currently connected */
val isConnected: Boolean
get() = connection != null
var isConnected: Boolean = false
private set

/**
* Disconnects the Socket
Expand All @@ -173,7 +172,7 @@ open class PhxSocket(
*/
fun connect() {
// Do not attempt to reconnect if already connected
if (isConnected) return
if (connection != null) return
connection = client.newWebSocket(request, this)
}

Expand Down Expand Up @@ -440,6 +439,7 @@ open class PhxSocket(
// WebSocketListener
//------------------------------------------------------------------------------
override fun onOpen(webSocket: WebSocket?, response: Response?) {
isConnected = true
this.onConnectionOpened()

}
Expand All @@ -451,10 +451,12 @@ open class PhxSocket(
}

override fun onClosed(webSocket: WebSocket?, code: Int, reason: String?) {
isConnected = false
this.onConnectionClosed(code)
}

override fun onFailure(webSocket: WebSocket?, t: Throwable, response: Response?) {
isConnected = false
this.onConnectionError(t, response)
}
}
42 changes: 42 additions & 0 deletions src/test/kotlin/org/phoenixframework/PhxSocketTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.phoenixframework

import com.google.common.truth.Truth.assertThat
import okhttp3.WebSocket
import org.junit.Test
import org.mockito.Mockito

class PhxSocketTest {

Expand Down Expand Up @@ -36,4 +38,44 @@ class PhxSocketTest {
assertThat(PhxSocket("wss://localhost:4000/socket/websocket", spacesParams).endpoint.toString())
.isEqualTo("https://localhost:4000/socket/websocket?user_id=1&token=abc%20123")
}

@Test
fun isConnected_isTrue_WhenSocketConnected() {
val mockSocket = Mockito.mock(WebSocket::class.java)
val socket = PhxSocket("http://localhost:4000/socket/websocket")

socket.onOpen(mockSocket, null)

assertThat(socket.isConnected).isTrue()
}

@Test
fun isConnected_isFalse_WhenSocketNotYetConnected() {
val socket = PhxSocket("http://localhost:4000/socket/websocket")


assertThat(socket.isConnected).isFalse()
}

@Test
fun isConnected_isFalse_WhenSocketFailed() {
val mockSocket = Mockito.mock(WebSocket::class.java)
val socket = PhxSocket("http://localhost:4000/socket/websocket")

socket.onFailure(mockSocket, RuntimeException(), null)


assertThat(socket.isConnected).isFalse()
}

@Test
fun isConnected_isFalse_WhenSocketClosed() {
val mockSocket = Mockito.mock(WebSocket::class.java)
val socket = PhxSocket("http://localhost:4000/socket/websocket")

socket.onClosed(mockSocket, 0, "closed")


assertThat(socket.isConnected).isFalse()
}
}