Skip to content

Commit

Permalink
feat: add timeouts to spark-api requests (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos authored Nov 20, 2023
1 parent 4c27d6b commit 3357243
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export default class Spark {
}

async getRetrieval () {
console.log('Getting retrieval...')
console.log('Getting current SPARK round details...')
const res = await this.#fetch('https://api.filspark.com/rounds/current', {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
headers: { 'Content-Type': 'application/json' },
signal: AbortSignal.timeout(10_000)
})
await assertOkResponse(res, 'Failed to fetch the current SPARK round')
this.#activity.onHealthy()
Expand Down Expand Up @@ -116,7 +117,8 @@ export default class Spark {
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
},
signal: AbortSignal.timeout(10_000)
})
await assertOkResponse(res, 'Failed to submit measurement')
const { id } = await res.json()
Expand Down
6 changes: 4 additions & 2 deletions test/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ test('getRetrieval', async () => {
]
}
const requests = []
const fetch = async (url, opts) => {
const fetch = async (url, allOpts) => {
const { signal, ...opts } = allOpts
requests.push({ url, opts })
return {
status: 200,
Expand Down Expand Up @@ -112,7 +113,8 @@ test('fetchCAR exceeding MAX_CAR_SIZE', async () => {

test('submitRetrieval', async () => {
const requests = []
const fetch = async (url, opts) => {
const fetch = async (url, allOpts) => {
const { signal, ...opts } = allOpts
requests.push({ url, opts })
return { status: 200, ok: true, async json () { return { id: 123 } } }
}
Expand Down

0 comments on commit 3357243

Please sign in to comment.