mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-25 07:38:57 +00:00
37 lines
583 B
Go
37 lines
583 B
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
package bank_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"gopl.io/ch9/bank1"
|
||
|
)
|
||
|
|
||
|
func TestBank(t *testing.T) {
|
||
|
done := make(chan struct{})
|
||
|
|
||
|
// Alice
|
||
|
go func() {
|
||
|
bank.Deposit(200)
|
||
|
fmt.Println("=", bank.Balance())
|
||
|
done <- struct{}{}
|
||
|
}()
|
||
|
|
||
|
// Bob
|
||
|
go func() {
|
||
|
bank.Deposit(100)
|
||
|
done <- struct{}{}
|
||
|
}()
|
||
|
|
||
|
// Wait for both transactions.
|
||
|
<-done
|
||
|
<-done
|
||
|
|
||
|
if got, want := bank.Balance(), 300; got != want {
|
||
|
t.Errorf("Balance = %d, want %d", got, want)
|
||
|
}
|
||
|
}
|