diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt index 3b82b1cc..26f3564b 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt @@ -24,9 +24,9 @@ import java.time.Instant @Suppress("MagicNumber") open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateService { - var lastTimeStamp: Long = -1 - var sequenceId: Int = 0 - val mutex = Mutex() + private var lastTimeStamp: Long = -1 + private var sequenceId: Long = 0 + private val mutex = Mutex() @Throws(IllegalStateException::class) override suspend fun generateId(): Long { @@ -34,7 +34,6 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe var timestamp = getTime() if (timestamp < lastTimeStamp) { timestamp = wait(timestamp) - // throw IllegalStateException(" $lastTimeStamp $timestamp ${lastTimeStamp-timestamp} ") } if (timestamp == lastTimeStamp) { sequenceId++ @@ -46,7 +45,7 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe sequenceId = 0 } lastTimeStamp = timestamp - return@withLock (timestamp - baseTime).shl(22).or(1L.shl(12)).or(sequenceId.toLong()) + return@withLock (timestamp - baseTime).shl(22).or(1L.shl(12)).or(sequenceId) } } @@ -77,8 +76,10 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe override fun hashCode(): Int { var result = baseTime.hashCode() result = 31 * result + lastTimeStamp.hashCode() - result = 31 * result + sequenceId + result = 31 * result + sequenceId.hashCode() result = 31 * result + mutex.hashCode() return result } + + } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt index 98855ca8..0c7cb432 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt @@ -1,28 +1,22 @@ package dev.usbharu.hideout.core.infrastructure.other -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test class TwitterSnowflakeIdGenerateServiceTest { @Test fun noDuplicateTest() = runBlocking { - val mutex = Mutex() - val mutableListOf = mutableListOf() - coroutineScope { - repeat(500000) { - launch(Dispatchers.IO) { - val id = TwitterSnowflakeIdGenerateService.generateId() - mutex.withLock { - mutableListOf.add(id) - } + + val mutableListOf = coroutineScope { + (1..10000).map { + async { + TwitterSnowflakeIdGenerateService.generateId() } - } + }.awaitAll() } assertEquals(0, mutableListOf.size - mutableListOf.toSet().size)