-
Notifications
You must be signed in to change notification settings - Fork 176
/
memo_test.go
53 lines (47 loc) · 951 Bytes
/
memo_test.go
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
package memo
import (
"fmt"
"sync"
"testing"
)
func TestCancel(t *testing.T) {
cancelled := fmt.Errorf("cancelled")
finish := make(chan string)
f := func(key string, done <-chan struct{}) (interface{}, error) {
if done == nil {
res := <-finish
return res, nil
} else {
<-done
return nil, cancelled
}
}
m := New(Func(f))
key := "key"
done := make(chan struct{})
wg1 := &sync.WaitGroup{}
wg1.Add(1)
go func() {
v, err := m.Get(key, done)
wg1.Done()
if v != nil || err != cancelled {
t.Errorf("got %v, %v; want %v, %v", v, err, nil, cancelled)
}
}()
close(done)
// Wait for the cancellation to be processed before making the next request
// for `key`.
wg1.Wait()
wg2 := &sync.WaitGroup{}
wg2.Add(1)
go func() {
v, err := m.Get(key, nil)
s := v.(string)
if s != "ok" || err != nil {
t.Errorf("got %v, %v; want %v, %v", s, err, "ok", nil)
}
wg2.Done()
}()
finish <- "ok"
wg2.Wait()
}