/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://glassfish.dev.java.net/public/CDDLv1.0.html or
* glassfish/bootstrap/legal/CDDLv1.0.txt.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at glassfish/bootstrap/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
*/
package com.sun.grizzly;
import com.sun.grizzly.filter.EchoFilter;
import com.sun.grizzly.filter.ReadFilter;
import com.sun.grizzly.utils.ControllerUtils;
import com.sun.grizzly.utils.TCPIOClient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
/**
* Tests a BaseSelectionKeyHandler configuration, (no keep alive).
*
* @author charlie hunt
*/
public class BaseSelectionKeyHandlerTest extends TestCase {
private static final int NUMBER_OF_ITERATIONS = 2;
private static final int KEEP_ALIVE_TIMEOUT = 35 * 1000;
private static final int PORT = 18888;
public void testSimplePacket() throws IOException {
// set up server side
final Controller controller = new Controller();
final TCPSelectorHandler selectorHandler = new TCPSelectorHandler();
selectorHandler.setPort(PORT);
selectorHandler.setSelectionKeyHandler(new BaseSelectionKeyHandler());
controller.addSelectorHandler(selectorHandler);
final ProtocolChainInstanceHandler pciHandler =
new ProtocolChainInstanceHandler() {
final private ProtocolChain protocolChain =
new DefaultProtocolChain();
public ProtocolChain poll() {
return protocolChain;
}
public boolean offer(ProtocolChain pc) {
return true;
}
};
controller.setProtocolChainInstanceHandler(pciHandler);
pciHandler.poll().addFilter(new ReadFilter());
pciHandler.poll().addFilter(new EchoFilter());
controller.setPipeline(new DefaultPipeline());
controller.getPipeline().setMaxThreads(5);
// set up client side, start controller & run test
List clients = null;
try {
final byte[] testData = makeDataToSend();
ControllerUtils.startController(controller);
clients = initializeClients("localhost", PORT, 2);
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
for (TCPIOClient client : clients) {
client.send(testData);
byte[] response = new byte[testData.length];
client.receive(response);
assertTrue(Arrays.equals(testData, response));
}
}
Controller.logger().log(Level.INFO,
"Checking if keep alive is active ...");
Controller.logger().log(Level.INFO, "Sorry, have to wait for (" +
KEEP_ALIVE_TIMEOUT/1000 + ") " + "seconds ...");
Thread.sleep(KEEP_ALIVE_TIMEOUT);
Controller.logger().log(Level.INFO,
"Clients attempting to send again ...");
// Clients should still be connected, if not, then we have problem.
try {
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
for (TCPIOClient client : clients) {
client.send(testData);
byte[] response = new byte[testData.length];
client.receive(response);
assertTrue(Arrays.equals(testData, response));
}
}
Controller.logger().log(Level.INFO,
"Keep alive not active as expected ...");
assertTrue("Keep alive not active as expected ...", true);
} catch (IOException ex) {
assertTrue("Client reporting unexpected error: " +
ex.toString(), false);
}
} catch (InterruptedException ex) {
Logger.getLogger(BaseSelectionKeyHandlerTest.class.getName()).
log(Level.SEVERE, null, ex);
} finally {
if (clients != null) {
closeClients(clients);
}
controller.stop();
}
}
private List initializeClients(String host, int port,
int count) throws IOException {
final List clients = new ArrayList(count);
for(int i=0; i clients) {
for(TCPIOClient client : clients) {
try {
client.close();
} catch (IOException ex) {
Logger.getLogger(BaseSelectionKeyHandlerTest.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
private byte[] makeDataToSend() {
final int TARGET_MSG_FACTOR = 12;
// total message length = TARGET_MSG_SIZE x msg.length x # of bytes per char
final String msg = "Hello Grizzly, How is the bear today? ";
final StringBuilder sb = new StringBuilder(msg.length()*TARGET_MSG_FACTOR );
for (int i = 0; i < TARGET_MSG_FACTOR ; i++) {
sb.append(msg);
}
return sb.toString().getBytes();
}
}