-
Notifications
You must be signed in to change notification settings - Fork 11
/
pipeline_test.go
89 lines (72 loc) · 1.5 KB
/
pipeline_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dns
import (
"context"
"errors"
"net"
"testing"
"time"
)
func TestPipeline(t *testing.T) {
stallc := make(chan struct{})
errc := make(chan error, 1)
srv := mustServer(HandlerFunc(func(ctx context.Context, w MessageWriter, r *Query) {
switch r.ID {
case 1:
<-stallc
w.Answer("AAAA.dev.", time.Minute, answers[questions["AAAA"]])
case 2:
defer close(stallc)
w.Answer("A.dev.", time.Minute, answers[questions["A"]])
default:
w.Status(NXDomain)
errc <- errors.New("bad message ID")
}
}))
addr, err := net.ResolveTCPAddr("tcp", srv.Addr)
if err != nil {
t.Fatal(err)
}
tport := new(Transport)
conn1, err := tport.DialAddr(context.Background(), addr)
if err != nil {
t.Fatal(err)
}
msg := &Message{
ID: 1,
Questions: []Question{questions["A"]},
}
if err := conn1.Send(msg); err != nil {
errc <- err
}
go func() {
defer close(errc)
var msg Message
if err := conn1.Recv(&msg); err != nil {
errc <- err
}
if msg.ID != 1 {
errc <- errors.New("message ID mismatch")
}
}()
conn2, err := tport.DialAddr(context.Background(), addr)
if err != nil {
t.Fatal(err)
}
msg = &Message{
ID: 2,
Questions: []Question{questions["AAAA"]},
}
if err := conn2.Send(msg); err != nil {
errc <- err
}
msg = new(Message)
if err := conn2.Recv(msg); err != nil {
t.Fatal(err)
}
if want, got := 2, msg.ID; want != got {
t.Errorf("want response message ID %d, got %d", want, got)
}
if err := <-errc; err != nil {
t.Fatal(err)
}
}