-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test : Lock,Cache Manager 테스트 코드 작성(#132)
- Loading branch information
1 parent
086fac1
commit 030f924
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Support-Module/cache/src/test/kotlin/com/bamyanggang/cache/CacheManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.bamyanggang.cache | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class CacheManagerTest { | ||
|
||
@Test | ||
fun testCacheByKey() { | ||
// Given | ||
val key = "testKey" | ||
val body = { | ||
"body" | ||
} | ||
// When | ||
val result = CacheManager.cacheByKey(key, body = body) | ||
|
||
// Then | ||
assertEquals("body", result) | ||
} | ||
|
||
@Test | ||
fun testCacheByKeyWithSameKey() { | ||
// Given | ||
val key = "testKey" | ||
val body = { | ||
"body" | ||
} | ||
// When | ||
val result1 = CacheManager.cacheByKey(key, body = body) | ||
val result2 = CacheManager.cacheByKey(key, body = body) | ||
|
||
// Then | ||
assertEquals("body", result1) | ||
assertEquals("body", result2) | ||
} | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
Support-Module/lock/src/test/kotlin/com/bamyanggang/lock/LockManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.bamyanggang.lock | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
class LockManagerTest { | ||
|
||
@Test | ||
fun testLockByKey() { | ||
// Given | ||
val key = "key" | ||
val body = { | ||
Thread.sleep(10) | ||
"body" | ||
} | ||
|
||
// When | ||
val result = LockManager.lockByKey(key, body = body) | ||
|
||
// Then | ||
assert(result == "body") | ||
} | ||
|
||
|
||
@Test | ||
fun testLockByKeyTimeout() { | ||
// Given | ||
val key = "key" | ||
val time = 1L | ||
val body = { | ||
Thread.sleep(2000) | ||
"body" | ||
} | ||
|
||
val countDownLatch = CountDownLatch(2) | ||
var isTimeout = false | ||
// When | ||
for (i in 0..1) { | ||
|
||
Thread { | ||
try { | ||
LockManager.lockByKey(key, time, body) | ||
} catch (e: RuntimeException) { | ||
isTimeout = true | ||
} finally { | ||
countDownLatch.countDown() | ||
} | ||
}.start() | ||
} | ||
countDownLatch.await() | ||
|
||
// Then | ||
isTimeout shouldBe true | ||
} | ||
|
||
@Test | ||
fun testLockConcurrent() { | ||
// Given | ||
var count = 0 | ||
val key = "key" | ||
val body = { | ||
val currentCount = count | ||
Thread.sleep(1) | ||
count = currentCount + 1 | ||
} | ||
|
||
val countDown = 100 | ||
|
||
val countDownLatch = CountDownLatch(countDown) | ||
|
||
// When | ||
for (i in 0 until countDown) { | ||
Thread { | ||
LockManager.lockByKey(key, body = body) | ||
countDownLatch.countDown() | ||
}.start() | ||
} | ||
|
||
countDownLatch.await() | ||
|
||
// Then | ||
count shouldBe 100 | ||
} | ||
|
||
@Test | ||
fun testLockIndependence() { | ||
// Given | ||
val key1 = "key1" | ||
val key2 = "key2" | ||
val body = { Thread.sleep(1000) } | ||
val countDownLatch = CountDownLatch(2) | ||
|
||
// When | ||
Thread { | ||
LockManager.lockByKey(key1, body = body) | ||
countDownLatch.countDown() | ||
}.start() | ||
|
||
Thread { | ||
LockManager.lockByKey(key2, body = body) | ||
countDownLatch.countDown() | ||
}.start() | ||
|
||
// Then | ||
countDownLatch.await(2, TimeUnit.SECONDS) shouldBe true | ||
} | ||
|
||
@Test | ||
fun testLockWait() { | ||
// Given | ||
val key = "key" | ||
val body = { Thread.sleep(1000) } | ||
val countDownLatch = CountDownLatch(2) | ||
|
||
// When | ||
Thread { | ||
LockManager.lockByKey(key, body = body) | ||
countDownLatch.countDown() | ||
}.start() | ||
|
||
Thread { | ||
LockManager.lockByKey(key, body = body) | ||
countDownLatch.countDown() | ||
}.start() | ||
|
||
// Then | ||
countDownLatch.await(3, TimeUnit.SECONDS) shouldBe true | ||
} | ||
|
||
} |