use of com.github.ambry.network.NetworkSend in project ambry by linkedin.
the class MockSelector method poll.
/**
* Mocks sending and polling. Calls into the {@link MockServer} associated with the connection id with the request
* and uses the response from the call to construct receives. If the state is not {@link MockSelectorState#Good},
* then the behavior will be as determined by the state.
* @param timeoutMs Ignored.
* @param sends The list of new sends.
*/
@Override
public void poll(long timeoutMs, List<NetworkSend> sends) throws IOException {
this.sends = sends == null ? null : new ArrayList<>(sends);
disconnected.clear();
if (state.get() == MockSelectorState.FailConnectionInitiationOnPoll) {
disconnected.addAll(connected);
connected.clear();
}
if (sends != null) {
for (NetworkSend send : sends) {
if (state.get() == MockSelectorState.ThrowExceptionOnSend) {
throw new IOException("Mock exception on send");
}
if (state.get() == MockSelectorState.ThrowThrowableOnSend) {
throw new Error("Mock throwable on send");
}
if (state.get() == MockSelectorState.DisconnectOnSend) {
disconnected.add(send.getConnectionId());
this.sends.remove(send);
} else {
MockServer server = connIdToServer.get(send.getConnectionId());
BoundedNettyByteBufReceive receive = server.send(send.getPayload());
if (receive != null) {
receives.add(new NetworkReceive(send.getConnectionId(), receive, time));
}
// Just like Selector, MockSelector needs to release the sends' resources after completed.
send.getPayload().release();
}
}
}
if (state.get() == MockSelectorState.ThrowExceptionOnAllPoll) {
throw new IOException("Mock exception on poll");
}
}
Aggregations