@@ -4,11 +4,12 @@ import https from 'https';
4
4
import { promisify } from 'util' ;
5
5
import { readFile } from 'fs-extra' ;
6
6
import test from 'ava' ;
7
- import { isFunction , isPlainObject , inRange } from 'lodash' ;
7
+ import { inRange } from 'lodash' ;
8
8
import { stub , spy } from 'sinon' ;
9
9
import proxyquire from 'proxyquire' ;
10
10
import Proxy from 'proxy' ;
11
11
import serverDestroy from 'server-destroy' ;
12
+ import Octokit from '@octokit/rest' ;
12
13
import rateLimit from './helpers/rate-limit' ;
13
14
14
15
const getClient = proxyquire ( '../lib/get-client' , { './definitions/rate-limit' : rateLimit } ) ;
@@ -85,32 +86,15 @@ test.serial('Use a https proxy', async t => {
85
86
await promisify ( server . destroy ) . bind ( server ) ( ) ;
86
87
} ) ;
87
88
88
- test ( 'Wrap Octokit in a proxy' , t => {
89
- const github = getClient ( { githubToken : 'github_token' } ) ;
90
-
91
- t . true ( Reflect . apply ( Object . prototype . hasOwnProperty , github , [ 'repos' ] ) ) ;
92
- t . true ( isPlainObject ( github . repos ) ) ;
93
- t . true ( Reflect . apply ( Object . prototype . hasOwnProperty , github . repos , [ 'createRelease' ] ) ) ;
94
- t . true ( isFunction ( github . repos . createRelease ) ) ;
95
-
96
- t . true ( Reflect . apply ( Object . prototype . hasOwnProperty , github , [ 'search' ] ) ) ;
97
- t . true ( isPlainObject ( github . search ) ) ;
98
- t . true ( Reflect . apply ( Object . prototype . hasOwnProperty , github . search , [ 'issues' ] ) ) ;
99
- t . true ( isFunction ( github . search . issues ) ) ;
100
-
101
- t . falsy ( github . unknown ) ;
102
- } ) ;
103
-
104
89
test ( 'Use the global throttler for all endpoints' , async t => {
105
- const createRelease = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
106
- const createComment = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
107
- const issues = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
108
- const octokit = { repos : { createRelease} , issues : { createComment} , search : { issues} , authenticate : stub ( ) } ;
109
90
const rate = 150 ;
91
+
92
+ const octokit = new Octokit ( ) ;
93
+ octokit . hook . wrap ( 'request' , ( ) => Date . now ( ) ) ;
110
94
const github = proxyquire ( '../lib/get-client' , {
111
95
'@octokit/rest' : stub ( ) . returns ( octokit ) ,
112
96
'./definitions/rate-limit' : { RATE_LIMITS : { search : 1 , core : 1 } , GLOBAL_RATE_LIMIT : rate } ,
113
- } ) ( ) ;
97
+ } ) ( { githubToken : 'token' } ) ;
114
98
115
99
const a = await github . repos . createRelease ( ) ;
116
100
const b = await github . issues . createComment ( ) ;
@@ -132,16 +116,15 @@ test('Use the global throttler for all endpoints', async t => {
132
116
} ) ;
133
117
134
118
test ( 'Use the same throttler for endpoints in the same rate limit group' , async t => {
135
- const createRelease = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
136
- const createComment = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
137
- const issues = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
138
- const octokit = { repos : { createRelease} , issues : { createComment} , search : { issues} , authenticate : stub ( ) } ;
139
119
const searchRate = 300 ;
140
120
const coreRate = 150 ;
121
+
122
+ const octokit = new Octokit ( ) ;
123
+ octokit . hook . wrap ( 'request' , ( ) => Date . now ( ) ) ;
141
124
const github = proxyquire ( '../lib/get-client' , {
142
125
'@octokit/rest' : stub ( ) . returns ( octokit ) ,
143
126
'./definitions/rate-limit' : { RATE_LIMITS : { search : searchRate , core : coreRate } , GLOBAL_RATE_LIMIT : 1 } ,
144
- } ) ( ) ;
127
+ } ) ( { githubToken : 'token' } ) ;
145
128
146
129
const a = await github . repos . createRelease ( ) ;
147
130
const b = await github . issues . createComment ( ) ;
@@ -164,15 +147,15 @@ test('Use the same throttler for endpoints in the same rate limit group', async
164
147
} ) ;
165
148
166
149
test ( 'Use different throttler for read and write endpoints' , async t => {
167
- const createRelease = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
168
- const get = stub ( ) . callsFake ( ( ) => Date . now ( ) ) ;
169
- const octokit = { repos : { createRelease, get} , authenticate : stub ( ) } ;
170
150
const writeRate = 300 ;
171
151
const readRate = 150 ;
152
+
153
+ const octokit = new Octokit ( ) ;
154
+ octokit . hook . wrap ( 'request' , ( ) => Date . now ( ) ) ;
172
155
const github = proxyquire ( '../lib/get-client' , {
173
156
'@octokit/rest' : stub ( ) . returns ( octokit ) ,
174
157
'./definitions/rate-limit' : { RATE_LIMITS : { core : { write : writeRate , read : readRate } } , GLOBAL_RATE_LIMIT : 1 } ,
175
- } ) ( ) ;
158
+ } ) ( { githubToken : 'token' } ) ;
176
159
177
160
const a = await github . repos . get ( ) ;
178
161
const b = await github . repos . get ( ) ;
@@ -186,32 +169,32 @@ test('Use different throttler for read and write endpoints', async t => {
186
169
} ) ;
187
170
188
171
test ( 'Use the same throttler when retrying' , async t => {
172
+ const coreRate = 200 ;
173
+
189
174
// eslint-disable-next-line require-await
190
- const createRelease = stub ( ) . callsFake ( async ( ) => {
175
+ const request = stub ( ) . callsFake ( async ( ) => {
191
176
const err = new Error ( ) ;
192
177
err . time = Date . now ( ) ;
193
- err . code = 404 ;
178
+ err . status = 404 ;
194
179
throw err ;
195
180
} ) ;
196
-
197
- const octokit = { repos : { createRelease} , authenticate : stub ( ) } ;
198
- const coreRate = 200 ;
181
+ const octokit = new Octokit ( ) ;
182
+ octokit . hook . wrap ( 'request' , request ) ;
199
183
const github = proxyquire ( '../lib/get-client' , {
200
184
'@octokit/rest' : stub ( ) . returns ( octokit ) ,
201
185
'./definitions/rate-limit' : {
202
186
RETRY_CONF : { retries : 3 , factor : 1 , minTimeout : 1 } ,
203
187
RATE_LIMITS : { core : coreRate } ,
204
188
GLOBAL_RATE_LIMIT : 1 ,
205
189
} ,
206
- } ) ( ) ;
190
+ } ) ( { githubToken : 'token' } ) ;
207
191
208
192
await t . throws ( github . repos . createRelease ( ) ) ;
209
- t . is ( createRelease . callCount , 4 ) ;
210
193
211
- const { time : a } = await t . throws ( createRelease . getCall ( 0 ) . returnValue ) ;
212
- const { time : b } = await t . throws ( createRelease . getCall ( 1 ) . returnValue ) ;
213
- const { time : c } = await t . throws ( createRelease . getCall ( 2 ) . returnValue ) ;
214
- const { time : d } = await t . throws ( createRelease . getCall ( 3 ) . returnValue ) ;
194
+ const { time : a } = await t . throws ( request . getCall ( 0 ) . returnValue ) ;
195
+ const { time : b } = await t . throws ( request . getCall ( 1 ) . returnValue ) ;
196
+ const { time : c } = await t . throws ( request . getCall ( 2 ) . returnValue ) ;
197
+ const { time : d } = await t . throws ( request . getCall ( 3 ) . returnValue ) ;
215
198
216
199
// Each retry should be done after `coreRate` ms
217
200
t . true ( inRange ( b - a , coreRate - 50 , coreRate + 50 ) ) ;
0 commit comments