Skip to content

Conversation

@erickoledadevrel
Copy link
Contributor

Fixes #105

@erickoledadevrel
Copy link
Contributor Author

Just realized I've only covered hasAccess(), and there is still a potential race condition in refresh(). Stay tuned...

@erickoledadevrel
Copy link
Contributor Author

@grant Can you please review this PR?

Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending this to me. I added some comments below.

src/Service.gs Outdated
Service_.EXPIRATION_BUFFER_SECONDS_ = 60;

/**
* The number of seconds that a token should remain in the cache.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not seconds, milliseconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

};

/**
* Sets the lock to use when checking and refreshing credentials (optional).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of:

Sets the lock ... (optional).

say:

Sets an optional lock ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is already used extensively in the library, don't see a lot of value in switching.

* Using a lock will ensure that only one execution will be able to access the
* stored credentials at a time. This can prevent race conditions that arise
* when two executions attempt to refresh an expired token.
* @param {LockService.Lock} lock The lock to use when accessing credentials.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add:

@see https://developers.google.com/apps-script/reference/lock/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Service.gs Outdated
};

/**
* Locks access to a block of code, if a lock has been set on this service.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove awkward ",".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

var waitingFibers = [];

var MockLock = function() {
this.gotLock = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually these boolean are written has* as in hasLock or hasOwnProperty. gotLock sounds awkward.
See: https://google.github.io/styleguide/jsguide.html#naming-method-names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasLock is already defined, and one of the methods it's mocking, hence my search for another name. Replaced with "hasLock_".

test/test.js Outdated
expires_in: 100,
refresh_token: 'bar'
};
var properties = new MockProperties();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than declaring:

var properties = new MockProperties();
properties.setProperty(...)

twice, can you allow MockProperties to accept default properties? It would make sense and is small 2 line change.

var MockProperties = function(store) {
  this.store = store || {};
  this.counter = 0;
};

then create MockProperties.

var properties = new MockProperties({'oauth2.test': JSON.stringify(token)});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Switched to using it in most cases, but not all.

test/test.js Outdated
};

var refreshToken = function() {
var service = OAuth2.createService('test')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This same exact service is being created twice in this file on line 157. Extract this into function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more coincidental than anything, I'd rather have it defined locally for clarity.

package.json Outdated
"gas-local": "^1.3.0",
"gulp": "^3.9.0",
"gulp": "^3.9.1",
"gulp-bump": "^0.3.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're cleaning up deps, gulp-bump doesn't seem to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

src/Service.gs Outdated
throw new Error('Offline access is required.');
}
var headers = {
'Accept': this.tokenFormat_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@erickoledadevrel erickoledadevrel Mar 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, although the rule is about mixing them, not about not having them at all. I think quotes may actually be more appropriate here, since it represents a dict.

this.resultFunction = () => '';
};

MockUrlFetchApp.prototype.fetch = function(url, optOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please document these methods. I don't know what these methods (giving 0 and '') do.
  • The params don't seem to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are mocking out the equivalent functions in UrlFetchApp, added a note. I decided to keep the same method signature, even though this implementation currently isn't use those parameters.

@erickoledadevrel
Copy link
Contributor Author

Thanks for the review, PTAL.

Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@erickoledadevrel erickoledadevrel merged commit c08ae9a into master Mar 10, 2018
@erickoledadevrel erickoledadevrel deleted the locking branch March 21, 2018 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants