Skip to content

Commit

Permalink
fix(fjage): adding test for ParameterChangeNtf and fixed implementati…
Browse files Browse the repository at this point in the history
…on issue
  • Loading branch information
notthetup committed Jan 22, 2024
1 parent 904176b commit 4945921
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ protected ParameterRsp processParameterReq(ParameterReq msg, ParameterRsp rsp) {
}
if (sv != null) {
rsp.set(e.param, sv, false);
if (sv != current) onParamChange(e.param, ndx, sv);
if (!sv.equals(current)) onParamChange(e.param, ndx, sv);
}
} catch (NoSuchMethodException ex) {
Object rv = setParam(e.param, ndx, evalue);
if (rv != null) {
rsp.set(e.param, rv, isReadOnly(e.param, ndx));
if (rv != current) onParamChange(e.param, ndx, rv);
if (!rv.equals(current)) onParamChange(e.param, ndx, rv);
} else {
if (ndx >= 0) throw ex;
Field f = cls.getField(fldName);
Expand All @@ -259,7 +259,7 @@ protected ParameterRsp processParameterReq(ParameterReq msg, ParameterRsp rsp) {
}
rv = f.get(agent);
rsp.set(e.param, rv, ro);
if (rv != current) onParamChange(e.param, ndx, rv);
if (!rv.equals(current)) onParamChange(e.param, ndx, rv);
}
}
}
Expand Down
68 changes: 66 additions & 2 deletions src/test/java/org/arl/fjage/test/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,30 @@ public void testAIDParam() {
assertTrue(client1.count + client2.count + client3.count > 100);
}

@Test
public void testParamNtf(){
log.info("testParamNtf");
Platform platform = new RealTimePlatform();
Container container = new Container(platform);
ParamServerAgent server = new ParamServerAgent(false);
ParamNtfClientAgent client = new ParamNtfClientAgent();
container.add("S", server);
container.add("C", client);
platform.start();
platform.delay(2000);
assertEquals(server.n, 1);
client.zVal = 7;
platform.delay(2000);
assertEquals(server.n, 2);
log.info("Warnings: "+client.warnings);
log.info("Errors: "+(server.errors + client.errors));
assertEquals(server.errors, 0);
assertEquals(client.errors, 0);
assertEquals(client.warnings, 0);
platform.shutdown();
platform.delay(2000);
}

private static class RequestMessage extends Message {
private static final long serialVersionUID = 1L;
public int x;
Expand All @@ -517,12 +541,14 @@ public NuisanceMessage(AgentID recipient) {
}

public enum Params implements Parameter {
x, y, s
x, y, z, s
}

public class ParamServerAgent extends Agent {
public int errors = 0;
public int x = 1;
public int n = 0;
public int z = 0;
public float getY() { return 2; }
public String getS() { return "xxx"; }
public int setX(int x) { return x+1; }
Expand All @@ -535,7 +561,12 @@ public ParamServerAgent(boolean yor) {
@Override
public void init() {
register("server");
add(new ParameterMessageBehavior(Params.class));
add(new ParameterMessageBehavior(Params.class){
@Override
protected void onParamChange(Parameter p, int ndx, Object v) {
n++;
}
});
add(new MessageBehavior() {
@Override
public void onReceive(Message msg) {
Expand Down Expand Up @@ -699,6 +730,39 @@ public void onTick() {
}
}

private class ParamNtfClientAgent extends Agent{
public int zVal = 4;
public int errors = 0;
public int warnings = 0;
public ParamNtfClientAgent() {
super();
}
@Override
public void init(){
delay(200);

AgentID server = agentForService("server");
if (server == null) {
log.warning("Unable to find server");
errors++;
return;
}
add(new TickerBehavior(100) {
@Override
public void onTick() {
Message req = new ParameterReq().set(Params.z, zVal);
req.setRecipient(server);
Message rsp = request(req, 1000);
if (rsp == null) {
log.warning("Unable to set z to " + zVal);
warnings++;
}
}
});
}

}

private class AIDParamClientAgent extends Agent {
public int errors = 0;
public int warnings = 0;
Expand Down

0 comments on commit 4945921

Please sign in to comment.