forked from Tinder/Scarlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scarlet-stream-adapter-coroutines produces hot flow and hot receive c…
…hannel Fixes Tinder#169 Fixes Tinder#163 Fixes Tinder#27
- Loading branch information
1 parent
952bdba
commit 9433b84
Showing
7 changed files
with
232 additions
and
93 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...-adapter-coroutines/src/main/java/com/tinder/streamadapter/coroutines/ChannelForwarder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.tinder.streamadapter.coroutines | ||
|
||
import com.tinder.scarlet.Stream | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.channels.ReceiveChannel | ||
import kotlinx.coroutines.channels.sendBlocking | ||
|
||
internal class ChannelForwarder<T> : Stream.Observer<T> { | ||
private val _channel = Channel<T>() | ||
val channel: ReceiveChannel<T> = _channel | ||
|
||
fun start(stream: Stream<T>): ReceiveChannel<T> { | ||
stream.start(this) | ||
return channel | ||
} | ||
|
||
override fun onComplete() { | ||
_channel.close() | ||
} | ||
|
||
override fun onError(throwable: Throwable) { | ||
_channel.close(throwable) | ||
} | ||
|
||
override fun onNext(data: T) { | ||
println("$data is ") | ||
_channel.sendBlocking(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...coroutines/src/test/java/com/tinder/scarlet/streamadapter/coroutines/ClientServerModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.tinder.scarlet.streamadapter.coroutines | ||
|
||
import com.tinder.scarlet.Lifecycle | ||
import com.tinder.scarlet.Scarlet | ||
import com.tinder.scarlet.WebSocket | ||
import com.tinder.scarlet.lifecycle.LifecycleRegistry | ||
import com.tinder.scarlet.testutils.TestStreamObserver | ||
import com.tinder.scarlet.testutils.ValueAssert | ||
import com.tinder.scarlet.testutils.any | ||
import com.tinder.scarlet.websocket.mockwebserver.newWebSocketFactory | ||
import com.tinder.scarlet.websocket.okhttp.newWebSocketFactory | ||
import com.tinder.streamadapter.coroutines.CoroutinesStreamAdapterFactory | ||
import okhttp3.OkHttpClient | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.rules.ExternalResource | ||
import java.util.concurrent.TimeUnit | ||
|
||
abstract class ClientServerModel<T : Any>(private val clazz: Class<T>) : ExternalResource() { | ||
|
||
val mockWebServer = MockWebServer() | ||
private val serverUrlString by lazy { mockWebServer.url("/").toString() } | ||
|
||
private val serverLifecycleRegistry = LifecycleRegistry() | ||
private lateinit var server: T | ||
private lateinit var serverEventObserver: TestStreamObserver<WebSocket.Event> | ||
|
||
private val clientLifecycleRegistry = LifecycleRegistry() | ||
private lateinit var client: T | ||
private lateinit var clientEventObserver: TestStreamObserver<WebSocket.Event> | ||
|
||
internal fun givenConnectionIsEstablished(): Pair<T, T> { | ||
createClientAndServer() | ||
serverLifecycleRegistry.onNext(Lifecycle.State.Started) | ||
clientLifecycleRegistry.onNext(Lifecycle.State.Started) | ||
blockUntilConnectionIsEstablish() | ||
return client to server | ||
} | ||
|
||
internal fun createClientAndServer() { | ||
server = createServer() | ||
client = createClient() | ||
clientEventObserver = observeWebSocketEvents(client) // client.observeEvents().test() | ||
serverEventObserver = observeWebSocketEvents(server) // server.observeEvents().test() | ||
} | ||
abstract fun observeWebSocketEvents(service: T): TestStreamObserver<WebSocket.Event> | ||
internal fun createServer(): T { | ||
val webSocketFactory = mockWebServer.newWebSocketFactory() | ||
val scarlet = Scarlet.Builder() | ||
.webSocketFactory(webSocketFactory) | ||
.lifecycle(serverLifecycleRegistry) | ||
.addStreamAdapterFactory(CoroutinesStreamAdapterFactory()) | ||
.build() | ||
return scarlet.create(clazz) | ||
} | ||
|
||
internal fun createClient(): T { | ||
val okHttpClient = OkHttpClient.Builder() | ||
.writeTimeout(500, TimeUnit.MILLISECONDS) | ||
.readTimeout(500, TimeUnit.MILLISECONDS) | ||
.build() | ||
val webSocketFactory = okHttpClient.newWebSocketFactory(serverUrlString) | ||
val scarlet = Scarlet.Builder() | ||
.webSocketFactory(webSocketFactory) | ||
.lifecycle(clientLifecycleRegistry) | ||
.addStreamAdapterFactory(CoroutinesStreamAdapterFactory()) | ||
.build() | ||
return scarlet.create(clazz) | ||
} | ||
|
||
internal fun awaitValues(vararg values: ValueAssert<Any>) { | ||
serverEventObserver.awaitValues(*values) | ||
} | ||
|
||
internal fun blockUntilConnectionIsEstablish() { | ||
clientEventObserver.awaitValues( | ||
any<WebSocket.Event.OnConnectionOpened<*>>() | ||
) | ||
serverEventObserver.awaitValues( | ||
any<WebSocket.Event.OnConnectionOpened<*>>() | ||
) | ||
} | ||
|
||
override fun after() { | ||
mockWebServer.shutdown() | ||
} | ||
|
||
override fun before() { | ||
mockWebServer.start() | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...utines/src/test/java/com/tinder/scarlet/streamadapter/coroutines/FlowStreamAdapterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.tinder.scarlet.streamadapter.coroutines | ||
|
||
import com.google.common.truth.Correspondence | ||
import com.google.common.truth.Truth.assertThat | ||
import com.tinder.scarlet.Stream | ||
import com.tinder.scarlet.WebSocket | ||
import com.tinder.scarlet.testutils.TestStreamObserver | ||
import com.tinder.scarlet.testutils.any | ||
import com.tinder.scarlet.testutils.containingBytes | ||
import com.tinder.scarlet.testutils.containingText | ||
import com.tinder.scarlet.testutils.test | ||
import com.tinder.scarlet.ws.Receive | ||
import com.tinder.scarlet.ws.Send | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.take | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import java.util.Arrays | ||
|
||
class FlowStreamAdapterTest { | ||
|
||
@get:Rule | ||
val clientServerModel = object : ClientServerModel<Service>(Service::class.java) { | ||
override fun observeWebSocketEvents(service: Service): TestStreamObserver<WebSocket.Event> { | ||
return service.observeEvents().test() | ||
} | ||
} | ||
|
||
fun `adapt - given a stream of strings, provides a Flow interface bound to the stream`() = | ||
runBlocking { | ||
// Given | ||
val (client, server) = clientServerModel.givenConnectionIsEstablished() | ||
val textMessage1 = "Hello" | ||
val textMessage2 = "Hi!" | ||
val bytesMessage1 = "Yo".toByteArray() | ||
val bytesMessage2 = "Sup".toByteArray() | ||
val testTextChannel = server.observeText() | ||
val testBytesChannel = server.observeBytes() | ||
|
||
// When | ||
client.sendText(textMessage1) | ||
val isSendTextSuccessful = client.sendTextAndConfirm(textMessage2) | ||
client.sendBytes(bytesMessage1) | ||
val isSendBytesSuccessful = client.sendBytesAndConfirm(bytesMessage2) | ||
|
||
// Then | ||
assertThat(isSendBytesSuccessful).isTrue() | ||
assertThat(isSendTextSuccessful).isTrue() | ||
|
||
clientServerModel.awaitValues( | ||
any<WebSocket.Event.OnConnectionOpened<*>>(), | ||
any<WebSocket.Event.OnMessageReceived>().containingText(textMessage1), | ||
any<WebSocket.Event.OnMessageReceived>().containingText(textMessage2), | ||
any<WebSocket.Event.OnMessageReceived>().containingBytes(bytesMessage1), | ||
any<WebSocket.Event.OnMessageReceived>().containingBytes(bytesMessage2) | ||
) | ||
|
||
assertThat(testTextChannel.take(2).toList()).containsExactly( | ||
textMessage1, | ||
textMessage2 | ||
).inOrder() | ||
|
||
val bytes = testBytesChannel.take(2).toList() | ||
assertThat(bytes).comparingElementsUsing(BYTE_ARRAY_CORRESPONDENCE).containsExactly( | ||
bytesMessage1, | ||
bytesMessage2 | ||
).inOrder() | ||
} | ||
|
||
val BYTE_ARRAY_CORRESPONDENCE = Correspondence.from<ByteArray, ByteArray>({ actual, expected -> | ||
Arrays.compare(actual, expected) == 0 | ||
}, "Compare using Arrays.equals") | ||
|
||
interface Service { | ||
@Receive | ||
fun observeEvents(): Stream<WebSocket.Event> | ||
|
||
@Receive | ||
fun observeText(): Flow<String> | ||
|
||
@Receive | ||
fun observeBytes(): Flow<ByteArray> | ||
|
||
@Send | ||
fun sendText(message: String) | ||
|
||
@Send | ||
fun sendTextAndConfirm(message: String): Boolean | ||
|
||
@Send | ||
fun sendBytes(message: ByteArray) | ||
|
||
@Send | ||
fun sendBytesAndConfirm(message: ByteArray): Boolean | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters