2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connector.remoterpc;
10 import junit.framework.Assert;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.connector.remoterpc.utils.MessagingUtil;
15 import org.opendaylight.controller.sal.core.api.Broker;
16 import org.zeromq.ZMQ;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
23 import static org.mockito.Mockito.mock;
25 public class ServerRequestHandlerTest {
27 ServerRequestHandler handler;
29 ExecutorService executorService = Executors.newCachedThreadPool();
30 private int workerCount = 2;
31 private String mockDealerAddress = "inproc://rpc-request-handler";
32 private String mockServerIp = "localhost";
33 private int mockServerPort = 5554;
36 public void setUp() throws Exception {
37 context = ZMQ.context(1);
38 String mockServerAddress = mockServerIp + ":" + mockServerPort;
39 Broker.ProviderSession mockSession = mock(Broker.ProviderSession.class);
40 handler = new ServerRequestHandler(context, mockSession, workerCount, mockDealerAddress, mockServerAddress);
45 public void tearDown() throws Exception {
46 executorService.shutdown();
47 MessagingUtil.closeZmqContext(context);
52 public void testStart() throws Exception {
53 //should start workers == workerCount
54 Assert.assertEquals(workerCount, handler.getWorkerPool().getPoolSize());
56 //killing a thread should recreate another one
58 //start router-dealer bridge
59 executorService.execute(MessagingUtil.createRouterDealerBridge(context, mockDealerAddress, mockServerPort));
60 Thread.sleep(1000); //give sometime for socket initialization
62 //this will kill the thread
63 final String WORKER_THREAD_NAME = "remote-rpc-worker";
64 interruptAThreadWithName(WORKER_THREAD_NAME);
66 //send 4 message to router
67 for (int i = 0; i < 4; i++)
68 executorService.execute(MessagingUtil.sendAnEmptyMessage(context, "tcp://" + mockServerIp + ":" + mockServerPort));
70 //worker pool size should not change.
71 Assert.assertEquals(workerCount, handler.getWorkerPool().getPoolSize());
73 Thread.sleep(10000); //wait for processing to complete
77 public void testClose() throws Exception {
82 * Interrupts the first thread found whose name starts with the provided name
86 private void interruptAThreadWithName(String name) {
87 List<Thread> workerThreads = findThreadsWithName(name);
88 if (workerThreads.size() > 0) workerThreads.get(0).interrupt();
92 * Find all threads that start with the given name
97 private List<Thread> findThreadsWithName(String name) {
98 Thread[] threads = new Thread[Thread.activeCount()];
99 Thread.enumerate(threads);
101 List<Thread> foundThreads = new ArrayList();
102 for (Thread t : threads) {
103 if (t.getName().startsWith(name))