-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
answer_test.go
139 lines (125 loc) · 3.65 KB
/
answer_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package capnp
import (
"context"
"errors"
"strings"
"testing"
)
var dummyMethod = Method{
InterfaceID: 0xa7317bd7216570aa,
InterfaceName: "Foo",
MethodID: 9,
MethodName: "bar",
}
func TestPromiseReject(t *testing.T) {
t.Run("Done", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
done := p.Answer().Done()
p.Reject(errors.New("omg bbq"))
select {
case <-done:
p.ReleaseClients()
default:
t.Error("answer not resolved")
}
})
t.Run("Struct", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
defer p.ReleaseClients()
ans := p.Answer()
p.Reject(errors.New("omg bbq"))
if _, err := ans.Struct(); err == nil || !strings.Contains(err.Error(), "omg bbq") || !strings.Contains(err.Error(), "Foo.bar") {
t.Errorf("answer error = %v; want message containing \"omg bbq\" and \"Foo.bar\"", err)
}
})
t.Run("Client", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
defer p.ReleaseClients()
pc := p.Answer().Field(1, nil).Client()
p.Reject(errors.New("omg bbq"))
ctx := context.Background()
if err := pc.Resolve(ctx); err != nil {
t.Error("pc.Resolve:", err)
}
ans, release := pc.SendCall(ctx, Send{})
_, err := ans.Struct()
release()
if err == nil || !strings.Contains(err.Error(), "omg bbq") || !strings.Contains(err.Error(), "Foo.bar") {
t.Errorf("pc.SendCall error = %v; want message containing \"omg bbq\"", err)
}
})
}
func TestPromiseFulfill(t *testing.T) {
t.Parallel()
t.Run("Done", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
done := p.Answer().Done()
msg, seg, _ := NewMessage(SingleSegment(nil))
defer msg.Release()
res, _ := NewStruct(seg, ObjectSize{DataSize: 8})
p.Fulfill(res.ToPtr())
select {
case <-done:
p.ReleaseClients()
default:
t.Error("answer not resolved")
}
})
t.Run("Struct", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
defer p.ReleaseClients()
ans := p.Answer()
msg, seg, _ := NewMessage(SingleSegment(nil))
defer msg.Release()
res, _ := NewStruct(seg, ObjectSize{DataSize: 8})
res.SetUint32(0, 0xdeadbeef)
p.Fulfill(res.ToPtr())
s, err := ans.Struct()
if eq, err := Equal(res.ToPtr(), s.ToPtr()); err != nil {
t.Error("Equal(p.Answer.Struct(), res):", err)
} else if !eq {
t.Error("p.Answer().Struct() != res")
}
if err != nil {
t.Error("p.Answer().Struct():", err)
}
})
t.Run("Client", func(t *testing.T) {
p := NewPromise(dummyMethod, dummyPipelineCaller{}, nil)
defer p.ReleaseClients()
pc := p.Answer().Field(1, nil).Client()
h := new(dummyHook)
c := NewClient(h)
defer c.Release()
msg, seg, _ := NewMessage(SingleSegment(nil))
defer msg.Release()
res, _ := NewStruct(seg, ObjectSize{PointerCount: 3})
res.SetPtr(1, NewInterface(seg, msg.CapTable().Add(c.AddRef())).ToPtr())
p.Fulfill(res.ToPtr())
ctx := context.Background()
if err := pc.Resolve(ctx); err != nil {
t.Error("pc.Resolve:", err)
}
if !pc.IsSame(c) {
t.Errorf("pc != c; pc = %v, c = %v", pc, c)
}
c.Release()
ans, release := pc.SendCall(ctx, Send{})
_, err := ans.Struct()
release()
if err != nil {
t.Error("pc.SendCall:", err)
}
if h.calls == 0 {
t.Error("hook never called")
}
})
}
type dummyPipelineCaller struct{}
func (dummyPipelineCaller) PipelineRecv(ctx context.Context, transform []PipelineOp, r Recv) PipelineCaller {
r.Reject(errors.New("dummy call"))
return nil
}
func (dummyPipelineCaller) PipelineSend(ctx context.Context, transform []PipelineOp, s Send) (*Answer, ReleaseFunc) {
return ErrorAnswer(s.Method, errors.New("dummy call")), func() {}
}