Revision: 16277
Author: oleg.kulikoff
Date: Sat Jan 8 21:49:18 2011
Log: Request executors tests
http://code.google.com/p/mobicents/source/detail?r=16277
Added:
/trunk/servers/media/core/controllers/mgcp/src/test/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutorsTest.java
Modified:
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutor.java
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutors.java
=======================================
--- /dev/null
+++
/trunk/servers/media/core/controllers/mgcp/src/test/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutorsTest.java
Sat Jan 8 21:49:18 2011
@@ -0,0 +1,105 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.mobicents.media.server.ctrl.mgcp.signal;
+
+import jain.protocol.ip.mgcp.message.parms.EventName;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.mobicents.media.server.spi.Connection;
+import org.mobicents.media.server.spi.Endpoint;
+
+/**
+ *
+ * @author kulikov
+ */
+public class RequestExecutorsTest implements Dispatcher {
+ private RequestExecutors executors;
+ private RequestExecutor executor;
+
+ private int count;
+ private Logger logger = Logger.getLogger(RequestExecutorsTest.class);
+
+ public RequestExecutorsTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ logger.info("Preloading executors....");
+ count = 0;
+ executors = new RequestExecutors(3);
+ logger.info("Done");
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ /**
+ * Test of setDispatcher method, of class RequestExecutor.
+ */
+ @Test
+ public void testPollAndRecycle() throws Exception {
+ logger.info("Polling executors and finally recycle executor");
+ assertEquals(3, executors.remainder());
+
+ //poll and check remainder
+ executor = executors.poll();
+ assertEquals(2, executors.remainder());
+
+ executor.setDispatcher(this);
+ executor.execute();
+ assertEquals(1, count);
+
+ //recyle and check remainder
+ executor.recycle();
+ assertEquals(3, executors.remainder());
+
+ //do it once again
+ executor = executors.poll();
+ assertEquals(2, executors.remainder());
+
+ executor.execute();
+ assertEquals(1, count);
+
+ executor.recycle();
+ assertEquals(3, executors.remainder());
+
+ }
+
+
+
+ public void onEvent(EventName event) {
+ logger.info("Receive " + event);
+ count++;
+ }
+
+ public void completed() {
+ count++;
+ }
+
+ public Endpoint getEndpoint() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public Connection getConnection(String ID) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+
+}
=======================================
---
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutor.java
Sat Jan 8 05:35:12 2011
+++
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutor.java
Sat Jan 8 21:49:18 2011
@@ -76,8 +76,8 @@
*
* @param dispatcher the handler.
*/
- public void setDispatcher(Dispatcher listener) {
- this.dispatcher = listener;
+ public void setDispatcher(Dispatcher dispatcher) {
+ this.dispatcher = dispatcher;
}
/**
@@ -114,6 +114,8 @@
if (signals.size() > 0) {
currentSignal = signals.remove(0);
currentSignal.execute();
+ } else if (dispatcher != null) {
+ dispatcher.completed();
}
}
@@ -161,9 +163,17 @@
}
public void recycle() {
+ //cancels executions of currently active signal
+ cancel();
+
+ //deregister dispatcher
+ this.dispatcher = null;
+
+ //reset all executors
for (SignalExecutor exec : executors) {
exec.reset();
}
+
pool.recycle(this);
}
/**
=======================================
---
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutors.java
Sat Jan 8 05:35:12 2011
+++
/trunk/servers/media/core/controllers/mgcp/src/main/java/org/mobicents/media/server/ctrl/mgcp/signal/RequestExecutors.java
Sat Jan 8 21:49:18 2011
@@ -19,7 +19,6 @@
package org.mobicents.media.server.ctrl.mgcp.signal;
import javolution.util.FastList;
-import org.apache.log4j.Logger;
/**
* Pool of Request executors.
@@ -31,14 +30,15 @@
private FastList<RequestExecutor> pool = new FastList();
private final MgcpPackages packages = new MgcpPackages();
- private Logger logger = Logger.getLogger(RequestExecutors.class);
/**
* Creates new instance of this pool and pre-loads it.
*
* @param count the number of executors in the poll loaded at start up.
*/
- public RequestExecutors(int count) throws Exception {
- pool.add(new RequestExecutor(this, packages.loadAll()));
+ public RequestExecutors(int count) throws Exception {
+ for (int i = 0; i < count; i++) {
+ pool.add(new RequestExecutor(this, packages.loadAll()));
+ }
}
/**
@@ -47,16 +47,12 @@
* @return first unused executor.
*/
public RequestExecutor poll() throws Exception {
- logger.info("Polling, size= " + pool.size());
if (pool.size() > 0) {
RequestExecutor re = pool.remove(0);
- logger.info("Return " + re);
return re;
}
RequestExecutor re = new RequestExecutor(this, packages.loadAll());
- logger.info("Return " + re);
-
return re;
}
@@ -67,6 +63,14 @@
*/
protected void recycle(RequestExecutor executor) {
this.pool.add(executor);
- logger.info("Recycled " + executor + " size=" + pool.size());
+ }
+
+ /**
+ * Returns number of inactive executors in the pool.
+ *
+ * @return the number of executors.
+ */
+ public int remainder() {
+ return pool.size();
}
}