From c872a837ebbc180f09a471d2cb54c1cb43561243 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 11:31:36 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20SnowflakeIdGenerateService=E3=82=92?= =?UTF-8?q?=E6=94=B9=E8=89=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../other/SnowflakeIdGenerateService.kt | 13 ++++++----- .../TwitterSnowflakeIdGenerateServiceTest.kt | 22 +++++++------------ 2 files changed, 15 insertions(+), 20 deletions(-) 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)