Changed codec for Identityref in JSON transformation
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / RpcSocketTest.java
1 /*
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
6  */
7
8 package org.opendaylight.controller.sal.connector.remoterpc;
9
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;
19
20 import java.util.concurrent.TimeoutException;
21
22 import static org.mockito.Mockito.doNothing;
23
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);
29
30   @Test
31   public void testCreateSocket() throws Exception {
32     Assert.assertEquals("tcp://localhost:5554", spy.getAddress());
33     Assert.assertEquals(ZMQ.REQ, spy.getSocket().getType());
34   }
35
36   @Test(expected = TimeoutException.class)
37   public void send_WhenQueueGetsFull_ShouldThrow() throws Exception {
38
39     doNothing().when(spy).process();
40
41     //10 is queue size
42     for (int i=0;i<10;i++){
43       spy.send(getEmptyMessageWrapper());
44     }
45
46     //sending 11th message should throw
47     spy.send(getEmptyMessageWrapper());
48   }
49
50   @Test
51   public void testHasTimedOut() throws Exception {
52     spy.send(getEmptyMessageWrapper());
53     Assert.assertFalse(spy.hasTimedOut());
54     Thread.sleep(1000);
55     Assert.assertFalse(spy.hasTimedOut());
56     Thread.sleep(1000);
57     Assert.assertTrue(spy.hasTimedOut());
58   }
59
60   @Test
61   public void testProcess() throws Exception {
62     PowerMockito.doNothing().when(spy, "sendMessage");
63     spy.send(getEmptyMessageWrapper());
64
65     //Next message should get queued
66     spy.send(getEmptyMessageWrapper());
67
68     //queue size should be 2
69     Assert.assertEquals(2, spy.getQueueSize());
70
71
72     spy.process();
73     //sleep for 2 secs (timeout)
74     //message send would be retried
75     Thread.sleep(2000);
76     spy.process();
77     Thread.sleep(2000);
78     spy.process();
79     Thread.sleep(2000);
80     spy.process(); //retry fails, next message will get picked up
81     Assert.assertEquals(1, spy.getQueueSize());
82   }
83
84   @Test
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());
90     Thread.sleep(200);
91     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
92     Thread.sleep(1800);
93
94     //1st timeout, 2nd try
95     spy.process();
96     Thread.sleep(200);
97     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
98     Thread.sleep(1800);
99
100     //2nd timeout, 3rd try
101     spy.process();
102     Thread.sleep(200);
103     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
104     Thread.sleep(1800);
105
106     //3rd timeout, no more tries => remove
107     spy.process();
108     Thread.sleep(200);
109     Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
110     Assert.assertEquals(0, spy.getQueueSize());
111   }
112
113   @Test
114   public void testParseMessage() throws Exception {
115     // Write an integration test for parseMessage
116   }
117
118   @Test
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());
122     spy.close();
123     Assert.assertEquals(10, spy.getSocket().getLinger());
124     Assert.assertFalse(spy.getSocket().base().check_tag());
125   }
126
127   @Test
128   public void testClose() throws Exception {
129     Assert.assertTrue(spy.getSocket().base().check_tag());
130     spy.close();
131     Assert.assertEquals(10, spy.getSocket().getLinger());
132     Assert.assertFalse(spy.getSocket().base().check_tag());
133   }
134
135   @Test
136   public void testReceive() throws Exception {
137     PowerMockito.doReturn(null).when(spy, "parseMessage");
138     PowerMockito.doNothing().when(spy, "process");
139     spy.send(getEmptyMessageWrapper());
140
141     //There should be 1 message waiting in the queue
142     Assert.assertEquals(1, spy.getQueueSize());
143
144     spy.receive();
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());
149
150   }
151
152   @Test
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());
157
158     //There should be 1 message waiting in the queue
159     Assert.assertEquals(1, spy.getQueueSize());
160     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
161
162     spy.receive();
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);
167   }
168
169   private MessageWrapper getEmptyMessageWrapper(){
170     return new MessageWrapper(new Message(), null);
171   }
172
173   @Test
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);
182
183
184     Thread.sleep(2000);
185     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
186     spy.receive();
187     Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
188     Assert.assertEquals(1, spy.getQueueSize());
189
190     spy.process();
191     Assert.assertTrue(spy.getState() instanceof RpcSocket.BusySocketState);
192     spy.receive();
193     Assert.assertTrue(spy.getState() instanceof RpcSocket.IdleSocketState);
194     Assert.assertEquals(0, spy.getQueueSize());
195   }
196 }