Init wsclient and code tests with rsocket client

This commit is contained in:
2020-07-11 21:01:38 +07:00
commit a26812a6c2
13 changed files with 447 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package com.cubetiqs.wsclient
import org.springframework.messaging.rsocket.RSocketRequester
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
@Service
interface StockClient {
fun priceFor(symbol: String): Flux<StockPrice>
}
@Service
class RSocketStockClient (
private val requesterBuilder: RSocketRequester.Builder
) : StockClient {
private fun createRSocketRequester(): RSocketRequester {
return requesterBuilder.connectTcp("localhost", 7000).block() ?: throw Exception("requester not loaded")
}
override fun priceFor(symbol: String): Flux<StockPrice> {
return createRSocketRequester().route("stockPrices")
.data(symbol)
.retrieveFlux(StockPrice::class.java)
}
}

View File

@@ -0,0 +1,9 @@
package com.cubetiqs.wsclient
import java.time.LocalDateTime
data class StockPrice (
val symbol: String,
val price: Double,
val time: LocalDateTime
)

View File

@@ -0,0 +1,6 @@
package com.cubetiqs.wsclient
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class WsClientApplication

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,23 @@
package com.cubetiqs.wsclient
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import reactor.core.publisher.Flux
@SpringBootTest
class RSocketClientApplicationTests @Autowired constructor(
private val stockClient: StockClient
) {
@Test
fun shouldRetrieveStockPricesFromTheService() {
val prices: Flux<StockPrice> = stockClient.priceFor("USD")
Assertions.assertNotNull(prices)
val fivePrices = prices.take(5)
Assertions.assertEquals(5, fivePrices.count().block())
Assertions.assertEquals("USD", fivePrices.blockFirst()?.symbol)
}
}

View File

@@ -0,0 +1,13 @@
package com.cubetiqs.wsclient
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class WsClientApplicationTests {
@Test
fun contextLoads() {
}
}