mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-12 17:33:40 +00:00
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
package memo_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gopl.io/ch9/memo1"
|
|
"gopl.io/ch9/memotest"
|
|
)
|
|
|
|
var httpGetBody = memotest.HTTPGetBody
|
|
|
|
func Test(t *testing.T) {
|
|
m := memo.New(httpGetBody)
|
|
memotest.Sequential(t, m)
|
|
}
|
|
|
|
// NOTE: not concurrency-safe! Test fails.
|
|
func TestConcurrent(t *testing.T) {
|
|
m := memo.New(httpGetBody)
|
|
memotest.Concurrent(t, m)
|
|
}
|
|
|
|
/*
|
|
//!+output
|
|
$ go test -v gopl.io/ch9/memo1
|
|
=== RUN Test
|
|
https://golang.org, 175.026418ms, 7537 bytes
|
|
https://godoc.org, 172.686825ms, 6878 bytes
|
|
https://play.golang.org, 115.762377ms, 5767 bytes
|
|
http://gopl.io, 749.887242ms, 2856 bytes
|
|
|
|
https://golang.org, 721ns, 7537 bytes
|
|
https://godoc.org, 152ns, 6878 bytes
|
|
https://play.golang.org, 205ns, 5767 bytes
|
|
http://gopl.io, 326ns, 2856 bytes
|
|
--- PASS: Test (1.21s)
|
|
PASS
|
|
ok gopl.io/ch9/memo1 1.257s
|
|
//!-output
|
|
*/
|
|
|
|
/*
|
|
//!+race
|
|
$ go test -run=TestConcurrent -race -v gopl.io/ch9/memo1
|
|
=== RUN TestConcurrent
|
|
...
|
|
WARNING: DATA RACE
|
|
Write by goroutine 36:
|
|
runtime.mapassign1()
|
|
~/go/src/runtime/hashmap.go:411 +0x0
|
|
gopl.io/ch9/memo1.(*Memo).Get()
|
|
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
|
|
...
|
|
|
|
Previous write by goroutine 35:
|
|
runtime.mapassign1()
|
|
~/go/src/runtime/hashmap.go:411 +0x0
|
|
gopl.io/ch9/memo1.(*Memo).Get()
|
|
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
|
|
...
|
|
Found 1 data race(s)
|
|
FAIL gopl.io/ch9/memo1 2.393s
|
|
//!-race
|
|
*/
|