-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryTransport.php
More file actions
140 lines (125 loc) · 3.7 KB
/
MemoryTransport.php
File metadata and controls
140 lines (125 loc) · 3.7 KB
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
140
<?php
declare(strict_types=1);
namespace Arcp\Transport;
use function Amp\async;
use Amp\Cancellation;
use Amp\DeferredFuture;
use Amp\Future;
use Arcp\Envelope\Envelope;
/**
* In-process loopback transport. A single call to {@see MemoryTransport::pair()}
* returns the two endpoints of a bidirectional channel; what one writes,
* the other reads, in FIFO order. Used by every integration test that
* doesn't need a real socket.
*/
final class MemoryTransport implements Transport
{
/** @var \SplQueue<Envelope> */
private \SplQueue $inbox;
/** @var list<DeferredFuture<Envelope|null>> */
private array $waiters = [];
private bool $closed = false;
private ?MemoryTransport $peer = null;
private function __construct()
{
/** @var \SplQueue<Envelope> $q */
$q = new \SplQueue();
$this->inbox = $q;
}
/**
* @return array{0: MemoryTransport, 1: MemoryTransport}
*/
public static function pair(): array
{
$a = new self();
$b = new self();
$a->peer = $b;
$b->peer = $a;
return [$a, $b];
}
#[\Override]
public function send(Envelope $env, ?Cancellation $cancellation = null): void
{
if ($this->closed) {
throw new \RuntimeException('memory transport closed');
}
$peer = $this->peer ?? throw new \RuntimeException('memory transport unpaired');
$peer->deliver($env);
}
private function deliver(Envelope $env): void
{
if ($this->waiters !== []) {
$waiter = array_shift($this->waiters);
$waiter->complete($env);
return;
}
$this->inbox->enqueue($env);
}
#[\Override]
public function receive(?Cancellation $cancellation = null): ?Envelope
{
if (!$this->inbox->isEmpty()) {
return $this->inbox->dequeue();
}
if ($this->closed) {
return null;
}
/** @var DeferredFuture<Envelope|null> $deferred */
$deferred = new DeferredFuture();
$this->waiters[] = $deferred;
try {
/** @var Envelope|null $result */
$result = $deferred->getFuture()->await($cancellation);
return $result;
} catch (\Throwable $e) {
$kept = [];
foreach ($this->waiters as $w) {
if ($w !== $deferred) {
$kept[] = $w;
}
}
$this->waiters = $kept;
throw $e;
}
}
/**
* Receive in the background; useful for setting up two-sided test scenarios.
*
* @return Future<Envelope|null>
*/
public function receiveAsync(?Cancellation $cancellation = null): Future
{
/** @var Future<Envelope|null> $future */
$future = async(fn (): ?Envelope => $this->receive($cancellation));
return $future;
}
#[\Override]
public function close(): void
{
if ($this->closed) {
return;
}
$this->closed = true;
foreach ($this->waiters as $w) {
$w->complete(null);
}
$this->waiters = [];
if ($this->peer !== null && !$this->peer->closed) {
// Closing one end naturally signals the other on next receive().
$peer = $this->peer;
$this->peer = null;
$peer->peer = null;
// Wake any waiters on the peer with null so reads return a clean EOF.
foreach ($peer->waiters as $w) {
$w->complete(null);
}
$peer->waiters = [];
$peer->closed = true;
}
}
#[\Override]
public function isClosed(): bool
{
return $this->closed;
}
}