1
exercism fetch kotlin bank-account

src/test/kotlin/BankAccountTest.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import org.junit.Ignore
import org.junit.Test
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.coroutines.experimental.*

class BankAccountTest {

    @Test
    fun zeroBalanceWhenOpened() {
        val account = BankAccount()
        assertEquals(0, account.balance)
    }

    @Ignore
    @Test
    fun sequentialBalanceAdjustments() {
        val account = BankAccount()

        account.adjustBalance(1000)
        assertEquals(1000, account.balance)

        account.adjustBalance(-958)
        assertEquals(42, account.balance)
    }

    @Ignore
    @Test
    fun closedAccountHasNoBalance() {
        val account = BankAccount()
        account.close()

        assertFailsWith(IllegalStateException::class, { account.balance })
    }

    @Ignore
    @Test
    fun closedAccountCannotBeAdjusted() {
        val account = BankAccount()
        account.close()

        assertFailsWith(IllegalStateException::class, { account.adjustBalance(1000) })
    }

    @Ignore
    @Test
    fun concurrentBalanceAdjustments() {
        val threads = 1000
        val iterations = 500
        val random = Random()

        val account = BankAccount()

        val jobs = List(threads) {
            launch(CommonPool) {
                repeat(iterations) {
                    account.adjustBalance(1)
                    delay(random.nextInt(10).toLong())
                    account.adjustBalance(-1)
                }
            }
        }

        runBlocking {
            jobs.forEach { it.join() }
        }

        assertEquals(0, account.balance)
    }

}