\BUG-7386 Remove blocking gracefulshutdown
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
12 import static org.opendaylight.protocol.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
13
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.protocol.pcep.PCEPCapability;
29 import org.opendaylight.protocol.pcep.PCEPDispatcher;
30 import org.opendaylight.protocol.pcep.PCEPSession;
31 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
32 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
33 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
34 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
35 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
36 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
37 import org.opendaylight.protocol.util.InetSocketAddressUtil;
38
39 public class PCCDispatcherImplTest {
40
41     private static final List<PCEPCapability> CAPS = new ArrayList<>();
42     private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(30, 120, CAPS);
43     private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0);
44     private PCCDispatcherImpl dispatcher;
45     private PCEPDispatcher pcepDispatcher;
46     private InetSocketAddress serverAddress;
47     private InetSocketAddress clientAddress;
48     private EventLoopGroup workerGroup;
49     private EventLoopGroup bossGroup;
50
51     @Before
52     public void setUp() {
53         this.workerGroup = new NioEventLoopGroup();
54         this.bossGroup = new NioEventLoopGroup();
55         this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
56         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
57             this.nf, this.bossGroup, this.workerGroup);
58         this.serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
59         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
60     }
61
62     @After
63     public void tearDown() throws InterruptedException, ExecutionException {
64         this.dispatcher.close();
65         closeEventLoopGroups();
66     }
67
68     private void closeEventLoopGroups() throws ExecutionException, InterruptedException {
69         this.workerGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
70         this.bossGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
71     }
72
73     @Test
74     public void testClientReconnect() throws Exception {
75         final Future<PCEPSession> futureSession = this.dispatcher.createClient(this.serverAddress, 1, new TestingSessionListenerFactory(),
76             this.nf, null, this.clientAddress);
77         waitFutureSuccess(futureSession);
78         final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
79         final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.serverAddress, slf, null);
80         waitFutureSuccess(futureServer);
81         final Channel channel = futureServer.channel();
82         Assert.assertNotNull(futureSession.get());
83         checkSessionListenerNotNull(slf, this.clientAddress.getHostString());
84         final TestingSessionListener sl = checkSessionListenerNotNull(slf, this.clientAddress.getAddress().getHostAddress());
85         Assert.assertNotNull(sl.getSession());
86         Assert.assertTrue(sl.isUp());
87         channel.close().get();
88         closeEventLoopGroups();
89
90         this.workerGroup = new NioEventLoopGroup();
91         this.bossGroup = new NioEventLoopGroup();
92         this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
93             this.nf, this.bossGroup, this.workerGroup);
94
95         final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
96         final ChannelFuture future2 = this.pcepDispatcher.createServer(this.serverAddress, slf2, null);
97         waitFutureSuccess(future2);
98         final Channel channel2 = future2.channel();
99         final TestingSessionListener sl2 = checkSessionListenerNotNull(slf2, this.clientAddress.getAddress().getHostAddress());
100         Assert.assertNotNull(sl2.getSession());
101         Assert.assertTrue(sl2.isUp());
102         channel2.close();
103     }
104 }