Skip to content

Instantly share code, notes, and snippets.

View MovileGente's full-sized avatar

Movile MovileGente

View GitHub Profile
{
"type": "record",
"name": "SubscriptionRequest",
"namespace": "com.felipeassoline.subscription.payment.api",
"fields": [
{"name": "cardNumber", "type": ["string", "null"]},
{"name": "secret", "type": ["string", "null"]},
{"name": "status", "type": ["string", "null"]}
]
}
cloud.stream:
bindings.output:
destination: payment-approval-topic
contentType: application/*+avro
schema:
avro:
schema-locations: classpath:avro/schema.avsc
schema-registry-client:
endpoint: http://localhost:8080
@SpringBootApplication
@EnableSchemaRegistryServer
public class SchemaRegistryServerApplication {
public static void main(String[] args) {
SpringApplication.run(SchemaRegistryServerApplication.class, args);
}
}
func testWhateverINeed() {
// Given
// create here the mocked data needed to call the method below
// When
// call here the method you will test
// Then
// insert here the assertions
}
// a keyword `suspend` define uma suspending function
suspend fun queryUser(id: Int): User {
 delay(100) // simulando latência de rede ou outra operação lenta
 return User(id = id, name = "gente.firme", email = "talentos@movile.com")
}
interface Continuation<in T> {
   val context: CoroutineContext
   fun resume(value: T)
   fun resumeWithException(exception: Throwable)
}
suspend fun queryUser(id: Int): User {
   delay(500) // simulando latência de rede ou outra operação lenta
   return User(id = id, name = "gente.firme", email = "talentos@movile.com", phone = 5511000000000L)
}
suspend fun sendEmail(destination: String, body: String): Boolean {
   println("Sending email to $destination with body: $body")
   delay(1000) // simulando um request/operação lenta
   val couldSentEmail = Random().nextBoolean()
   if (couldSentEmail) {
fun sendMessageToUser(userId: Int, message: String, cont: Continuation) {
   val stateMachine = cont as? CoroutineImpl // verifica se já temos uma máquina de estados instanciada (ou seja, já passamos pelo menos uma vez por essa função)
                      ?: object : CoroutineImpl { // cria uma máquina de estados que chama a própria função
       fun resume(...) {
           sendMessageToUser(userId, message, this) // Hooray, Callback
       }
   }
   switch(stateMachine.label) {
   case 0: // Kotlin usa labels para saber em qual passo estamos
sealed class ActorCommand<out T> {
   class REGISTER<T>(val id: Int, val name: String, val response: SendChannel<T>? = null) : ActorCommand<T>()
   class REMOVE<T>(val id: Int, val response: SendChannel<T>? = null) : ActorCommand<T>()
   class QUERY<T>(val response: SendChannel<T>) : ActorCommand<T>()
}
val actorJob = actor<ActorCommand<Any?>> {
   val state = mutableMapOf<Int, String>()
   for (msg in channel) {
       when (msg) {
// define uma função para criar um future
// exemplo de uso:
// val stringFuture = future { “yay” }
// println(stringFuture.get())
fun <T> future(context: CoroutineContext = CommonPool, block: suspend () -> T): CompletableFuture<T> =
CompletableFutureCoroutine<T>(context).also {
block.startCoroutine(completion = it)
}
class CompletableFutureCoroutine<T>(override val context: CoroutineContext) : CompletableFuture<T>(), Continuation<T> {