    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.transport.vmpipe;
    21
    22  import java.util.concurrent.Semaphore;
    23  import java.util.concurrent.TimeUnit;
    24
    25  import junit.framework.Assert;
    26  import junit.framework.TestCase;
    27
    28  import org.apache.mina.common.ByteBuffer;
    29  import org.apache.mina.common.ConnectFuture;
    30  import org.apache.mina.common.IoAcceptor;
    31  import org.apache.mina.common.IoConnector;
    32  import org.apache.mina.common.IoHandlerAdapter;
    33  import org.apache.mina.common.IoSession;
    34  import org.apache.mina.common.ThreadModel;
    35
    36  /**
    37   * Makes sure if the order of event is correct.
    38   *
    39   * @author The Apache MINA Project Team (dev@mina.apache.org)
    40   * @version $Rev: 635494 $, $Date: 2008-03-10 18:04:45 +0900 (Mon, 10 Mar 2008) $
    41   */
    42  public class VmPipeEventOrderTest extends TestCase {
    43      public void testServerToClient() throws Exception {
    44          IoAcceptor acceptor = new VmPipeAcceptor();
    45          acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
    46          //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
    47
    48          IoConnector connector = new VmPipeConnector();
    49          connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
    50          //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
    51
    52          acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() {
    53              public void sessionOpened(IoSession session) throws Exception {
    54                  session.write("B");
    55              }
    56
    57              public void messageSent(IoSession session, Object message)
    58                      throws Exception {
    59                  session.close();
    60              }
    61          });
    62
    63          final StringBuffer actual = new StringBuffer();
    64
    65          ConnectFuture future = connector.connect(new VmPipeAddress(1),
    66                  new IoHandlerAdapter() {
    67
    68                      public void messageReceived(IoSession session,
    69                              Object message) throws Exception {
    70                          actual.append(message);
    71                      }
    72
    73                      public void sessionClosed(IoSession session)
    74                              throws Exception {
    75                          actual.append("C");
    76                      }
    77
    78                      public void sessionOpened(IoSession session)
    79                              throws Exception {
    80                          actual.append("A");
    81                      }
    82
    83                  });
    84
    85          future.join();
    86          future.getSession().getCloseFuture().join();
    87          acceptor.unbindAll();
    88
    89          // sessionClosed() might not be invoked yet
    90          // even if the connection is closed.
    91          while (actual.indexOf("C") < 0) {
    92              Thread.yield();
    93          }
    94
    95          Assert.assertEquals("ABC", actual.toString());
    96      }
    97
    98      public void testClientToServer() throws Exception {
    99          IoAcceptor acceptor = new VmPipeAcceptor();
    100         acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
    101         //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
    102
    103         IoConnector connector = new VmPipeConnector();
    104         connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
    105         //connector.getFilterChain().addLast( "logger", new LoggingFilter() );
    106
    107         final StringBuffer actual = new StringBuffer();
    108
    109         acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() {
    110
    111             public void messageReceived(IoSession session, Object message)
    112                     throws Exception {
    113                 actual.append(message);
    114             }
    115
    116             public void sessionClosed(IoSession session) throws Exception {
    117                 actual.append("C");
    118             }
    119
    120             public void sessionOpened(IoSession session) throws Exception {
    121                 actual.append("A");
    122             }
    123
    124         });
    125
    126         ConnectFuture future = connector.connect(new VmPipeAddress(1),
    127                 new IoHandlerAdapter() {
    128                     public void sessionOpened(IoSession session)
    129                             throws Exception {
    130                         session.write("B");
    131                     }
    132
    133                     public void messageSent(IoSession session, Object message)
    134                             throws Exception {
    135                         session.close();
    136                     }
    137                 });
    138
    139         future.join();
    140         future.getSession().getCloseFuture().join();
    141         acceptor.unbindAll();
    142
    143         // sessionClosed() might not be invoked yet
    144         // even if the connection is closed.
    145         while (actual.indexOf("C") < 0) {
    146             Thread.yield();
    147         }
    148
    149         Assert.assertEquals("ABC", actual.toString());
    150     }
    151
    152
    153     public void testSessionCreated() throws Exception {
    154         final Semaphore semaphore = new Semaphore(0);
    155         final StringBuffer stringBuffer = new StringBuffer();
    156         VmPipeAcceptor vmPipeAcceptor = new VmPipeAcceptor();
    157         vmPipeAcceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
    158         final VmPipeAddress vmPipeAddress = new VmPipeAddress(12345);
    159         vmPipeAcceptor.bind(vmPipeAddress, new IoHandlerAdapter() {
    160             @Override
    161             public void sessionCreated(IoSession session) throws Exception {
    162                 // pretend we are doing some time-consuming work. For
    163                 // performance reasons, you would never want to do time
    164                 // consuming work in sessionCreated.
    165                 // However, this increases the likelihood of the timing bug.
    166                 Thread.sleep(1000);
    167                 stringBuffer.append("A");
    168             }
    169
    170             @Override
    171             public void sessionOpened(IoSession session) throws Exception {
    172                 stringBuffer.append("B");
    173             }
    174
    175             @Override
    176             public void messageReceived(IoSession session, Object message)
    177                     throws Exception {
    178                 stringBuffer.append("C");
    179             }
    180
    181             @Override
    182             public void sessionClosed(IoSession session) throws Exception {
    183                 stringBuffer.append("D");
    184                 semaphore.release();
    185             }
    186         });
    187
    188         final VmPipeConnector vmPipeConnector = new VmPipeConnector();
    189         ConnectFuture connectFuture = vmPipeConnector.connect(vmPipeAddress, new IoHandlerAdapter() {
    190             @Override
    191             public void sessionOpened(IoSession session) throws Exception {
    192                 session.write(ByteBuffer.wrap(new byte[1]));
    193             }
    194         });
    195
    196         connectFuture.join();
    197         connectFuture.getSession().close();
    198         semaphore.tryAcquire(1, TimeUnit.SECONDS);
    199         vmPipeAcceptor.unbind(vmPipeAddress);
    200         Assert.assertEquals("ABCD", stringBuffer.toString());
    201     }
    202 }