Skip to content

Commit

Permalink
fixed unit test - mockgateway now with bi-directional reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Dec 9, 2023
1 parent 975a806 commit ffe474d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 33 deletions.
36 changes: 30 additions & 6 deletions src/main/java/org/myrobotlab/service/MockGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void handle(Message msg) {

}
}

@Override
public void send(Message msg) {
super.send(msg);
invoke("publishMessageEvent", msg);
msgs.add(msg);
}

public MockGateway(String reservedKey, String inId) {
super(reservedKey, inId);
Expand Down Expand Up @@ -114,9 +121,13 @@ public void sendRemote(Message msg) throws Exception {
}

q.add(msg);
invoke("publishSendMessage", msg);
invoke("publishMessageEvent", msg);
msgs.add(msg);

// verify the msg can be serialized
String json = CodecUtils.toJson(msg);
log.debug("sendRemote {}", json);

if (!remoteServices.containsKey(msg.name)) {
error("got remote message from %s - and do not have its client !!!", msg.name);
return;
Expand Down Expand Up @@ -271,11 +282,7 @@ public String onToString() {
return toString();
}

public Message publishSendMessage(Message msg) {
return msg;
}

public Message publishReceiveMessage(Message msg) {
public Message publishMessageEvent(Message msg) {
return msg;
}

Expand Down Expand Up @@ -308,4 +315,21 @@ public static void main(String[] args) {
e.printStackTrace();
}
}

public Message getMsg(String name, String callback) {
String fullName = getFullRemoteName(name);

String key = String.format("%s.%s", fullName, callback);
if (!sendQueues.containsKey(key)) {
return null;
}

try {
Message msg = sendQueues.get(key).poll(0, TimeUnit.MILLISECONDS);
return msg;
} catch (InterruptedException e) {
log.info("interrupted");
}
return null;
}
}
49 changes: 26 additions & 23 deletions src/main/resources/resource/WebGui/app/service/js/MockGatewayGui.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
angular.module("mrlapp.service.MockGatewayGui", []).controller("MockGatewayGuiCtrl", [
"$scope",
"mrl",
function ($scope, mrl) {
angular.module("mrlapp.service.MockGatewayGui", []).controller("MockGatewayGuiCtrl", ["$scope", "mrl", function($scope, mrl) {
console.info("MockGatewayGuiCtrl")
var _self = this
var msg = this.msg

// GOOD TEMPLATE TO FOLLOW
this.updateState = function (service) {
$scope.service = service
this.updateState = function(service) {
$scope.service = service
}

$scope.msgToRemote = function(msg) {
if ($scope.service) {
return msg.name.endsWith($scope.service.remoteId)
} else {
return false
}
}

this.onMsg = function (inMsg) {
let data = inMsg.data[0]
switch (inMsg.method) {
this.onMsg = function(inMsg) {
let data = inMsg.data[0]
switch (inMsg.method) {
case "onState":
_self.updateState(data)
$scope.$apply()
break
case "onSendMessage":
const date = new Date(data)
$scope.onTime = date.toLocaleString()
$scope.$apply()
break
_self.updateState(data)
$scope.$apply()
break
case "onMessageEvent":
$scope.service.msgs.push(data)
$scope.$apply()
break
default:
console.error("ERROR - unhandled method " + $scope.name + " " + inMsg.method)
break
}
console.error("ERROR - unhandled method " + $scope.name + " " + inMsg.method)
break
}
}

// msg.subscribe("publishSendMessage")
msg.subscribe("publishMessageEvent")
msg.subscribe(this)
},
])
}
, ])
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<div class="col-md-12">
<table class="table table-striped table-hover">

<h3>{{service.id}} <span class="glyphicon glyphicon-resize-horizontal" style="color: grey;" /> {{service.remoteId}} </h3>

<thead>
<tr>
<th>ts</th>
<th>sender</th>
<th></th>
<th>name</th>
<th>method</th>
<th>data</th>
Expand All @@ -14,11 +17,12 @@
<tbody>
<tr ng-repeat="msg in service.msgs">
<td><small>{{msg.msgId}}</small></td>
<td><small>{{msg.sender}} <span class="glyphicon glyphicon-arrow-right" style="color: grey;" ></span></small></td>
<td><small>{{msg.sender}} </small></td>
<td><small><span ng-hide="msgToRemote(msg)" class="glyphicon glyphicon-arrow-left" style="color: grey;" /><span ng-hide="!msgToRemote(msg)" class="glyphicon glyphicon-arrow-right" style="color: grey;" /></small></td>
<td><small>{{msg.name}}</small></td>
<td><small>[{{msg.method}}]</small></td>
<td><small>{{msg.method}}</small></td>
<td><small>{{msg.data}}</small></td>
</tr>
</tbody>
</table>
</table>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testProxyFactory() {
// TimeoutException indicates we're getting all the way to
// Service.waitOn(), so things *probably* work.
// FIXME use mocks or startup a second instance to rigorously test
assertThrows(TimeoutException.class, proxy::getName);
assertThrows(TimeoutException.class, proxy::getPeerKeys);
}

@Test
Expand Down

0 comments on commit ffe474d

Please sign in to comment.