Skip to content

Commit

Permalink
fix(registrar): PR comments and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 22, 2019
1 parent 772e907 commit 41725bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
21 changes: 13 additions & 8 deletions more/registrar/registrar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright (C) 2019 Agoric, under Apache License 2.0

import harden from "@agoric/harden";
import { insist } from "../../util/insist";
import { generateSparseInts } from "../../util/sparseInts";
import harden from '@agoric/harden';
import { insist } from '../../util/insist';
import { generateSparseInts } from '../../util/sparseInts';

// Example REPL imnvocations
// home.registrar~.register('handoff', home.handoffService)
// for (let i = 0; i < 3000; i++) { home.registrar~.register('handoff', home.handoffService); }

const minimumDigits = 4;

// Generated keys must end with _ and digits.
const keyFormat = new RegExp('.*_[0-9]+');
const keyFormat = new RegExp(`.*_\\d{${minimumDigits},}$`);

function makeRegistrar(systemVersion, seed = 0) {
// TODO make a better token algorithm.
Expand All @@ -23,7 +25,10 @@ function makeRegistrar(systemVersion, seed = 0) {
const realName = name.toLowerCase();
const useCount = (useCounts.get(realName) || 0) + 1;
useCounts.set(realName, useCount);
const depth = Math.max(4, Math.floor(Math.log10(useCount) + 1.6));
const depth = Math.max(
minimumDigits,
Math.floor(Math.log10(useCount) + 1.6),
);

// Retry until we have a unique key.
let key;
Expand All @@ -34,22 +39,22 @@ function makeRegistrar(systemVersion, seed = 0) {
key = `${realName}_${keyString}`;
} while (contents.has(key));

contents.set(key, value);
contents.set(key, harden(value));
return key;
},
get(key, version = null) {
insist(typeof key === 'string')`\
Key must be string ${key}`;
insist(keyFormat.test(key))`\
Key must end with _<digits> ${key}`
Key must end with _<digits> ${key}`;
if (version) {
insist(version === systemVersion)`\
Key is from incompatible version: ${version} should be ${systemVersion}`;
}
return contents.get(key);
},
keys() {
return contents.keys();
return harden([...contents.keys()]);
},
});

Expand Down
38 changes: 33 additions & 5 deletions test/unitTests/more/registrar/test-registrar.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { test } from 'tape-promise/tape';
import { makeRegistrar } from '../../../../more/registrar/registrar';

test('Registrar creation', async t => {
test('Registrar operations', async t => {
try {
const registrarService = makeRegistrar('testnet');
const obj1 = {};
const obj2 = {};
const id1 = registrarService.register('myname', obj1);
t.assert(id1.match(/^myname_\d{4,}$/), 'id1 is correct format')
t.assert(id1.match(/^myname_\d{4,}$/), 'id1 is correct format');
const id2 = registrarService.register('myname', obj2);
t.assert(id2.match(/^myname_\d{4,}$/), 'id2 is correct format')
t.assert(id2.match(/^myname_\d{4,}$/), 'id2 is correct format');
t.isNot(id2, id1, 'ids for different objects are different');
const id1a = registrarService.register('myname', obj1);
t.assert(id1a.match(/^myname_\d{4,}$/), 'id1a is correct format')
t.assert(id1a.match(/^myname_\d{4,}$/), 'id1a is correct format');
t.isNot(id1a, id1, 'ids for same object are different');
const id1b = registrarService.register('othername', obj1);
t.assert(id1b.match(/^othername_\d{4,}$/), 'id1b is correct format')
t.assert(id1b.match(/^othername_\d{4,}$/), 'id1b is correct format');
const ret1 = registrarService.get(id1);
t.equals(ret1, obj1, 'returned obj1 is equal');
const ret2 = registrarService.get(id2);
Expand All @@ -24,6 +24,34 @@ test('Registrar creation', async t => {
t.equals(ret1a, obj1, 'returned obj1a is equal');
const ret1b = registrarService.get(id1b);
t.equals(ret1b, obj1, 'returned obj1b is equal');

t.equals(registrarService.keys().length, 4, 'number of keys is expected');
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
});

test.only('Registrar collisions', async t => {
try {
const registrarService = makeRegistrar('collide');
const iterations = 3000;
const myobj = {};
let maxlength = Number.NEGATIVE_INFINITY;
for (let i = 0; i < iterations; i += 1) {
const id = registrarService.register('a', myobj);
maxlength = Math.max(maxlength, id.length);
}
t.equals(maxlength, 7, 'expected maximum key length');

const keys = registrarService.keys();
t.equals(keys.length, iterations, 'expected number of keys');
t.equals(
keys.filter(key => registrarService.get(key) !== myobj).length,
0,
'expected no deviations',
);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
Expand Down
1 change: 0 additions & 1 deletion util/sparseInts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (C) 2019 Agoric, under Apache License 2.0

import harden from '@agoric/harden';
import { insist } from "./insist";

// Generator function to produce a stream of positive integers that are
// sparsely scattered across the number space. This supports IDs that
Expand Down

0 comments on commit 41725bf

Please sign in to comment.