    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.activeio;
    19
    20  import java.io.IOException;
    21  import java.net.URI;
    22  import java.net.URISyntaxException;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.activeio.adapter.AsynchToSynchChannelAdapter;
    27  import org.activeio.adapter.SynchToAsynchChannelAdapter;
    28  import org.activeio.net.AIOAsynchChannel;
    29  import org.activeio.net.AIOSynchChannelServer;
    30  import org.activeio.net.NIOAsynchChannel;
    31  import org.activeio.net.NIOAsynchChannelServer;
    32  import org.activeio.net.NIOSynchChannel;
    33  import org.activeio.net.NIOSynchChannelServer;
    34  import org.activeio.net.SocketSynchChannel;
    35  import org.activeio.net.SocketSynchChannelServer;
    36  import org.activeio.net.VMPipeAsynchChannelPipe;
    37  import org.activeio.net.VMPipeAsynchChannelServer;
    38  import org.apache.commons.logging.Log;
    39  import org.apache.commons.logging.LogFactory;
    40
    41  import EDU.oswego.cs.dl.util.concurrent.Latch;
    42
    43  /***
    44   */
    45  public class ChannelFactoryTest extends TestCase {
    46
    47      static final Log log = LogFactory.getLog(ChannelFactoryTest.class);
    48      static boolean aioDisabled = System.getProperty("disable.aio.tests", "false").equals("true");
    49
    50      ChannelFactory factory = new ChannelFactory();
    51
    52      private SynchChannelServer synchChannelServer;
    53      private SynchChannel clientSynchChannel;
    54      private SynchChannel serverSynchChannel;
    55
    56      private AsynchChannelServer asynchChannelServer;
    57      private AsynchChannel clientAsynchChannel;
    58      private AsynchChannel serverAsynchChannel;
    59
    60      protected void setUp() throws Exception {
    61          log.info("Running: "+getName());
    62      }
    63
    64      public void testSocket() throws IOException, URISyntaxException, InterruptedException {
    65
    66          createSynchObjects("socket://localhost:0");
    67          assertNotNull( synchChannelServer.narrow(SocketSynchChannelServer.class) );
    68          assertNotNull( clientSynchChannel.narrow(SocketSynchChannel.class) );
    69          assertNotNull( serverSynchChannel.narrow(SocketSynchChannel.class) );
    70
    71          createAsynchObjects("socket://localhost:0");
    72          assertNotNull( asynchChannelServer.narrow(SocketSynchChannelServer.class) );
    73          assertNotNull( clientAsynchChannel.narrow(SocketSynchChannel.class) );
    74          assertNotNull( serverAsynchChannel.narrow(SocketSynchChannel.class) );
    75
    76      }
    77
    78      public void testAIO() throws IOException, URISyntaxException, InterruptedException {
    79
    80          if( aioDisabled ) {
    81              return;
    82          }
    83
    84          createSynchObjects("aio://localhost:0");
    85          assertNotNull( synchChannelServer.narrow(AIOSynchChannelServer.class) );
    86          assertNotNull( clientSynchChannel.narrow(AIOAsynchChannel.class) );
    87          assertNotNull( serverSynchChannel.narrow(AIOAsynchChannel.class) );
    88
    89          createAsynchObjects("aio://localhost:0");
    90          assertNotNull( asynchChannelServer.narrow(AIOSynchChannelServer.class) );
    91          assertNotNull( clientAsynchChannel.narrow(AIOAsynchChannel.class) );
    92          assertNotNull( serverAsynchChannel.narrow(AIOAsynchChannel.class) );
    93
    94      }
    95
    96      public void testNIO() throws IOException, URISyntaxException, InterruptedException {
    97
    98          createSynchObjects("nio://localhost:0");
    99          assertNotNull( synchChannelServer.narrow(NIOSynchChannelServer.class) );
    100         assertNotNull( clientSynchChannel.narrow(NIOSynchChannel.class) );
    101         assertNotNull( serverSynchChannel.narrow(NIOSynchChannel.class) );
    102
    103         createAsynchObjects("nio://localhost:0");
    104         assertNotNull( asynchChannelServer.narrow(NIOAsynchChannelServer.class) );
    105         assertNotNull( clientAsynchChannel.narrow(NIOAsynchChannel.class) );
    106         assertNotNull( serverAsynchChannel.narrow(NIOAsynchChannel.class) );
    107
    108     }
    109
    110     public void testVMPipe() throws IOException, URISyntaxException, InterruptedException {
    111
    112         createSynchObjects("vmpipe://localhost");
    113         assertNotNull( synchChannelServer.narrow(VMPipeAsynchChannelServer.class) );
    114         assertNotNull( clientSynchChannel.narrow(VMPipeAsynchChannelPipe.PipeChannel.class) );
    115         assertNotNull( serverSynchChannel.narrow(VMPipeAsynchChannelPipe.PipeChannel.class) );
    116
    117         createAsynchObjects("vmpipe://localhost");
    118         assertNotNull( asynchChannelServer.narrow(VMPipeAsynchChannelServer.class) );
    119         assertNotNull( clientAsynchChannel.narrow(VMPipeAsynchChannelPipe.PipeChannel.class) );
    120         assertNotNull( serverAsynchChannel.narrow(VMPipeAsynchChannelPipe.PipeChannel.class) );
    121
    122     }
    123
    124     private void createSynchObjects(String bindURI) throws IOException, URISyntaxException {
    125         synchChannelServer = factory.bindSynchChannel(new URI(bindURI));
    126         synchChannelServer.start();
    127         clientSynchChannel = factory.openSynchChannel(synchChannelServer.getConnectURI());
    128         serverSynchChannel = AsynchToSynchChannelAdapter.adapt( synchChannelServer.accept(1000*5) );
    129         serverSynchChannel.dispose();
    130         clientSynchChannel.dispose();
    131         synchChannelServer.dispose();
    132     }
    133
    134     private void createAsynchObjects(String bindURI) throws IOException, URISyntaxException, InterruptedException {
    135         asynchChannelServer = factory.bindAsynchChannel(new URI(bindURI));
    136         final Latch accepted = new Latch();
    137         asynchChannelServer.setAcceptListener(new AcceptListener() {
    138             public void onAccept(Channel channel) {
    139                 serverAsynchChannel = SynchToAsynchChannelAdapter.adapt(channel);
    140                 channel.dispose();
    141                 accepted.release();
    142             }
    143             public void onAcceptError(IOException error) {
    144                 error.printStackTrace();
    145             }
    146         });
    147         asynchChannelServer.start();
    148         clientAsynchChannel = factory.openAsynchChannel(asynchChannelServer.getConnectURI());
    149         accepted.attempt(1000*10);
    150         clientAsynchChannel.dispose();
    151         asynchChannelServer.dispose();
    152     }
    153
    154 }