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.net.SocketAddress;
23  import java.util.Collection;
24  
25  import junit.framework.Assert;
26  
27  import org.apache.mina.common.ConnectFuture;
28  import org.apache.mina.common.IoConnector;
29  import org.apache.mina.common.IoHandlerAdapter;
30  import org.apache.mina.common.IoSession;
31  import org.apache.mina.transport.AbstractBindTest;
32  
33  /**
34   * Tests {@link VmPipeAcceptor} bind and unbind.
35   * 
36   * @author The Apache Directory Project (mina-dev@directory.apache.org)
37   * @version $Rev$, $Date$ 
38   */
39  public class VmPipeBindTest extends AbstractBindTest {
40  
41      public VmPipeBindTest() {
42          super(new VmPipeAcceptor());
43      }
44  
45      protected SocketAddress createSocketAddress(int port) {
46          return new VmPipeAddress(port);
47      }
48  
49      protected int getPort(SocketAddress address) {
50          return ((VmPipeAddress) address).getPort();
51      }
52  
53      public void testUnbindDisconnectsClients() throws Exception {
54          // TODO: This test is almost identical to the test with the same name in SocketBindTest
55          bind(false);
56  
57          SocketAddress addr = createSocketAddress(port);
58  
59          IoConnector connector = new VmPipeConnector();
60          IoSession[] sessions = new IoSession[5];
61          for (int i = 0; i < sessions.length; i++) {
62              ConnectFuture future = connector.connect(addr,
63                      new IoHandlerAdapter());
64              future.join();
65              sessions[i] = future.getSession();
66              Assert.assertTrue(sessions[i].isConnected());
67          }
68  
69          // Wait for the server side sessions to be created.
70          Thread.sleep(500);
71  
72          Collection managedSessions = acceptor.getManagedSessions(addr);
73          Assert.assertEquals(5, managedSessions.size());
74          // Make sure it's the server side sessions we get when calling getManagedSessions()
75          for (int i = 0; i < sessions.length; i++) {
76              Assert.assertFalse(managedSessions.contains(sessions[i]));
77          }
78  
79          acceptor.unbind(addr);
80  
81          // Wait for the client side sessions to close.
82          Thread.sleep(500);
83  
84          for (int i = 0; i < sessions.length; i++) {
85              Assert.assertFalse(sessions[i].isConnected());
86          }
87      }
88  }