Skip to content

Commit 24d9db1

Browse files
committed
binder: add a decorator that can queue txns for manual delivery later
1 parent 353f33d commit 24d9db1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

binder/src/testFixtures/java/io/grpc/binder/internal/OneWayBinderProxies.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,49 @@ public void transact(int code, ParcelHolder data) throws RemoteException {
119119
}
120120
}
121121

122+
/** A {@link OneWayBinderProxy} that queues transactions for a test to deliver manually later. */
123+
public static final class QueueingOneWayBinderProxy extends OneWayBinderProxy {
124+
public static final class Transaction {
125+
public final int code;
126+
private final ParcelHolder parcel;
127+
128+
public Transaction(int code, ParcelHolder parcel) {
129+
this.code = code;
130+
this.parcel = parcel;
131+
}
132+
}
133+
134+
private final BlockingQueue<Transaction> queue = new LinkedBlockingQueue<>();
135+
private final OneWayBinderProxy wrapped;
136+
137+
public QueueingOneWayBinderProxy(OneWayBinderProxy wrapped) {
138+
super(wrapped.getDelegate());
139+
this.wrapped = wrapped;
140+
}
141+
142+
@Override
143+
public void transact(int code, ParcelHolder data) throws RemoteException {
144+
queue.add(new Transaction(code, new ParcelHolder(data.release())));
145+
}
146+
147+
/**
148+
* Returns the next transaction that was queued in order, waiting up to the specified timeout.
149+
*/
150+
public Transaction pollNextTransaction(long timeout, TimeUnit unit)
151+
throws InterruptedException {
152+
return queue.poll(timeout, unit);
153+
}
154+
155+
/**
156+
* Delivers a previously queued transaction to its original destination.
157+
*
158+
* @throws IllegalStateException if transaction was already delivered once before
159+
*/
160+
public void deliver(Transaction transaction) throws RemoteException {
161+
wrapped.transact(transaction.code, transaction.parcel);
162+
}
163+
}
164+
122165
// Cannot be instantiated.
123166
private OneWayBinderProxies() {}
124167
;

0 commit comments

Comments
 (0)