diff --git a/deploy/saneUrl/store.js b/deploy/saneUrl/store.js index 1cb6cb8dbbff9b22495eb0ba64dc74684f476dd9..9765a6181053223b891e05d5d1c04172b7ffa1b9 100644 --- a/deploy/saneUrl/store.js +++ b/deploy/saneUrl/store.js @@ -43,6 +43,7 @@ class Store { async getToken() { this.token = await this.wrapper.getScopedToken({ projectId: OBJ_STORAGE_PROJECT_ID }) + return this.token } get(id) { @@ -60,7 +61,7 @@ class Store { }) } - set(id, value) { + _set(id, value) { return new Promise((rs, rj) => { request.put(`${this.objStorateRootUrl}/${id}`, { headers: { @@ -75,6 +76,17 @@ class Store { }) } + async set(id, value) { + try { + const result = await this._set(id, value) + return result + } catch (e) { + await this.getToken() + const result = await this._set(id, value) + return result + } + } + async healthCheck(){ } diff --git a/deploy/saneUrl/store.spec.js b/deploy/saneUrl/store.spec.js index 32ce7c043c16dd25451167f1d3b182ba477f2bc8..fc9a37eba3827e61c1e9d507fed0f51577a0a91a 100644 --- a/deploy/saneUrl/store.spec.js +++ b/deploy/saneUrl/store.spec.js @@ -28,6 +28,10 @@ describe('> store.js', () => { expect(token).to.equal(fakeToken) }) + it('> spy gets reset', async () => { + expect(getTokenSpy.notCalled).to.be.true + }) + it('> get works', async () => { const scope = nock(objStorateRootUrl) .get(`/${objName}`) @@ -50,8 +54,22 @@ describe('> store.js', () => { }) await store.set(objName, objContent) + expect(scope.isDone()).to.be.true + }) + + it('> set retries if at first fails', async () => { + let index = 0 + const scope = nock(objStorateRootUrl) + .put(`/${objName}`) + .twice() + .reply((_uri, _reqBody, cb) => { + cb(null, [ index % 2 === 0 ? 401 : 200 ]) + index ++ + }) + await store.set(objName, objContent) expect(scope.isDone()).to.be.true + expect(getTokenSpy.called).to.be.true }) }) })