-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcurrency_test.go
60 lines (54 loc) · 957 Bytes
/
concurrency_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
54
55
56
57
58
59
60
package signer_test
import (
"fmt"
"testing"
"time"
"github.com/as/signer"
)
// run with 'go test -race .'
func TestConcurrentSignVerify(t *testing.T) {
const (
N = 64
T = 3* time.Second
)
s, err := signer.New([]byte("0123456789abcdef0123456789abcdef"))
if err != nil {
t.Fatal(err)
}
done, errc := make(chan bool), make(chan error)
defer close(done)
for i := 0; i < N; i++ {
go signVerify(s, done, errc)
}
select {
case err := <-errc:
t.Fatal(err)
case <-time.After(T):
}
}
func signVerify(s *signer.Signer, done chan bool, errc chan error) {
const input = "hello world"
for {
select {
case <-done:
return
default:
}
tok, err := s.Sign([]byte(input), nil)
if err != nil {
errc <- err
return
}
p, err := s.Verify(tok)
if err != nil {
errc <- err
return
}
if string(p) != input {
if err != nil {
errc <- fmt.Errorf("have %q, want %q", string(p), input)
return
}
}
}
}