Skip to content

Commit

Permalink
Some code enhancements and workaround to solve NPE in testsuite for L…
Browse files Browse the repository at this point in the history
…RR. Issues #37 and #38, PR #111
  • Loading branch information
Fernando Mendioroz committed Aug 7, 2017
1 parent fa8c4ac commit 142dc02
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public boolean handleEvent(StateEvent event) throws InternalException, OverloadE
return false;
}
final SLgSessionState state = this.sessionData.getSLgSessionState();
SLgSessionState newState = null;
SLgSessionState newState;
Event localEvent = (Event) event;
Event.Type eventType = (Type) event.getType();
switch (state) {
Expand Down Expand Up @@ -218,7 +218,7 @@ protected void setState(SLgSessionState newState) {
this.sessionData.setSLgSessionState(newState);

for (StateChangeListener i : stateListeners) {
i.stateChanged(this, (Enum) oldState, (Enum) newState);
i.stateChanged(this, oldState, newState);
}
if (newState == SLgSessionState.TERMINATED || newState == SLgSessionState.TIMEDOUT) {
super.cancelMsgTimer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean handleEvent(StateEvent event) throws InternalException, OverloadE
return false;
}
final SLhSessionState state = this.sessionData.getSLhSessionState();
SLhSessionState newState = null;
SLhSessionState newState;
Event localEvent = (Event) event;
Event.Type eventType = (Type) event.getType();
switch (state) {
Expand Down Expand Up @@ -200,7 +200,7 @@ protected void setState(SLhSessionState newState) {
this.sessionData.setSLhSessionState(newState);

for (StateChangeListener i : stateListeners) {
i.stateChanged(this, (Enum) oldState, (Enum) newState);
i.stateChanged(this, oldState, newState);
}
if (newState == SLhSessionState.TERMINATED || newState == SLhSessionState.TIMEDOUT) {
super.cancelMsgTimer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public boolean handleEvent(StateEvent event) throws InternalException, OverloadE
return false;
}
final SLgSessionState state = this.sessionData.getSLgSessionState();
SLgSessionState newState = null;
SLgSessionState newState;
Event localEvent = (Event) event;
Event.Type eventType = (Type) event.getType();

Expand Down Expand Up @@ -220,7 +220,7 @@ protected void setState(SLgSessionState newState) {
this.sessionData.setSLgSessionState(newState);

for (StateChangeListener i : stateListeners) {
i.stateChanged(this, (Enum) oldState, (Enum) newState);
i.stateChanged(this, oldState, newState);
}
if (newState == SLgSessionState.TERMINATED || newState == SLgSessionState.TIMEDOUT) {
super.cancelMsgTimer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean handleEvent(StateEvent event) throws InternalException, OverloadE
return false;
}
final SLhSessionState state = this.sessionData.getSLhSessionState();
SLhSessionState newState = null;
SLhSessionState newState;
Event localEvent = (Event) event;
Event.Type eventType = (Type) event.getType();

Expand Down Expand Up @@ -204,7 +204,7 @@ protected void setState(SLhSessionState newState) {
this.sessionData.setSLhSessionState(newState);

for (StateChangeListener i : stateListeners) {
i.stateChanged(this, (Enum) oldState, (Enum) newState);
i.stateChanged(this, oldState, newState);
}
if (newState == SLhSessionState.TERMINATED || newState == SLhSessionState.TIMEDOUT) {
super.cancelMsgTimer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ public Answer processRequest(Request request) {
return null;
}

// PLR methods
//*********************************************************
//***************** PLR methods ***************************
//*********************************************************

// { SLg-Location-Type }
protected int getSLgLocationType() {
Expand Down Expand Up @@ -552,7 +554,12 @@ public boolean isSentPLR() {
return sentPLR;
}

// LRA methods

//*********************************************************
//***************** LRA methods ***************************
//*********************************************************

// (commented LRA methods are already defined above for PLR)

/*@Override
protected java.net.InetAddress getGMLCAddress()*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2017, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

package org.mobicents.diameter.stack.functional.slg.base;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.jdiameter.api.DisconnectCause;
import org.jdiameter.api.Mode;
import org.jdiameter.api.Peer;
import org.jdiameter.api.PeerState;
import org.jdiameter.api.PeerTable;
import org.jdiameter.api.Stack;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
*
* @author <a href="mailto:fernando.mendioroz@gmail.com"> Fernando Mendioroz </a>
* @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
* @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
*/
@RunWith(Parameterized.class)
public class SLgSessionBasicSLgFlowTest {
// TODO: add test on replicated nodes ?
private ClientSLg clientNode;
private ServerSLg serverNode1;
private URI clientConfigURI;
private URI serverNode1ConfigURI;

/**
* @param clientConfigUrl
* @param serverNode1ConfigURL
* // @param node2
* // @param serverCount
*/
public SLgSessionBasicSLgFlowTest(String clientConfigUrl, String serverNode1ConfigURL) throws Exception {
super();
this.clientConfigURI = new URI(clientConfigUrl);
this.serverNode1ConfigURI = new URI(serverNode1ConfigURL);
}

@Before
public void setUp() throws Exception {
try {
this.clientNode = new ClientSLg();
this.serverNode1 = new ServerSLg();

this.serverNode1.init(new FileInputStream(new File(this.serverNode1ConfigURI)), "SERVER1");
this.serverNode1.start();

this.clientNode.init(new FileInputStream(new File(this.clientConfigURI)), "CLIENT");
this.clientNode.start(Mode.ANY_PEER, 10, TimeUnit.SECONDS);
Stack stack = this.clientNode.getStack();
List<Peer> peers = stack.unwrap(PeerTable.class).getPeerTable();
if (peers.size() == 1) {
// ok
}
else if (peers.size() > 1) {
// works better with replicated, since disconnected peers are also listed
boolean foundConnected = false;
for (Peer p : peers) {
if (p.getState(PeerState.class).equals(PeerState.OKAY)) {
if (foundConnected) {
throw new Exception("Wrong number of connected peers: " + peers);
}
foundConnected = true;
}
}
}
else {
throw new Exception("Wrong number of connected peers: " + peers);
}
}
catch (Throwable e) {
e.printStackTrace();
}
}

@After
public void tearDown() {
if (this.serverNode1 != null) {
try {
this.serverNode1.stop(DisconnectCause.REBOOTING);
}
catch (Exception e) {

}
this.serverNode1 = null;
}

if (this.clientNode != null) {
try {
this.clientNode.stop(DisconnectCause.REBOOTING);
}
catch (Exception e) {

}
this.clientNode = null;
}
}

@Test
public void testLocationReportRequestBasicFlow() throws Exception {
try {
// pain of parameter tests :) ?
clientNode.sendProvideLocationRequest();
waitForMessage();

serverNode1.sendProvideLocationAnswer();
waitForMessage();

serverNode1.sendLocationReportRequest();
waitForMessage();

clientNode.sendLocationReportAnswer();
waitForMessage();

}
catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}

if (!serverNode1.isReceivedPLR()) {
StringBuilder sb = new StringBuilder("Did not receive PLR! ");
sb.append("Server ER:\n").append(serverNode1.createErrorReport(this.serverNode1.getErrors()));

fail(sb.toString());
}
if (!clientNode.isReceivedPLA()) {
StringBuilder sb = new StringBuilder("Did not receive PLA! ");
sb.append("Client ER:\n").append(clientNode.createErrorReport(this.clientNode.getErrors()));

fail(sb.toString());
}
if (!clientNode.isReceivedLRR()) {
StringBuilder sb = new StringBuilder("Did not receive LRR! ");
sb.append("Client ER:\n").append(clientNode.createErrorReport(this.clientNode.getErrors()));

fail(sb.toString());
}
if (!serverNode1.isReceivedLRA()) {
StringBuilder sb = new StringBuilder("Did not receive LRA! ");
sb.append("Server ER:\n").append(serverNode1.createErrorReport(this.serverNode1.getErrors()));

fail(sb.toString());
}

if (!clientNode.isPassed()) {
StringBuilder sb = new StringBuilder();
sb.append("Client ER:\n").append(clientNode.createErrorReport(this.clientNode.getErrors()));

fail(sb.toString());
}

if (!serverNode1.isPassed()) {
StringBuilder sb = new StringBuilder();
sb.append("Server ER:\n").append(serverNode1.createErrorReport(this.serverNode1.getErrors()));

fail(sb.toString());
}
}

@Parameters
public static Collection<Object[]> data() {

String client = "configurations/functional-slg/config-client.xml";
String server1 = "configurations/functional-slg/config-server-node1.xml";

//String replicatedClient = "configurations/functional-slh/replicated-config-client.xml";
//String replicatedServer1 = "configurations/functional-slh/replicated-config-server-node1.xml";

Class<SLgSessionBasicSLgFlowTest> t = SLgSessionBasicSLgFlowTest.class;
client = t.getClassLoader().getResource(client).toString();
server1 = t.getClassLoader().getResource(server1).toString();
//replicatedClient = t.getClassLoader().getResource(replicatedClient).toString();
//replicatedServer1 = t.getClassLoader().getResource(replicatedServer1).toString();

return Arrays.asList(new Object[][] { { client, server1 }/*, { replicatedClient, replicatedServer1 } */});
}

private void waitForMessage() {
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void sendProvideLocationAnswer() throws Exception {
}

public void sendLocationReportRequest() throws Exception {
try {

super.serverSLgSession = this.sessionFactory.getNewAppSession("xx-SLg-TESTxx", getApplicationId(), ServerSLgSession.class, (Object) null);

} catch (Exception e) {
e.printStackTrace();
fail(null, e);
}
LocationReportRequest lrr = super.createLRR(super.serverSLgSession);
this.serverSLgSession.sendLocationReportRequest(lrr);
Utils.printMessage(log, super.stack.getDictionary(), lrr.getMessage(), true);
Expand Down Expand Up @@ -123,7 +131,9 @@ public void doLocationReportAnswerEvent(ServerSLgSession session, LocationReport
this.receivedLRA = true;
}

// PLA methods
//*********************************************************
//***************** PLA methods ***************************
//*********************************************************

@Override
protected byte[] getLocationEstimate() {
Expand Down Expand Up @@ -395,7 +405,12 @@ public boolean isSentPLA() {
return sentPLA;
}

// LRR methods

//*********************************************************
//***************** LRR methods ***************************
//*********************************************************

// (some of LRR methods are already defined above for PLA)

@Override
protected String getUserName() {
Expand Down

0 comments on commit 142dc02

Please sign in to comment.