Skip to content

Fix reply timeouts #37

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

Merged
merged 3 commits into from
Mar 4, 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
4 changes: 2 additions & 2 deletions src/main/kotlin/org/phoenixframework/PhxPush.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class PhxPush(
val mutPayload = payload.toMutableMap()
mutPayload["status"] = status

refEvent?.let {
val message = PhxMessage(it, "", "", mutPayload)
refEvent?.let { safeRefEvent ->
val message = PhxMessage(event = safeRefEvent, payload = mutPayload)
this.channel.trigger(message)
}
}
Expand Down
52 changes: 51 additions & 1 deletion src/test/kotlin/org/phoenixframework/PhxChannelTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package org.phoenixframework

import com.google.common.truth.Truth.assertThat
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.mockito.Spy
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

class PhxChannelTest {

private val defaultRef = "1"
private val topic = "topic"

@Spy
var socket: PhxSocket = PhxSocket("http://localhost:4000/socket/websocket")
Expand All @@ -23,7 +31,7 @@ class PhxChannelTest {
Mockito.doReturn(defaultRef).`when`(socket).makeRef()

socket.timeout = 1234
channel = PhxChannel("topic", hashMapOf("one" to "two"), socket)
channel = PhxChannel(topic, hashMapOf("one" to "two"), socket)
}


Expand Down Expand Up @@ -149,4 +157,46 @@ class PhxChannelTest {

CompletableFuture.allOf(f1, f3).get(10, TimeUnit.SECONDS)
}

@Test
fun `issue 36 - verify timeouts remove bindings`() {
// mock okhttp to get isConnected to return true for the socket
val mockOkHttp = Mockito.mock(OkHttpClient::class.java)
val mockSocket = Mockito.mock(WebSocket::class.java)
`when`(mockOkHttp.newWebSocket(ArgumentMatchers.any(Request::class.java), ArgumentMatchers.any(WebSocketListener::class.java))).thenReturn(mockSocket)

// local mocks for this test
val localSocket = Mockito.spy(PhxSocket(url = "http://localhost:4000/socket/websocket", client = mockOkHttp))
val localChannel = PhxChannel(topic, hashMapOf("one" to "two"), localSocket)

// setup makeRef so it increments
val refCounter = AtomicInteger(1)
Mockito.doAnswer {
refCounter.getAndIncrement().toString()
}.`when`(localSocket).makeRef()

//connect the socket
localSocket.connect()

//join the channel
val joinPush = localChannel.join()
localChannel.trigger(PhxMessage(
ref = joinPush.ref!!,
joinRef = joinPush.ref!!,
event = PhxChannel.PhxEvent.REPLY.value,
topic = topic,
payload = mutableMapOf("status" to "ok")))

//get bindings
val originalBindingsSize = localChannel.bindings.size
val pushCount = 100
repeat(pushCount) {
localChannel.push("some-event", mutableMapOf(), timeout = 500)
}
//verify binding count before timeouts
assertThat(localChannel.bindings.size).isEqualTo(originalBindingsSize + pushCount)
Thread.sleep(1000)
//verify binding count after timeouts
assertThat(localChannel.bindings.size).isEqualTo(originalBindingsSize)
}
}