Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 63a844f

Browse files
author
Gabriel Schulhof
committed
Tests: Add repeated observation
1 parent 1bdfc97 commit 63a844f

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var client = require( process.argv[ 3 ] ).client;
16+
17+
console.log( JSON.stringify( { assertionCount: 2 } ) );
18+
19+
function observeResource( resource ) {
20+
return new Promise( function( fulfill ) {
21+
var updateCount = 0;
22+
function update( resource ) {
23+
if ( ++updateCount === 6 ) {
24+
console.log( JSON.stringify( { assertion: "ok", arguments: [
25+
true, "Client: Observation sequence complete"
26+
] } ) );
27+
resource.removeListener( "update", update );
28+
fulfill();
29+
}
30+
}
31+
resource.on( "update", update );
32+
} );
33+
}
34+
35+
function resourcefound( resource ) {
36+
client.removeListener( "resourcefound", resourcefound );
37+
observeResource( resource )
38+
.then( function() {
39+
return new Promise( function( fulfill ) {
40+
setTimeout( function() {
41+
fulfill( observeResource( resource ) );
42+
}, 5000 );
43+
} );
44+
} )
45+
.catch( function( error ) {
46+
console.log( JSON.stringify( { assertion: "ok", arguments: [
47+
false, "Client: Error during observation: " + ( "" + error ) + "\n" +
48+
JSON.stringify( error, null, 4 )
49+
] } ) );
50+
} );
51+
}
52+
53+
client
54+
.findResources( { resourcePath: "/a/" + process.argv[ 2 ] }, resourcefound )
55+
.catch( function( error ) {
56+
console.log( JSON.stringify( { assertion: "ok", arguments: [
57+
false, "Client: Failed to initiate resource discovery: " + ( "" + error ) + "\n" +
58+
JSON.stringify( error, null, 4 )
59+
] } ) );
60+
} );
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var server = require( process.argv[ 3 ] ).server;
16+
var timeoutId = 0;
17+
var observerCount = 0;
18+
var cycleCount = 0;
19+
20+
console.log( JSON.stringify( { assertionCount: 8 } ) );
21+
22+
function fakeSensorLoop( resource ) {
23+
resource.properties.value = Math.random() + 1;
24+
25+
// Ensure a total of six notifications
26+
timeoutId = setTimeout( fakeSensorLoop, ( Math.random() ) * 1000 + 500, resource );
27+
28+
resource.notify().catch( function( error ) {
29+
console.log( JSON.stringify( { assertion: "ok", arguments: [
30+
false, "Server: Notification error: " +
31+
( "" + error ) + "\n" + JSON.stringify( error, null, 4 )
32+
] } ) );
33+
} );
34+
}
35+
36+
server
37+
.register( {
38+
resourcePath: "/a/" + process.argv[ 2 ],
39+
resourceTypes: [ "core.light" ],
40+
interfaces: [ "oic.if.baseline" ],
41+
discoverable: true,
42+
observable: true,
43+
properties: {
44+
value: Math.random() + 1
45+
}
46+
} )
47+
.then(
48+
function( resource ) {
49+
resource.onretrieve( function( request ) {
50+
51+
observerCount += ( "observe" in request ) ? ( request.observe ? 1 : -1 ) : 0;
52+
53+
console.log( JSON.stringify( { assertion: "ok", arguments: [
54+
true, "Server: Retrieve request " +
55+
"(observe: " + request.observe + ", count: " + observerCount + ")"
56+
] } ) );
57+
58+
// Start the notification loop when all observers have checked in
59+
if ( observerCount > 0 && !timeoutId ) {
60+
timeoutId = setTimeout( fakeSensorLoop, 1000, resource );
61+
} else if ( observerCount === 0 && timeoutId ) {
62+
timeoutId = clearTimeout( timeoutId );
63+
cycleCount++;
64+
}
65+
66+
request.respond().then(
67+
function() {
68+
console.log( JSON.stringify( { assertion: "ok", arguments: [
69+
true, "Server: Successfully responded to retrieve request"
70+
] } ) );
71+
if ( cycleCount === 2 ) {
72+
console.log( JSON.stringify( { finished: 0 } ) );
73+
}
74+
},
75+
function( error ) {
76+
console.log( JSON.stringify( { assertion: "ok", arguments: [
77+
false, "Server: Failed to respond to retrieve request: " +
78+
( "" + error ) + "\n" + JSON.stringify( error, null, 4 )
79+
] } ) );
80+
} );
81+
} );
82+
console.log( JSON.stringify( { ready: true } ) );
83+
},
84+
function( error ) {
85+
console.log( JSON.stringify( { assertion: "ok", arguments: [
86+
false, "Server: Registering resource failed unexpectedly: " +
87+
( "" + error ) + "\n" + JSON.stringify( error, null, 4 )
88+
] } ) );
89+
} );

0 commit comments

Comments
 (0)