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.Test;
12 import org.junit.runner.RunWith;
13 import org.opendaylight.controller.sal.connector.remoterpc.dto.Message;
14 import org.opendaylight.controller.sal.connector.remoterpc.dto.MessageWrapper;
15 import org.powermock.api.mockito.PowerMockito;
16 import org.powermock.core.classloader.annotations.PrepareForTest;
17 import org.powermock.modules.junit4.PowerMockRunner;
18 import org.zeromq.ZMQ;
20 import java.util.concurrent.TimeoutException;
22 import static org.mockito.Mockito.doNothing;
24 @RunWith(PowerMockRunner.class)
25 @PrepareForTest(RpcSocket.class)
26 public class RpcSocketTest {
27 RpcSocket rpcSocket = new RpcSocket("tcp://localhost:5554", new ZMQ.Poller(1));
28 RpcSocket spy = PowerMockito.spy(rpcSocket);
31 public void testCreateSocket() throws Exception {
32 Assert.assertEquals("tcp://localhost:5554", spy.getAddress());
33 Assert.assertEquals(ZMQ.REQ, spy.getSocket().getType());
36 @Test(expected = TimeoutException.class)
37 public void send_WhenQueueGetsFull_ShouldThrow() throws Exception {
39 doNothing().when(spy).process();
42 for (int i=0;i<10;i++){
43 spy.send(getEmptyMessageWrapper());
46 //sending 11th message should throw
47 spy.send(getEmptyMessageWrapper());
51 public void testHasTimedOut() throws Exception {
52 spy.send(getEmptyMessageWrapper());
53 Assert.assertFalse(spy.hasTimedOut());
55 Assert.assertFalse(spy.hasTimedOut());
57 Assert.assertTrue(spy.hasTimedOut());
61 public void testProcess() throws Exception {
62 PowerMockito.doNothing().when(spy, "sendMessage");
63 spy.send(getEmptyMessageWrapper());
65 //Next message should get queued
66 spy.send(getEmptyMessageWrapper());
68 //queue size should be 2
69 Assert.assertEquals(2, spy.getQueueSize());
73 //sleep for 2 secs (timeout)
74 //message send would be retried
80 spy.process(); //retry fails, next message will get picked up
81 Assert.assertEquals(1, spy.getQueueSize());
85 public void testProcessStateTransitions() throws Exception {
86 PowerMockito.doNothing().when(spy, "sendMessage");
87 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
88 spy.send(getEmptyMessageWrapper());
89 Assert.assertEquals(1, spy.getQueueSize());
91 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
94 //1st timeout, 2nd try
97 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
100 //2nd timeout, 3rd try
103 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
106 //3rd timeout, no more tries => remove
109 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
110 Assert.assertEquals(0, spy.getQueueSize());
114 public void testParseMessage() throws Exception {
115 // Write an integration test for parseMessage
119 public void testRecycleSocket() throws Exception {
120 // This will need to be updated in the future...for now, recycleSocket() calls close()
121 Assert.assertTrue(spy.getSocket().base().check_tag());
123 Assert.assertEquals(10, spy.getSocket().getLinger());
124 Assert.assertFalse(spy.getSocket().base().check_tag());
128 public void testClose() throws Exception {
129 Assert.assertTrue(spy.getSocket().base().check_tag());
131 Assert.assertEquals(10, spy.getSocket().getLinger());
132 Assert.assertFalse(spy.getSocket().base().check_tag());
136 public void testReceive() throws Exception {
137 PowerMockito.doReturn(null).when(spy, "parseMessage");
138 PowerMockito.doNothing().when(spy, "process");
139 spy.send(getEmptyMessageWrapper());
141 //There should be 1 message waiting in the queue
142 Assert.assertEquals(1, spy.getQueueSize());
145 //This should complete message processing
146 //The message should be removed from the queue
147 Assert.assertEquals(0, spy.getQueueSize());
148 Assert.assertEquals(RpcSocket.NUM_RETRIES, spy.getRetriesLeft());
153 public void testReceiveStateTransitions() throws Exception {
154 PowerMockito.doReturn(null).when(spy, "parseMessage");
155 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
156 spy.send(getEmptyMessageWrapper());
158 //There should be 1 message waiting in the queue
159 Assert.assertEquals(1, spy.getQueueSize());
160 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
163 //This should complete message processing
164 //The message should be removed from the queue
165 Assert.assertEquals(0, spy.getQueueSize());
166 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
169 private MessageWrapper getEmptyMessageWrapper(){
170 return new MessageWrapper(new Message(), null);
174 public void testProcessReceiveSequence() throws Exception {
175 PowerMockito.doNothing().when(spy, "sendMessage");
176 PowerMockito.doReturn(null).when(spy, "parseMessage");
177 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
178 spy.send(getEmptyMessageWrapper());
179 spy.send(getEmptyMessageWrapper());
180 Assert.assertEquals(2, spy.getQueueSize());
181 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
185 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
187 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
188 Assert.assertEquals(1, spy.getQueueSize());
191 Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
193 Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
194 Assert.assertEquals(0, spy.getQueueSize());