9
9
"time"
10
10
11
11
"github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/require"
12
13
)
13
14
14
15
func TestClient (t * testing.T ) {
@@ -23,13 +24,14 @@ func TestClient(t *testing.T) {
23
24
if ! c .IsValid () {
24
25
t .Error ("new client is not valid" )
25
26
}
26
- state := c .State ()
27
- if state .IsPromise {
27
+ state := c .Snapshot ()
28
+ if state .IsPromise () {
28
29
t .Error ("c.State().IsPromise = true; want false" )
29
30
}
30
- if state .Brand .Value != int (42 ) {
31
- t .Errorf ("c.State().Brand.Value = %#v; want 42" , state .Brand .Value )
31
+ if state .Brand () .Value != int (42 ) {
32
+ t .Errorf ("c.State().Brand() .Value = %#v; want 42" , state .Brand () .Value )
32
33
}
34
+ state .Release ()
33
35
ans , finish := c .SendCall (ctx , Send {})
34
36
if _ , err := ans .Struct (); err != nil {
35
37
t .Error ("SendCall:" , err )
@@ -78,13 +80,14 @@ func TestReleasedClient(t *testing.T) {
78
80
if c .IsValid () {
79
81
t .Error ("released client is valid" )
80
82
}
81
- state := c .State ()
82
- if state .Brand .Value != nil {
83
- t .Errorf ("c.State ().Brand.Value = %#v; want <nil>" , state .Brand .Value )
83
+ state := c .Snapshot ()
84
+ if state .Brand () .Value != nil {
85
+ t .Errorf ("c.Snapshot ().Brand() .Value = %#v; want <nil>" , state .Brand () .Value )
84
86
}
85
- if state .IsPromise {
86
- t .Error ("c.State ().IsPromise = true; want false" )
87
+ if state .IsPromise () {
88
+ t .Error ("c.Snapshot ().IsPromise = true; want false" )
87
89
}
90
+ state .Release ()
88
91
ans , finish := c .SendCall (ctx , Send {})
89
92
if _ , err := ans .Struct (); err == nil {
90
93
t .Error ("SendCall did not return error" )
@@ -116,6 +119,49 @@ func TestReleasedClient(t *testing.T) {
116
119
t .Error ("second Release made more calls to ClientHook.Shutdown" )
117
120
}
118
121
}
122
+ func TestResolve (t * testing.T ) {
123
+ test := func (t * testing.T , name string , f func (t * testing.T , p1 , p2 Client , r1 , r2 Resolver [Client ])) {
124
+ t .Run (name , func (t * testing.T ) {
125
+ t .Parallel ()
126
+ p1 , r1 := NewLocalPromise [Client ]()
127
+ p2 , r2 := NewLocalPromise [Client ]()
128
+ defer p1 .Release ()
129
+ defer p2 .Release ()
130
+ f (t , p1 , p2 , r1 , r2 )
131
+ })
132
+ }
133
+ t .Run ("Clients" , func (t * testing.T ) {
134
+ test (t , "Waits for the full chain" , func (t * testing.T , p1 , p2 Client , r1 , r2 Resolver [Client ]) {
135
+ r1 .Fulfill (p2 )
136
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second / 10 )
137
+ defer cancel ()
138
+ require .NotNil (t , p1 .Resolve (ctx ), "blocks on second promise" )
139
+ r2 .Fulfill (Client {})
140
+ require .NoError (t , p1 .Resolve (context .Background ()), "resolves after second resolution" )
141
+ assert .True (t , p1 .IsSame (Client {}), "p1 resolves to null" )
142
+ assert .True (t , p2 .IsSame (Client {}), "p2 resolves to null" )
143
+ assert .True (t , p1 .IsSame (p2 ), "p1 & p2 are the same" )
144
+ })
145
+ })
146
+ t .Run ("Snapshots" , func (t * testing.T ) {
147
+ test (t , "Resolve1 only waits for one link" , func (t * testing.T , p1 , p2 Client , r1 , r2 Resolver [Client ]) {
148
+ s1 := p1 .Snapshot ()
149
+ defer s1 .Release ()
150
+ r1 .Fulfill (p2 )
151
+ require .NoError (t , s1 .Resolve1 (context .Background ()), "Resolve1 returns after first resolution" )
152
+ })
153
+ test (t , "Resolve waits for the full chain" , func (t * testing.T , p1 , p2 Client , r1 , r2 Resolver [Client ]) {
154
+ s1 := p1 .Snapshot ()
155
+ defer s1 .Release ()
156
+ r1 .Fulfill (p2 )
157
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second / 10 )
158
+ defer cancel ()
159
+ require .NotNil (t , s1 .Resolve (ctx ), "blocks on second promise" )
160
+ r2 .Fulfill (Client {})
161
+ require .NoError (t , s1 .Resolve (context .Background ()), "resolves after second resolution" )
162
+ })
163
+ })
164
+ }
119
165
120
166
func TestNullClient (t * testing.T ) {
121
167
ctx := context .Background ()
@@ -141,13 +187,14 @@ func TestNullClient(t *testing.T) {
141
187
if c .IsValid () {
142
188
t .Error ("null client is valid" )
143
189
}
144
- state := c .State ()
145
- if state .Brand .Value != nil {
146
- t .Errorf ("c.State ().Brand = %#v; want <nil>" , state .Brand )
190
+ state := c .Snapshot ()
191
+ if state .Brand () .Value != nil {
192
+ t .Errorf ("c.Snapshot ().Brand() = %#v; want <nil>" , state .Brand () )
147
193
}
148
- if state .IsPromise {
149
- t .Error ("c.State ().IsPromise = true; want false" )
194
+ if state .IsPromise () {
195
+ t .Error ("c.Snapshot ().IsPromise = true; want false" )
150
196
}
197
+ state .Release ()
151
198
ans , finish := c .SendCall (ctx , Send {})
152
199
if _ , err := ans .Struct (); err == nil {
153
200
t .Error ("SendCall did not return error" )
@@ -186,13 +233,14 @@ func TestPromisedClient(t *testing.T) {
186
233
if ca .IsSame (cb ) {
187
234
t .Error ("before resolution, ca == cb" )
188
235
}
189
- state := ca .State ()
190
- if state .Brand .Value != int (111 ) {
191
- t .Errorf ("before resolution, ca.State ().Brand.Value = %#v; want 111" , state .Brand .Value )
236
+ state := ca .Snapshot ()
237
+ if state .Brand () .Value != int (111 ) {
238
+ t .Errorf ("before resolution, ca.Snapshot ().Brand() .Value = %#v; want 111" , state .Brand () .Value )
192
239
}
193
- if ! state .IsPromise {
194
- t .Error ("before resolution, ca.State ().IsPromise = false; want true" )
240
+ if ! state .IsPromise () {
241
+ t .Error ("before resolution, ca.Snapshot ().IsPromise = false; want true" )
195
242
}
243
+ state .Release ()
196
244
_ , finish := ca .SendCall (ctx , Send {})
197
245
finish ()
198
246
pa .Fulfill (cb )
@@ -207,13 +255,14 @@ func TestPromisedClient(t *testing.T) {
207
255
if ! ca .IsSame (cb ) {
208
256
t .Errorf ("after resolution, ca != cb (%v vs. %v)" , ca , cb )
209
257
}
210
- state = ca .State ()
211
- if state .Brand .Value != int (222 ) {
212
- t .Errorf ("after resolution, ca.State ().Brand.Value = %#v; want 222" , state .Brand .Value )
258
+ state = ca .Snapshot ()
259
+ if state .Brand () .Value != int (222 ) {
260
+ t .Errorf ("after resolution, ca.Snapshot ().Brand() .Value = %#v; want 222" , state .Brand () .Value )
213
261
}
214
- if state .IsPromise {
215
- t .Error ("after resolution, ca.State ().IsPromise = true; want false" )
262
+ if state .IsPromise () {
263
+ t .Error ("after resolution, ca.Snapshot ().IsPromise = true; want false" )
216
264
}
265
+ state .Release ()
217
266
218
267
if b .shutdowns > 0 {
219
268
t .Error ("b shut down before clients released" )
0 commit comments