1
exercism fetch scala bank-account

src/test/scala/BankAccountTest.scala

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
74
import org.scalatest.concurrent.{IntegrationPatience, Conductors}
import org.scalatest.{Matchers, FunSuite}

/** @version created manually **/
class BankAccountTest extends FunSuite with Matchers with Conductors with IntegrationPatience {
  test("open account") {
    Bank.openAccount().getBalance should be (Some(0))
  }

  test("incrementing and checking balance") {
    pending
    val acct = Bank.openAccount()
    acct.getBalance should be (Some(0))
    acct.incrementBalance(10) should be (Some(10))
    acct.getBalance should be (Some(10))
    acct.incrementBalance(10) should be (Some(20))
    acct.getBalance should be (Some(20))
  }

  test("closed account should hold no balance") {
    pending
    val acct = Bank.openAccount()
    acct.closeAccount()
    acct.incrementBalance(10)
    acct.incrementBalance(10)
    acct.getBalance should be (None)
  }

  test("incrementing balance from multiple threads") {
    pending
    val conductor = new Conductor
    import conductor._

    val acct = Bank.openAccount()

    thread("t1") {
      acct.incrementBalance(10)
      acct.getBalance should be (Some(10))
      beat should be (1)
      waitForBeat(2)
      acct.getBalance should be (Some(15))
    }

    thread("t2") {
      waitForBeat(1)
      acct.getBalance should be (Some(10))
      acct.incrementBalance(5)
      acct.getBalance should be (Some(15))
      beat should be (2)
    }
  }

  test("incrementing balance from multiple threads - concurrent updates") {
    pending
    val conductor = new Conductor
    import conductor._

    val acct = Bank.openAccount()

    thread("t1") {
      for (a <- 1 to 10)
        acct.incrementBalance(10)
    }

    thread("t2") {
      for (a <- 1 to 10)
        acct.incrementBalance(5)
    }

    whenFinished {
      acct.getBalance should be (Some(150))
    }
  }
}