Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 1 | var test = require('prova'); |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 2 | var mercury = require('mercury'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 3 | var _ = require('lodash'); |
| 4 | var proxyquire = require('proxyquireify')(require); |
| 5 | var mockLRUCache = require('./mocks/lru-cache'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 6 | |
Ali Ghassemi | a35f042 | 2014-09-26 09:04:04 -0700 | [diff] [blame] | 7 | // @noCallThru ensures this completely overrdies the original config |
| 8 | // instead of inheriting the properties that are not defined here from |
| 9 | // the original dependency |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 10 | var veyronConfigForTest = { |
Ali Ghassemi | a35f042 | 2014-09-26 09:04:04 -0700 | [diff] [blame] | 11 | '@noCallThru': true |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 12 | }; |
| 13 | |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 14 | // The NAMESPACE_ROOT environment variable is set by servicerunner in the "make |
| 15 | // test" target. That environment variable is picked up by the "envify" prova |
| 16 | // transform and used to set process.env.NAMESPACE_ROOT. |
| 17 | var globalRoot = process.env.NAMESPACE_ROOT; |
| 18 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 19 | // Require namespaceService but using test specific mocks and configs |
| 20 | var namespaceService = |
| 21 | proxyquire('../../../../src/services/namespace/service.js', { |
| 22 | '../../veyron-config': veyronConfigForTest, |
| 23 | 'lru-cache': function() { |
| 24 | return mockLRUCache; |
| 25 | } |
| 26 | }); |
| 27 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 28 | test('getChildren of default namespace root', function(t) { |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 29 | namespaceService.getChildren(). |
| 30 | then(function assertResult(result) { |
Ali Ghassemi | 1125694 | 2014-10-08 15:14:49 -0700 | [diff] [blame] | 31 | assertIsImmutable(t, result); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 32 | // Wait until we finish, we expect 2 top level items: cottage, house |
| 33 | result.events.on('end', function validate() { |
Ali Ghassemi | 2a60a25 | 2014-11-13 09:00:23 -0800 | [diff] [blame] | 34 | mercury.watch(result, function(children) { |
Ali Ghassemi | 01811a3 | 2014-11-04 13:58:34 -0800 | [diff] [blame] | 35 | assertCottage(children[0]); |
| 36 | assertHouse(children[1]); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 37 | t.end(); |
Ali Ghassemi | 2a60a25 | 2014-11-13 09:00:23 -0800 | [diff] [blame] | 38 | }); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 39 | }); |
Ali Ghassemi | 65ee326 | 2015-02-23 11:36:59 -0800 | [diff] [blame^] | 40 | result.events.on('globError', function(error) { |
| 41 | t.notOk(error, 'did not expect any globs errors'); |
| 42 | t.end(); |
| 43 | }); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 44 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 45 | |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame] | 46 | function assertCottage(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 47 | assertServer(t, item, { |
| 48 | name: 'cottage', |
| 49 | objectName: 'cottage', |
| 50 | isGlobbable: true, |
| 51 | type: 'mounttable' |
| 52 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | function assertHouse(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 56 | assertServer(t, item, { |
| 57 | name: 'house', |
| 58 | objectName: 'house', |
| 59 | isGlobbable: true, |
| 60 | type: 'mounttable' |
| 61 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 62 | } |
| 63 | }); |
| 64 | |
| 65 | test('getChildren of cottage/lawn', function(t) { |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 66 | namespaceService.getChildren('cottage/lawn'). |
| 67 | then(function assertResult(result) { |
Ali Ghassemi | 1125694 | 2014-10-08 15:14:49 -0700 | [diff] [blame] | 68 | assertIsImmutable(t, result); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 69 | // Wait until we finish, we expect 3 items back, front and master-sprinkler |
| 70 | result.events.on('end', function validate() { |
| 71 | mercury.watch(result, function(children) { |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 72 | assertBack(children[0]); |
| 73 | assertSprinkler(children[2]); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 74 | t.end(); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 75 | }); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 76 | }); |
Ali Ghassemi | 65ee326 | 2015-02-23 11:36:59 -0800 | [diff] [blame^] | 77 | result.events.on('globError', function(error) { |
| 78 | t.notOk(error, 'did not expect any globs errors'); |
| 79 | t.end(); |
| 80 | }); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 81 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 82 | |
| 83 | function assertSprinkler(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 84 | assertServer(t, item, { |
| 85 | name: 'master-sprinkler', |
| 86 | objectName: 'cottage/lawn/master-sprinkler', |
| 87 | isGlobbable: false, |
| 88 | type: 'unknown' |
| 89 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | function assertBack(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 93 | assertIntermediaryName(t, item, { |
| 94 | name: 'back', |
| 95 | objectName: 'cottage/lawn/back' |
| 96 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 97 | } |
| 98 | }); |
| 99 | |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 100 | test('getChildren of rooted ' + globalRoot + '/house/kitchen', function(t) { |
| 101 | namespaceService.getChildren(globalRoot + '/house/kitchen'). |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 102 | then(function assertResult(result) { |
Ali Ghassemi | 1125694 | 2014-10-08 15:14:49 -0700 | [diff] [blame] | 103 | assertIsImmutable(t, result); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 104 | // Wait until we finish, we expect 2 items, lights and smoke-detector |
| 105 | result.events.on('end', function validate() { |
| 106 | mercury.watch(result, function(children) { |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 107 | assertLightSwitch(children[0]); |
| 108 | assertSmokeDetector(children[1]); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 109 | t.end(); |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 110 | }); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 111 | }); |
Ali Ghassemi | 65ee326 | 2015-02-23 11:36:59 -0800 | [diff] [blame^] | 112 | result.events.on('globError', function(error) { |
| 113 | t.notOk(error, 'did not expect any globs errors'); |
| 114 | t.end(); |
| 115 | }); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 116 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 117 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 118 | function assertLightSwitch(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 119 | assertServer(t, item, { |
| 120 | name: 'lights', |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 121 | objectName: globalRoot + '/house/kitchen/lights', |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 122 | isGlobbable: false, |
| 123 | type: 'unknown' |
| 124 | }); |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 127 | function assertSmokeDetector(item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 128 | assertServer(t, item, { |
| 129 | name: 'smoke-detector', |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 130 | objectName: globalRoot + '/house/kitchen/smoke-detector', |
| 131 | isGlobbable: false, |
| 132 | type: 'unknown' |
| 133 | }); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | // The HOUSE_MOUNTTABLE environment variable is set by run-tests.sh. That |
| 138 | // environment variable is picked up by the "envify" prova transform and used to |
| 139 | // set process.env.HOUSE_MOUNTTABLE. |
| 140 | var hostPortRoot = process.env.HOUSE_MOUNTTABLE; |
| 141 | |
| 142 | test('getChildren of rooted ' + hostPortRoot + '/kitchen', function(t) { |
| 143 | namespaceService.getChildren(hostPortRoot + '/kitchen'). |
| 144 | then(function assertResult(result) { |
| 145 | assertIsImmutable(t, result); |
| 146 | // Wait until we finish, we expect 2 items, lights and smoke-detector |
| 147 | result.events.on('end', function validate() { |
| 148 | mercury.watch(result, function(children) { |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 149 | assertLightSwitch(children[0]); |
| 150 | assertSmokeDetector(children[1]); |
| 151 | t.end(); |
| 152 | }); |
| 153 | }); |
Ali Ghassemi | 65ee326 | 2015-02-23 11:36:59 -0800 | [diff] [blame^] | 154 | result.events.on('globError', function(error) { |
| 155 | t.notOk(error, 'did not expect any globs errors'); |
| 156 | t.end(); |
| 157 | }); |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 158 | }).catch(t.end); |
| 159 | |
| 160 | function assertLightSwitch(item) { |
| 161 | assertServer(t, item, { |
| 162 | name: 'lights', |
| 163 | objectName: hostPortRoot + '/kitchen/lights', |
| 164 | isGlobbable: false, |
| 165 | type: 'unknown' |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | function assertSmokeDetector(item) { |
| 170 | assertServer(t, item, { |
| 171 | name: 'smoke-detector', |
| 172 | objectName: hostPortRoot + '/kitchen/smoke-detector', |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 173 | isGlobbable: false, |
| 174 | type: 'unknown' |
| 175 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 176 | } |
| 177 | }); |
| 178 | |
Ali Ghassemi | a7b255a | 2014-11-26 13:06:56 -0800 | [diff] [blame] | 179 | test('getChildren of non-existing mounttable', function(t) { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 180 | // TODO(aghassemi) why does namespace library return empty results instead of |
| 181 | // error when globbing rooted names that don't exist? |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 182 | namespaceService.getChildren('/DoesNotExist:666/What/Ever'). |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 183 | then(function assertResult(result) { |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 184 | result.events.on('end', function validate() { |
| 185 | // Expect empty results |
| 186 | mercury.watch(result, function(children) { |
| 187 | t.deepEqual(children, []); |
| 188 | t.end(); |
| 189 | }); |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 190 | }); |
Ali Ghassemi | 65ee326 | 2015-02-23 11:36:59 -0800 | [diff] [blame^] | 191 | result.events.on('globError', function(error) { |
| 192 | // we do actually expect a glob error in this case |
Ali Ghassemi | 218fa08 | 2014-12-01 17:00:38 -0800 | [diff] [blame] | 193 | t.ok(error); |
| 194 | }); |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 195 | }).catch(t.end); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 196 | }); |
| 197 | |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 198 | test('getNamespaceItem of leaf server', function(t) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 199 | namespaceService.getNamespaceItem('cottage/lawn/master-sprinkler'). |
| 200 | then(function assertItem(itemObs) { |
| 201 | assertIsImmutable(t, itemObs); |
| 202 | var item = itemObs(); |
| 203 | assertServer(t, item, { |
| 204 | name: 'master-sprinkler', |
| 205 | objectName: 'cottage/lawn/master-sprinkler', |
| 206 | isGlobbable: false, |
| 207 | type: 'unknown' |
| 208 | }); |
| 209 | t.end(); |
| 210 | }).catch(t.end); |
| 211 | }); |
| 212 | |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 213 | test('getNamespaceItem of intermediary name', function(t) { |
| 214 | namespaceService.getNamespaceItem('cottage/lawn/back'). |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 215 | then(function assertItem(itemObs) { |
| 216 | assertIsImmutable(t, itemObs); |
| 217 | var item = itemObs(); |
| 218 | assertIntermediaryName(t, item, { |
| 219 | name: 'back', |
| 220 | objectName: 'cottage/lawn/back' |
| 221 | }); |
| 222 | t.end(); |
| 223 | }).catch(t.end); |
| 224 | }); |
| 225 | |
Ali Ghassemi | 9c5cc19 | 2014-11-19 18:31:05 -0800 | [diff] [blame] | 226 | test('getNamespaceItem of mounttable leaf server', function(t) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 227 | namespaceService.getNamespaceItem('cottage'). |
| 228 | then(function assertItem(itemObs) { |
| 229 | assertIsImmutable(t, itemObs); |
| 230 | var item = itemObs(); |
| 231 | assertServer(t, item, { |
| 232 | name: 'cottage', |
| 233 | objectName: 'cottage', |
| 234 | isGlobbable: true, |
| 235 | type: 'mounttable' |
| 236 | }); |
| 237 | t.end(); |
| 238 | }).catch(t.end); |
| 239 | }); |
| 240 | |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 241 | test('search uses caching', function(t) { |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 242 | mockLRUCache.reset(); |
| 243 | |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 244 | namespaceService.search('house', '*'). |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 245 | then(function assertNoCacheHit() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 246 | t.notOk(mockLRUCache.wasCacheHit('glob|house/*'), |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 247 | 'first glob call is not a cache hit'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 248 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 249 | // Call second time, there should have been a cache hit |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 250 | return namespaceService.search('house', '*'); |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 251 | }).then(function assertCacheHit() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 252 | t.ok(mockLRUCache.wasCacheHit('glob|house/*'), |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 253 | 'second glob call is a cache hit'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 254 | |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 255 | // Call glob with same name, different query |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 256 | return namespaceService.search('house', 'foo*'); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 257 | }).then(function assertNoCacheHit() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 258 | t.notOk(mockLRUCache.wasCacheHit('glob|house/foo*'), |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 259 | 'third glob call with different query is not a cache hit'); |
| 260 | t.end(); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 261 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 262 | }); |
| 263 | |
| 264 | test('getSignature uses caching', function(t) { |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 265 | mockLRUCache.reset(); |
| 266 | |
| 267 | namespaceService.getSignature('house/alarm').then(function() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 268 | t.notOk(mockLRUCache.wasCacheHit('getSignature|house/alarm'), |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 269 | 'first getSignature call is not a cache hit'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 270 | // Call a second time |
| 271 | return namespaceService.getSignature('house/alarm'); |
| 272 | }).then(function() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 273 | t.ok(mockLRUCache.wasCacheHit('getSignature|house/alarm'), |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 274 | 'second getSignature call is a cache hit'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 275 | // Call a different name |
| 276 | return namespaceService.getSignature('house/kitchen/smoke-detector'); |
| 277 | }).then(function() { |
Ali Ghassemi | a8b1548 | 2014-10-20 12:47:01 -0700 | [diff] [blame] | 278 | t.notOk(mockLRUCache.wasCacheHit( |
| 279 | 'getSignature|house/kitchen/smoke-detector' |
| 280 | ),'third getSignature call to a different name is not a cache hit'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 281 | t.end(); |
Ali Ghassemi | 980a379 | 2014-10-01 13:54:03 -0700 | [diff] [blame] | 282 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 283 | }); |
| 284 | |
| 285 | //TODO(aghassemi) |
| 286 | //Tests for: |
| 287 | // Recursive glob |
| 288 | // Glob with some keyword |
| 289 | // Ensuring array is updated when nodes get mounted and unmounted (when we use |
| 290 | // watchGlob) |
Alex Fandrianto | 81b3184 | 2014-10-14 09:54:02 -0700 | [diff] [blame] | 291 | |
| 292 | // Make RPC: good inputs => no error |
| 293 | var okRPCs = { |
| 294 | 'no input': ['house/alarm', 'status', []], |
| 295 | 'bool input': ['house/living-room/lights', 'flipSwitch', [true]], |
| 296 | 'int input': ['cottage/smoke-detector', 'sensitivity', [2]], |
| 297 | 'float input': ['house/alarm', 'delayArm', [2.5]], |
| 298 | 'string input': ['cottage/pool/speaker', 'playSong', ['Happy Birthday']], |
| 299 | 'slice input': ['house/master-bedroom/speaker', 'addSongs', [['A', 'B']]], |
| 300 | '2+ inputs': ['cottage/pool/heater', 'start', [70, 5]], |
| 301 | }; |
| 302 | |
| 303 | _.forOwn(okRPCs, function run(params, inputType) { |
| 304 | test( |
| 305 | 'makeRPC accepts good input - ' + inputType, |
| 306 | testMakeRPCNoError.bind(null, params) |
| 307 | ); |
| 308 | }); |
| 309 | |
| 310 | // Make RPC: bad inputs => error |
| 311 | var badRPCs = { |
Ali Ghassemi | 78e3bc8 | 2014-11-30 11:53:43 -0800 | [diff] [blame] | 312 | //TODO(aghassemi) re-enable after #483 |
| 313 | //'no service': ['mansion/smoke-detector', 'status', []], |
Alex Fandrianto | 81b3184 | 2014-10-14 09:54:02 -0700 | [diff] [blame] | 314 | 'no method': ['cottage/pool/speaker', 'status', []], |
| 315 | 'no input': ['cottage/lights','flipSwitch', null], |
| 316 | 'bad type': ['cottage/lights','flipSwitch', ['notBool']], |
| 317 | 'lacks input': ['cottage/pool/heater','start', [80]], |
| 318 | 'invalid input': ['house/living-room/blast-speaker','playSong', ['notThere']] |
| 319 | }; |
| 320 | |
| 321 | _.forOwn(badRPCs, function run(params, inputType) { |
| 322 | test( |
| 323 | 'makeRPC errors on bad input - ' + inputType, |
| 324 | testMakeRPCHasError.bind(null, params) |
| 325 | ); |
| 326 | }); |
| 327 | |
| 328 | // Make RPC: outputs have the expected # of outputs |
| 329 | test('makeRPC returns output properly', function(t) { |
| 330 | namespaceService.makeRPC('cottage/alarm', 'panic', []).then( |
Alex Fandrianto | db090a6 | 2015-01-16 15:05:03 -0800 | [diff] [blame] | 331 | function got0Outputs(res) { // 0 outputs: has no result. |
| 332 | t.ok(res === undefined, '0 outputs => is undefined'); |
Alex Fandrianto | 81b3184 | 2014-10-14 09:54:02 -0700 | [diff] [blame] | 333 | |
| 334 | return namespaceService.makeRPC('house/alarm', 'status', []); |
| 335 | } |
| 336 | ).then( // 1 output: (Non-array/slice output) is not an Array. |
| 337 | function got1Output(res) { |
| 338 | t.notOk(res instanceof Array, '1 output => not an Array'); |
| 339 | |
| 340 | return namespaceService.makeRPC('cottage/smoke-detector', 'test', []); |
| 341 | } |
| 342 | ).then( // 1 output: Delayed return. Also not an array. |
| 343 | function got1OutputDelayed(res) { |
| 344 | t.notOk(res instanceof Array, '1 output => not an Array'); |
| 345 | |
| 346 | return namespaceService.makeRPC('cottage/pool/heater', 'status', []); |
| 347 | } |
| 348 | ).then( // 2 outputs: Is an Array of the correct length. |
| 349 | function got2Outputs(res) { |
| 350 | var ok = res instanceof Array && res.length === 2; |
| 351 | t.ok(ok, '2 outputs => length 2 Array'); |
| 352 | t.end(); |
| 353 | } |
| 354 | ).catch(t.end); |
| 355 | }); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Test helpers |
| 359 | */ |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 360 | function assertServer(t, item, vals) { |
| 361 | assertMountedName(t, item, vals.name); |
| 362 | assertObjectName(t, item, vals.objectName); |
| 363 | assertIsServer(t, item); |
| 364 | if (vals.isGlobbable === true) { |
| 365 | assertIsGlobbable(t, item); |
| 366 | } else if(vals.isGlobbable === false) { |
| 367 | assertIsNotGlobbable(t, item); |
| 368 | } |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 369 | |
| 370 | if (vals.type === 'unknown') { |
| 371 | assertUnknownServiceTypeInfo(t, item); |
| 372 | } else if (vals.type === 'mounttable') { |
| 373 | assertMounttableServiceTypeInfo(t, item); |
| 374 | } else { |
| 375 | t.fail('Unknown type: ' + vals.type); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | function assertIntermediaryName(t, item, vals) { |
| 380 | assertMountedName(t, item, vals.name); |
| 381 | assertObjectName(t, item, vals.objectName); |
| 382 | assertIsNotServer(t, item); |
| 383 | assertIsGlobbable(t, item); |
| 384 | } |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 385 | |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 386 | function assertMountedName(t, item, val) { |
| 387 | t.ok(item.mountedName, item.mountedName + ': has a mounted name'); |
| 388 | t.equal(item.mountedName, val, item.mountedName + ': mounted name matches'); |
| 389 | } |
| 390 | |
| 391 | function assertObjectName(t, item, val) { |
| 392 | t.ok(item.objectName, item.mountedName + ': has an object name'); |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 393 | t.equal(item.objectName, val, item.mountedName + ': object name matches'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | function assertIsServer(t, item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 397 | t.equal(item.isServer, true, item.mountedName + ': is a server'); |
| 398 | t.ok(item.serverInfo, item.mountedName + ': has server info'); |
Alex Fandrianto | f87d2a0 | 2015-01-27 10:42:52 -0800 | [diff] [blame] | 399 | t.ok(item.serverInfo.endpoints.length > 0, item.mountedName + |
| 400 | ': has at least 1 endpoint'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | function assertIsNotServer(t, item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 404 | t.equal(item.isServer, false, item.mountedName + ': is not a server'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | function assertIsGlobbable(t, item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 408 | t.equal(item.isGlobbable, true, item.mountedName + ': is globbable'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | function assertIsNotGlobbable(t, item) { |
Ali Ghassemi | 4306431 | 2014-10-14 17:24:32 -0700 | [diff] [blame] | 412 | t.equal(item.isGlobbable, false, item.mountedName + ': is not globbable'); |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Ali Ghassemi | 1125694 | 2014-10-08 15:14:49 -0700 | [diff] [blame] | 415 | function assertIsImmutable(t, observable) { |
| 416 | t.ok(observable.set === undefined, 'is immutable'); |
| 417 | } |
| 418 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 419 | /* |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 420 | * Asserts that a ServiceTypeInfo is of predefined type of Unknown Service. |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 421 | */ |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 422 | function assertUnknownServiceTypeInfo(t, item) { |
| 423 | var typeInfo = item.serverInfo.typeInfo; |
| 424 | t.equal(typeInfo.key, 'veyron-unknown', |
| 425 | item.mountedName + ': unknown type info has the right key'); |
| 426 | |
| 427 | t.equal(typeInfo.typeName, 'Service', |
| 428 | item.mountedName + ': unknown type info has the type name'); |
| 429 | |
| 430 | t.equal(typeInfo.description, null, |
| 431 | item.mountedName + ': unknown type info does not have description'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 435 | * Asserts that a ServiceTypeInfo is of predefined type of mounttable. |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 436 | */ |
Ali Ghassemi | d94e91c | 2014-10-01 11:22:46 -0700 | [diff] [blame] | 437 | function assertMounttableServiceTypeInfo(t, item) { |
| 438 | var typeInfo = item.serverInfo.typeInfo; |
| 439 | t.equal(typeInfo.key, 'veyron-mounttable', |
| 440 | item.mountedName + ': mounttable type info has the right key'); |
| 441 | |
| 442 | t.equal(typeInfo.typeName, 'Mount Table', |
| 443 | item.mountedName + ': mounttable type info has the type name'); |
| 444 | |
| 445 | t.ok(typeInfo.description, |
| 446 | item.mountedName + ': mounttable type info has a description'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 447 | } |
Alex Fandrianto | 81b3184 | 2014-10-14 09:54:02 -0700 | [diff] [blame] | 448 | /* |
| 449 | * Runs a test to ensure the makeRPC call terminates without error. |
| 450 | */ |
| 451 | function testMakeRPCNoError(args, t) { |
| 452 | namespaceService.makeRPC.apply(null, args).then(function(result) { |
| 453 | t.pass('completed without error'); |
| 454 | t.end(); |
| 455 | }).catch(function(err) { |
Ali Ghassemi | 442851c | 2014-12-12 13:47:40 -0800 | [diff] [blame] | 456 | t.end(err); |
Alex Fandrianto | 81b3184 | 2014-10-14 09:54:02 -0700 | [diff] [blame] | 457 | }); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Runs a test to ensure the makeRPC call terminates with an error. |
| 462 | */ |
| 463 | function testMakeRPCHasError(args, t) { |
| 464 | namespaceService.makeRPC.apply(null, args).then(function(result) { |
| 465 | t.fail('should not have completed without error'); |
| 466 | t.end(); |
| 467 | }).catch(function(err) { |
| 468 | t.pass('correctly returned an error'); |
| 469 | t.end(); |
| 470 | }); |
Nicolas LaCasse | 8d22fb2 | 2015-01-22 17:26:31 -0800 | [diff] [blame] | 471 | } |