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