Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ public void deleteConnection(String nodeId) {
}
}

public void invalidateConnection(Connection con) {
GenericObjectPool<Connection> pool = poolMap.get(con.getNodeId());
if (pool != null) {
synchronized(pool) {
if(poolMap.containsKey(con.getNodeId())) {
try {
pool.invalidateObject(con);
con.forceClose();
return;
} catch (Exception e) {
log.error("invalidateConnection() : " + e.getMessage());
}
}
}
}
}

public void returnConnection(Connection con) {
try {
con.setSoTimeout(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void failCount(Connection con) {
return;
}
failCount(con.getNodeId());
sps.invalidateConnection(con);
con.forceClose();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.rakuten.rit.roma.romac4j.connection;

import junit.framework.TestCase;

/**
* Created by yinchin.chen on 10/27/16.
*/
public class RomaSocketPoolTest extends TestCase {

RomaSocketPool FIXTURE;
String nodeId = "localhost_11211";

public void setUp() {
RomaSocketPool.init();
FIXTURE = RomaSocketPool.getInstance();
}

public void tearDown(){
FIXTURE.deleteConnection(nodeId);
}

public void testConnection() throws Exception {
// get connection1 and return connection1
Connection con1 = FIXTURE.getConnection(nodeId);
FIXTURE.returnConnection(con1);

// get connection2
Connection con2 = FIXTURE.getConnection(nodeId);
// con1 must equal con2
assertEquals(con1, con2);

// invalid con2
FIXTURE.invalidateConnection(con2);
// get con3
Connection con3 = FIXTURE.getConnection(nodeId);
assertNotSame(con2, con3);
// test connection.
assertTrue(con2.isClosed());
assertTrue(con3.isConnected());
FIXTURE.returnConnection(con3);
}

public void testGetConnections() throws Exception{
Connection con1 = FIXTURE.getConnection(nodeId);
Connection con2 = FIXTURE.getConnection(nodeId);
Connection con3 = FIXTURE.getConnection(nodeId);
Connection con4 = FIXTURE.getConnection(nodeId);
FIXTURE.returnConnection(con1);
FIXTURE.returnConnection(con2);
FIXTURE.returnConnection(con3);
FIXTURE.returnConnection(con4);
}
}