Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 1 | var test = require('prova'); |
| 2 | var _ = require('lodash'); |
| 3 | var proxyquire = require('proxyquireify')(require); |
| 4 | var mockLRUCache = require('./mocks/lru-cache'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 5 | |
| 6 | // 8885 is the expected wspr port to be running for the tests |
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 | 'authenticate': false, |
| 12 | 'wspr': 'http://localhost:8885', |
| 13 | '@noCallThru': true |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | // Require namespaceService but using test specific mocks and configs |
| 17 | var namespaceService = |
| 18 | proxyquire('../../../../src/services/namespace/service.js', { |
| 19 | '../../veyron-config': veyronConfigForTest, |
| 20 | 'lru-cache': function() { |
| 21 | return mockLRUCache; |
| 22 | } |
| 23 | }); |
| 24 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 25 | test('getChildren of default namespace root', function(t) { |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 26 | t.plan(9+9); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 27 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 28 | namespaceService.getChildren(). |
| 29 | then(function assertResult(result) { |
| 30 | // Wait until we receive the 4 top level items, |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 31 | // cottage, house |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 32 | var numReturnedChildren; |
| 33 | result(function(children) { |
| 34 | numReturnedChildren = children.length; |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 35 | if (numReturnedChildren === 2) { |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 36 | children = _.sortBy(children, 'mountedName'); |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 37 | assertCottage(children[0]); |
| 38 | assertHouse(children[1]); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 39 | } |
| 40 | }); |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 41 | }).catch(); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 42 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 43 | // 9 assertions |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 44 | function assertCottage(item) { |
| 45 | t.equal(item.mountedName, 'cottage'); |
| 46 | t.equal(item.objectName, 'cottage'); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 47 | t.equal(item.isServer, true); |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 48 | t.equal(item.isGlobbable, true); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 49 | |
| 50 | t.ok(item.serverInfo.signature); |
Ali Ghassemi | f2540c9 | 2014-10-01 09:00:15 -0700 | [diff] [blame^] | 51 | |
| 52 | assertMounttableServiceTypeInfo(t, item.serverInfo.typeInfo); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 55 | // 9 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 56 | function assertHouse(item) { |
| 57 | t.equal(item.mountedName, 'house'); |
| 58 | t.equal(item.objectName, 'house'); |
| 59 | t.equal(item.isServer, true); |
| 60 | t.equal(item.isGlobbable, true); |
| 61 | |
| 62 | t.ok(item.serverInfo.signature); |
| 63 | |
| 64 | assertMounttableServiceTypeInfo(t, item.serverInfo.typeInfo); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | test('getChildren of cottage/lawn', function(t) { |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 69 | t.plan(9+5); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 70 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 71 | namespaceService.getChildren('cottage/lawn'). |
| 72 | then(function assertResult(result) { |
| 73 | // Wait until we receive the 3 items, |
| 74 | // back, front and master-sprinkler come back |
| 75 | var numReturnedChildren; |
| 76 | result(function(children) { |
| 77 | numReturnedChildren = children.length; |
| 78 | if (numReturnedChildren === 3) { |
| 79 | children = _.sortBy(children, 'mountedName'); |
| 80 | assertBack(children[0]); |
| 81 | assertSprinkler(children[2]); |
| 82 | } |
| 83 | }); |
| 84 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 85 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 86 | // 9 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 87 | function assertSprinkler(item) { |
| 88 | t.equal(item.mountedName, 'master-sprinkler'); |
| 89 | t.equal(item.objectName, 'cottage/lawn/master-sprinkler'); |
| 90 | t.equal(item.isServer, true); |
| 91 | t.equal(item.isGlobbable, false); |
| 92 | |
| 93 | assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo); |
| 94 | |
| 95 | t.ok(item.serverInfo.signature); |
| 96 | } |
| 97 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 98 | // 5 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 99 | function assertBack(item) { |
| 100 | t.equal(item.mountedName, 'back'); |
| 101 | t.equal(item.objectName, 'cottage/lawn/back'); |
| 102 | t.equal(item.isServer, false); |
| 103 | t.equal(item.isGlobbable, true); |
| 104 | t.equal(item.serverInfo, null); |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | test('getChildren of rooted /localhost:8881/house/kitchen', function(t) { |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 109 | t.plan(9+9); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 110 | |
| 111 | // 8881 is the expected root mounttable port to be running for the tests |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 112 | namespaceService.getChildren('/localhost:8881/house/kitchen'). |
| 113 | then(function assertResult(result) { |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 114 | // Wait until we receive the 2 items, lights and smoke-detector |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 115 | var numReturnedChildren; |
| 116 | result(function(children) { |
| 117 | numReturnedChildren = children.length; |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 118 | if (numReturnedChildren === 2) { |
| 119 | children = _.sortBy(children, 'mountedName'); |
| 120 | assertLightSwitch(children[0]); |
| 121 | assertSmokeDetector(children[1]); |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 122 | t.end(); |
| 123 | } |
| 124 | }); |
| 125 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 126 | |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 127 | // 9 assertions |
| 128 | function assertLightSwitch(item) { |
| 129 | t.equal(item.mountedName, 'lights'); |
| 130 | t.equal(item.objectName, '/localhost:8881/house/kitchen/lights'); |
| 131 | t.equal(item.isServer, true); |
| 132 | t.equal(item.isGlobbable, false); |
| 133 | |
| 134 | assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo); |
| 135 | |
| 136 | t.ok(item.serverInfo.signature); |
| 137 | } |
| 138 | |
| 139 | // 9 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 140 | function assertSmokeDetector(item) { |
| 141 | t.equal(item.mountedName, 'smoke-detector'); |
| 142 | t.equal(item.objectName, '/localhost:8881/house/kitchen/smoke-detector'); |
| 143 | t.equal(item.isServer, true); |
| 144 | t.equal(item.isGlobbable, false); |
| 145 | |
| 146 | assertUnknownServiceTypeInfo(t, item.serverInfo.typeInfo); |
| 147 | |
| 148 | t.ok(item.serverInfo.signature); |
| 149 | } |
| 150 | }); |
| 151 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 152 | test('getChildren of non-existing mounttable', function(t) { |
| 153 | t.plan(1); |
| 154 | |
| 155 | // Should get an error |
| 156 | namespaceService.getChildren('/DoesNotExist:666/What/Ever'). |
| 157 | then(function shouldNotGetResult(result){ |
| 158 | t.fail('Should have returned an error instead of result'); |
| 159 | }). |
| 160 | catch(function assertThereIsError(err) { |
| 161 | t.ok(err); |
| 162 | }); |
| 163 | }); |
| 164 | |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 165 | test('glob uses caching', function(t) { |
| 166 | t.plan(3); |
| 167 | |
| 168 | mockLRUCache.reset(); |
| 169 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 170 | namespaceService.glob('house', '*'). |
| 171 | then(function assertNoCacheHit() { |
| 172 | // First time, no cache hit so a get call followed by a set call |
| 173 | t.notOk(mockLRUCache.lastCallWasCacheHit); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 174 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 175 | // Call second time, there should have been a cache hit |
| 176 | return namespaceService.glob('house', '*'); |
| 177 | }).then( function assertCacheHit() { |
| 178 | t.ok(mockLRUCache.lastCallWasCacheHit); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 179 | |
Ali Ghassemi | bbb2053 | 2014-09-22 15:05:58 -0700 | [diff] [blame] | 180 | // call glob with same name, different query, there should be no cache hit |
| 181 | return namespaceService.glob('house', 'foo*'); |
| 182 | }).then(function assertNoCacheHit() { |
| 183 | t.notOk(mockLRUCache.lastCallWasCacheHit); |
| 184 | }).catch(t.end); |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 185 | }); |
| 186 | |
| 187 | test('getSignature uses caching', function(t) { |
| 188 | t.plan(3); |
| 189 | |
| 190 | mockLRUCache.reset(); |
| 191 | |
| 192 | namespaceService.getSignature('house/alarm').then(function() { |
| 193 | // First time, no cache hit |
| 194 | t.notOk(mockLRUCache.lastCallWasCacheHit); |
| 195 | // Call a second time |
| 196 | return namespaceService.getSignature('house/alarm'); |
| 197 | }).then(function() { |
| 198 | // Second time, must be a cache hit |
| 199 | t.ok(mockLRUCache.lastCallWasCacheHit); |
| 200 | // Call a different name |
| 201 | return namespaceService.getSignature('house/kitchen/smoke-detector'); |
| 202 | }).then(function() { |
| 203 | // Different name, no cache hit |
| 204 | t.notOk(mockLRUCache.lastCallWasCacheHit); |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | //TODO(aghassemi) |
| 209 | //Tests for: |
| 210 | // Recursive glob |
| 211 | // Glob with some keyword |
| 212 | // Ensuring array is updated when nodes get mounted and unmounted (when we use |
| 213 | // watchGlob) |
| 214 | // makeRPC TODO(alexfandrianto) |
| 215 | |
| 216 | /* |
| 217 | * Test helpers |
| 218 | */ |
| 219 | |
| 220 | /* |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 221 | * Asserts that a ServiceTypeInfo is of predefined type of Unknown Service. |
| 222 | * 4 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 223 | */ |
| 224 | function assertUnknownServiceTypeInfo(t, typeInfo) { |
| 225 | t.equal(typeInfo.key, 'veyron-unknown'); |
| 226 | t.equal(typeInfo.typeName, 'Service'); |
| 227 | t.equal(typeInfo.description, null); |
| 228 | t.ok(typeInfo.icon); |
| 229 | } |
| 230 | |
| 231 | /* |
Alex Fandrianto | 4204a88 | 2014-09-19 13:34:32 -0700 | [diff] [blame] | 232 | * Asserts that a ServiceTypeInfo is of predefined type of mounttable. |
| 233 | * 4 assertions |
Ali Ghassemi | fcab3c3 | 2014-09-12 14:24:12 -0700 | [diff] [blame] | 234 | */ |
| 235 | function assertMounttableServiceTypeInfo(t, typeInfo) { |
| 236 | t.equal(typeInfo.key, 'veyron-mounttable'); |
| 237 | t.equal(typeInfo.typeName, 'Mount Table'); |
| 238 | t.ok(typeInfo.description); |
| 239 | t.ok(typeInfo.icon); |
| 240 | } |