File tree 3 files changed +31
-22
lines changed
src/Symfony/Component/HttpClient
3 files changed +31
-22
lines changed Original file line number Diff line number Diff line change @@ -112,19 +112,17 @@ public function sendAsyncRequest(RequestInterface $request): Promise
112
112
return new RejectedPromise ($ e );
113
113
}
114
114
115
- $ cancel = function () use ($ response ) {
116
- $ response ->cancel ();
117
- unset($ this ->promisePool [$ response ]);
118
- };
119
-
120
115
$ promise = new GuzzlePromise (function () use ($ response ) {
121
116
$ this ->pendingResponse = $ response ;
122
117
$ this ->wait ();
123
- }, $ cancel );
118
+ }, function () use ($ response ) {
119
+ $ response ->cancel ();
120
+ unset($ this ->promisePool [$ response ]);
121
+ });
124
122
125
123
$ this ->promisePool [$ response ] = [$ request , $ promise ];
126
124
127
- return new HttplugPromise ($ promise, $ cancel );
125
+ return new HttplugPromise ($ promise );
128
126
}
129
127
130
128
/**
Original file line number Diff line number Diff line change 23
23
final class HttplugPromise implements HttplugPromiseInterface
24
24
{
25
25
private $ promise ;
26
- private $ cancel ;
27
26
28
- public function __construct (GuzzlePromiseInterface $ promise, callable $ cancel = null )
27
+ public function __construct (GuzzlePromiseInterface $ promise )
29
28
{
30
29
$ this ->promise = $ promise ;
31
- $ this ->cancel = $ cancel ;
32
30
}
33
31
34
32
public function then (callable $ onFulfilled = null , callable $ onRejected = null ): self
@@ -58,16 +56,4 @@ public function wait($unwrap = true)
58
56
{
59
57
return $ this ->promise ->wait ($ unwrap );
60
58
}
61
-
62
- public function __destruct ()
63
- {
64
- if ($ this ->cancel ) {
65
- ($ this ->cancel )();
66
- }
67
- }
68
-
69
- public function __wakeup ()
70
- {
71
- throw new \BadMethodCallException ('Cannot unserialize ' .__CLASS__ );
72
- }
73
59
}
Original file line number Diff line number Diff line change @@ -75,6 +75,31 @@ public function testSendAsyncRequest()
75
75
$ this ->assertSame ('HTTP/1.1 ' , $ body ['SERVER_PROTOCOL ' ]);
76
76
}
77
77
78
+ public function testWait ()
79
+ {
80
+ $ client = new HttplugClient (new NativeHttpClient ());
81
+
82
+ $ successCallableCalled = false ;
83
+ $ failureCallableCalled = false ;
84
+ $ client ->sendAsyncRequest ($ client ->createRequest ('GET ' , 'http://localhost:8057 ' ))
85
+ ->then (function (ResponseInterface $ response ) use (&$ successCallableCalled ) {
86
+ $ successCallableCalled = true ;
87
+
88
+ return $ response ;
89
+ }, function (\Exception $ exception ) use (&$ failureCallableCalled ) {
90
+ $ failureCallableCalled = true ;
91
+
92
+ throw $ exception ;
93
+ });
94
+
95
+ $ client ->wait (0 );
96
+ $ this ->assertFalse ($ successCallableCalled , '$promise->then() should not be called yet. ' );
97
+
98
+ $ client ->wait ();
99
+ $ this ->assertTrue ($ successCallableCalled , '$promise->then() should have been called. ' );
100
+ $ this ->assertFalse ($ failureCallableCalled , 'Failure callable should not be called when request is successful. ' );
101
+ }
102
+
78
103
public function testPostRequest ()
79
104
{
80
105
$ client = new HttplugClient (new NativeHttpClient ());
You can’t perform that action at this time.
0 commit comments