Skip to content

Commit

Permalink
Additional changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Koleda committed Mar 10, 2018
1 parent 08d9d45 commit d8f4df8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
},
"rules": {
"comma-dangle": "off",
"no-var": "off"
"no-var": "off",
"generator-star-spacing": ["error", {"anonymous": "neither"}],
}
};
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -20,7 +20,6 @@
"eslint-config-google": "^0.9.1",
"gas-local": "^1.3.0",
"gulp": "^3.9.1",
"gulp-bump": "^0.3.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.1",
"gulp-eslint": "^4.0.2",
Expand Down
9 changes: 6 additions & 3 deletions src/Service.gs
Expand Up @@ -46,7 +46,7 @@ var Service_ = function(serviceName) {
Service_.EXPIRATION_BUFFER_SECONDS_ = 60;

/**
* The number of seconds that a token should remain in the cache.
* The number of milliseconds that a token should remain in the cache.
* @type {number}
* @private
*/
Expand Down Expand Up @@ -181,6 +181,7 @@ Service_.prototype.setClientSecret = function(clientSecret) {
* @param {PropertiesService.Properties} propertyStore The property store to use
* when persisting credentials.
* @return {Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/properties/
*/
Service_.prototype.setPropertyStore = function(propertyStore) {
this.propertyStore_ = propertyStore;
Expand All @@ -195,6 +196,7 @@ Service_.prototype.setPropertyStore = function(propertyStore) {
* @param {CacheService.Cache} cache The cache to use when persisting
* credentials.
* @return {Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/cache/
*/
Service_.prototype.setCache = function(cache) {
this.cache_ = cache;
Expand All @@ -208,6 +210,7 @@ Service_.prototype.setCache = function(cache) {
* when two executions attempt to refresh an expired token.
* @param {LockService.Lock} lock The lock to use when accessing credentials.
* @return {Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/lock/
*/
Service_.prototype.setLock = function(lock) {
this.lock_ = lock;
Expand Down Expand Up @@ -510,7 +513,7 @@ Service_.prototype.refresh = function() {
throw new Error('Offline access is required.');
}
var headers = {
'Accept': this.tokenFormat_
Accept: this.tokenFormat_
};
if (this.tokenHeaders_) {
headers = extend_(headers, this.tokenHeaders_);
Expand Down Expand Up @@ -661,7 +664,7 @@ Service_.prototype.createJwt_ = function() {
};

/**
* Locks access to a block of code, if a lock has been set on this service.
* Locks access to a block of code if a lock has been set on this service.
* @param {function} func The code to execute.
* @return {*} The result of the code block.
* @private
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/cache.js
@@ -1,5 +1,5 @@
var MockCache = function() {
this.store = {};
var MockCache = function(optCache) {
this.store = optCache || {};
this.counter = 0;
};

Expand Down
22 changes: 12 additions & 10 deletions test/mocks/lock.js
@@ -1,42 +1,44 @@
/**
* @file Mocks out Apps Script's LockService.Lock, using the fibers library to
* emulate concurrent executions. Like Apps Script's locks, only one execution
* can hold the lock at a time.
*/

var Fiber = require('fibers');

var locked = false;
var waitingFibers = [];

var MockLock = function() {
this.gotLock = false;
this.hasLock_ = false;
this.id = Math.random();
};

MockLock.prototype.waitLock = function(timeoutInMillis) {
var start = new Date();
do {
if (!locked || this.gotLock) {
if (!locked || this.hasLock_) {
locked = true;
this.gotLock = true;
this.hasLock_ = true;
return;
} else {
waitingFibers.push(Fiber.current);
Fiber.yield();
}
} while (timeDiffInMillis(new Date(), start) < timeoutInMillis);
} while (new Date().getTime() - start.getTime() < timeoutInMillis);
throw new Error('Unable to get lock');
};

MockLock.prototype.releaseLock = function() {
locked = false;
this.gotLock = false;
this.hasLock_ = false;
if (waitingFibers.length) {
waitingFibers.pop().run();
}
};

MockLock.prototype.hasLock = function() {
return this.gotLock;
return this.hasLock_;
};

function timeDiffInMillis(a, b) {
return a.getTime() - b.getTime();
}

module.exports = MockLock;
4 changes: 2 additions & 2 deletions test/mocks/properties.js
@@ -1,5 +1,5 @@
var MockProperties = function() {
this.store = {};
var MockProperties = function(optStore) {
this.store = optStore || {};
this.counter = 0;
};

Expand Down
7 changes: 6 additions & 1 deletion test/mocks/urlfetchapp.js
@@ -1,3 +1,8 @@
/**
* @file Mocks out Apps Script's UrlFetchApp, using the fibers library to
* emulate concurrent executions.
*/

var Future = require('fibers/future');

var MockUrlFetchApp = function() {
Expand All @@ -18,7 +23,7 @@ MockUrlFetchApp.prototype.fetch = function(url, optOptions) {
};

function sleep(ms) {
var future = new Future;
var future = new Future();
setTimeout(function() {
future.return();
}, ms);
Expand Down
63 changes: 31 additions & 32 deletions test/test.js
Expand Up @@ -47,44 +47,45 @@ describe('Service', function() {
});

it('should load from the cache', function() {
var cache = new MockCache();
var service = OAuth2.createService('test')
.setPropertyStore(new MockProperties())
.setCache(cache);
var token = {
access_token: 'foo'
};
cache.put('oauth2.test', JSON.stringify(token));
var cache = new MockCache({
'oauth2.test': JSON.stringify(token)
});
var service = OAuth2.createService('test')
.setPropertyStore(new MockProperties())
.setCache(cache);
assert.deepEqual(service.getToken(), token);
});

it('should load from the properties and set the cache', function() {
var token = {
access_token: 'foo'
};
var cache = new MockCache();
var properties = new MockProperties();
var properties = new MockProperties({
'oauth2.test': JSON.stringify(token)
});
var service = OAuth2.createService('test')
.setPropertyStore(properties)
.setCache(cache);
var key = 'oauth2.test';
var token = {
access_token: 'foo'
};
properties.setProperty(key, JSON.stringify(token));

assert.deepEqual(service.getToken(), token);
assert.deepEqual(JSON.parse(cache.get(key)), token);
assert.deepEqual(JSON.parse(cache.get('oauth2.test')), token);
});

it('should not hit the cache or properties on subsequent calls',
function() {
var cache = new MockCache();
var properties = new MockProperties();
var properties = new MockProperties({
'oauth2.test': JSON.stringify({
access_token: 'foo'
})
});
var service = OAuth2.createService('test')
.setPropertyStore(properties)
.setCache(cache);
var key = 'oauth2.test';
var token = {
access_token: 'foo'
};
properties.setProperty(key, JSON.stringify(token));

service.getToken();
var cacheStart = cache.counter;
Expand Down Expand Up @@ -144,14 +145,14 @@ describe('Service', function() {
expires_in: 100,
refresh_token: 'bar'
};
var properties = new MockProperties();
properties.setProperty('oauth2.test', JSON.stringify(token));
var properties = new MockProperties({
'oauth2.test': JSON.stringify(token)
});

mocks.UrlFetchApp.delayFunction = () => 100;
mocks.UrlFetchApp.resultFunction = () =>
JSON.stringify({
access_token: Math.random().toString(36)
});
mocks.UrlFetchApp.resultFunction = () => JSON.stringify({
access_token: Math.random().toString(36)
});

var getAccessToken = function() {
var service = OAuth2.createService('test')
Expand Down Expand Up @@ -197,8 +198,9 @@ describe('Service', function() {
expires_in: 100,
refresh_token: 'bar'
};
var properties = new MockProperties();
properties.setProperty('oauth2.test', JSON.stringify(token));
var properties = new MockProperties({
'oauth2.test': JSON.stringify(token)
});

var count = 0;
mocks.UrlFetchApp.resultFunction = function() {
Expand All @@ -215,13 +217,13 @@ describe('Service', function() {
};

var refreshToken = function() {
var service = OAuth2.createService('test')
OAuth2.createService('test')
.setClientId('abc')
.setClientSecret('def')
.setTokenUrl('http://www.example.com')
.setPropertyStore(properties)
.setLock(new MockLock());
service.refresh();
.setLock(new MockLock())
.refresh();
}.future();

Future.task(function() {
Expand All @@ -239,11 +241,8 @@ describe('Service', function() {
});
});
});

});



describe('Utilities', function() {
describe('#extend_()', function() {
var extend_ = OAuth2.extend_;
Expand Down

0 comments on commit d8f4df8

Please sign in to comment.