# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: C:\Projects\griz-last\trunk\modules\grizzly\src\test\java\com\sun\grizzly # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: ContextShareTest.java --- ContextShareTest.java Locally New +++ ContextShareTest.java Locally New @@ -0,0 +1,188 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, 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/CDDL+GPL.html + * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + * + */ + +package com.sun.grizzly; + +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.concurrent.CountDownLatch; +import junit.framework.TestCase; + +/** + * + * @author John Vieten + */ +public class ContextShareTest extends TestCase { + + public static final int PORT = 17502; + + + public void testContextStillValid() throws IOException { + final ProtocolFilter threadFilter = new ProtocolFilter() { + + public boolean execute(final Context ctx) throws IOException { + final CountDownLatch latch = new CountDownLatch(1); + + ctx.incrementRefCount(); + + new Thread() { + + @Override + public void run() { + + ctx.getController().returnContext(ctx); + //check ctx still valid + assertNotNull(ctx.getSelectionKey()); + latch.countDown(); + } + }.start(); + try { + latch.await(); + } catch (Exception e) { + } + + return false; + + } + + public boolean postExecute(Context ctx) throws IOException { + return true; + } + }; + + Controller controller = createController(PORT, threadFilter); + + try { + ControllerUtils.startController(controller); + sendSomething(); + sleep(2000); + } finally { + controller.stop(); + } + } + + public void testContextRecyled() throws IOException { + final ProtocolFilter threadFilter = new ProtocolFilter() { + + public boolean execute(final Context ctx) throws IOException { + ctx.incrementRefCount(); + + new Thread() { + + @Override + public void run() { + // wait till workerthread decrements ref count + + + ContextShareTest.this.sleep(1500); + + + ctx.getController().returnContext(ctx); + } + }.start(); + + return false; + + } + + public boolean postExecute(Context ctx) throws IOException { + return true; + } + }; + + Controller controller = createController(PORT, threadFilter); + + try { + ControllerUtils.startController(controller); + sendSomething(); + sleep(2000); + } finally { + controller.stop(); + } + } + + + private Controller createController(int port, final ProtocolFilter threadFilter) { + final ProtocolFilter readFilter = new ReadFilter(); + TCPSelectorHandler selectorHandler = new TCPSelectorHandler(); + selectorHandler.setPort(port); + + final Controller controller = new Controller(); + + controller.setSelectorHandler(selectorHandler); + + controller.setProtocolChainInstanceHandler( + new DefaultProtocolChainInstanceHandler() { + + @Override + public ProtocolChain poll() { + ProtocolChain protocolChain = protocolChains.poll(); + if (protocolChain == null) { + protocolChain = new DefaultProtocolChain(); + protocolChain.addFilter(readFilter); + protocolChain.addFilter(threadFilter); + } + return protocolChain; + } + }); + + return controller; + } + + private void sendSomething() throws IOException { + TCPIOClient client = new TCPIOClient("localhost", PORT); + try { + client.connect(); + client.send("Test".getBytes()); + + } finally { + client.close(); + } + + + } + + private void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (Exception e) { + } + } +}