Skip to content

Instantly share code, notes, and snippets.

@ncomet
ncomet / FlowToLiveChannel.kt
Last active December 18, 2023 09:13
LiveAsync
package org.example
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.selects.onTimeout
import kotlinx.coroutines.selects.select
@ncomet
ncomet / SyncChannel.kt
Last active December 18, 2023 09:32
2 flows sync on channel
package org.example
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.selects.onTimeout
import kotlinx.coroutines.selects.select
@ncomet
ncomet / parallel.kt
Created December 15, 2023 14:13
Experiments with coroutines and flows
package com.betclic.poker.gameserver.domain
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.selects.onTimeout
import kotlinx.coroutines.selects.select
sealed interface Outcome {
data class Ok(val result: String) : Outcome
@ncomet
ncomet / testcontainers_test.go
Last active May 9, 2022 11:57
TestMain final form
func TestMain(m *testing.M) {
var code = 1
defer func() { os.Exit(code) }()
ctx := context.Background()
mongoContainer, err := setup(ctx, mongoDB)
if err != nil {
log.Printf("Unexpected error, fallback to mem repository implementations.\nerror: %s.\n", err)
allGames = mem.NewAllGames()
} else {
func setup(ctx context.Context, image image) (*container, error) {
cont, uri, err := prepareContainer(ctx, image)
if err != nil {
return nil, err
}
return &container{Container: cont, URI: uri}, nil
}
@ncomet
ncomet / testcontainers_test.go
Created May 2, 2022 14:55
Container preparation
func prepareContainer(ctx context.Context, image image) (testcontainers.Container, string, error) {
req := testcontainers.ContainerRequest{
Image: image.name,
ExposedPorts: []string{image.port + "/tcp"},
WaitingFor: wait.ForListeningPort(nat.Port(image.port)),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
@ncomet
ncomet / testcontainers_test.go
Created May 2, 2022 14:51
core testcontainers logic
type image struct {
name string
port string
}
var (
allGames domain.AllGames
mongoDB = image{
name: "mongo:5.0.8",
@ncomet
ncomet / testmain_test.go
Last active May 2, 2022 12:59
TestMain
func TestMain(m *testing.M) {
log.Println("before all package tests")
exitCode := m.Run()
log.Println("after all package tests")
os.Exit(exitCode)
}
@ncomet
ncomet / all_games_test.go
Last active May 2, 2022 12:48
AllGames tests
...
func Test_Add(t *testing.T) {
gameId := domain.GameId(uuid.NewString())
allGames.Add(&domain.Game{
Id: gameId,
Title: "Assassin's Creed Valhalla",
PEGI: domain.Eighteen,
})
@ncomet
ncomet / games.go
Created May 2, 2022 12:43
Games Repository
package domain
type PEGI int
type GameId string
const (
Three PEGI = iota
Seven
Twelve
Sixteen