Search in sources :

Example 1 with NetworkReceive

use of com.github.ambry.network.NetworkReceive 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");
    }
}
Also used : BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) ArrayList(java.util.ArrayList) NetworkReceive(com.github.ambry.network.NetworkReceive) NetworkSend(com.github.ambry.network.NetworkSend) IOException(java.io.IOException)

Aggregations

BoundedNettyByteBufReceive (com.github.ambry.network.BoundedNettyByteBufReceive)1 NetworkReceive (com.github.ambry.network.NetworkReceive)1 NetworkSend (com.github.ambry.network.NetworkSend)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1