2 Copyright 2012 Google Inc.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
28 func TestDo(t *testing.T) {
30 v, err := g.Do("key", func() (interface{}, error) {
33 if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
34 t.Errorf("Do = %v; want %v", got, want)
37 t.Errorf("Do error = %v", err)
41 func TestDoErr(t *testing.T) {
43 someErr := errors.New("Some error")
44 v, err := g.Do("key", func() (interface{}, error) {
48 t.Errorf("Do error = %v; want someErr", err)
51 t.Errorf("unexpected non-nil value %#v", v)
55 func TestDoDupSuppress(t *testing.T) {
57 c := make(chan string)
59 fn := func() (interface{}, error) {
60 atomic.AddInt32(&calls, 1)
66 for i := 0; i < n; i++ {
69 v, err := g.Do("key", fn)
71 t.Errorf("Do error: %v", err)
73 if v.(string) != "bar" {
74 t.Errorf("got %q; want %q", v, "bar")
79 time.Sleep(100 * time.Millisecond) // let goroutines above block
82 if got := atomic.LoadInt32(&calls); got != 1 {
83 t.Errorf("number of calls = %d; want 1", got)