v1rtl
v1rtl•2y ago

error: BadResource: Bad resource ID

I'm writing tests for https://github.com/deno-libs/tinyhttp/pull/9.
import { describe, expect, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts'
import { App } from '../../app.ts'

describe('Request properties', () => {
it.only('should have default HTTP Request properties', async () => {
const app = new App()
app.get('/', (req, res) => {
res.json({ url: req.url })
})
const listener = Deno.listen({ port: 8080, hostname: 'localhost' })

setTimeout(() => {
fetch(`http://localhost:8080`).then((res) => res.json()).then((json) => {
expect(json).toEqual({ url: `http://localhost:8080/` })
})
}, 0)

const conn = await listener.accept()
const requests = Deno.serveHttp(conn)
const { request, respondWith } = (await requests.nextRequest())!
const response = await app.handler(request, conn)
if (response) {
respondWith(response)
// Deno.close(conn.rid)

listener.close()
}
})
})
import { describe, expect, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts'
import { App } from '../../app.ts'

describe('Request properties', () => {
it.only('should have default HTTP Request properties', async () => {
const app = new App()
app.get('/', (req, res) => {
res.json({ url: req.url })
})
const listener = Deno.listen({ port: 8080, hostname: 'localhost' })

setTimeout(() => {
fetch(`http://localhost:8080`).then((res) => res.json()).then((json) => {
expect(json).toEqual({ url: `http://localhost:8080/` })
})
}, 0)

const conn = await listener.accept()
const requests = Deno.serveHttp(conn)
const { request, respondWith } = (await requests.nextRequest())!
const response = await app.handler(request, conn)
if (response) {
respondWith(response)
// Deno.close(conn.rid)

listener.close()
}
})
})
When running this test I get either this:
Request properties > should have default HTTP Request properties => https://deno.land/x/tincan@1.0.1/src/runner.ts:166:10
error: BadResource: Bad resource ID
Deno.close(conn.rid)
^
at Object.close (ext:core/01_core.js:396:25)
at ItNode.fn (file:///home/v1rtl/Coding/deno-libs/tinyhttp/tests/core/request.test.ts:29:12)
at async wrappedFn (https://deno.land/x/tincan@1.0.1/src/runner.ts:141:9)
Request properties > should have default HTTP Request properties => https://deno.land/x/tincan@1.0.1/src/runner.ts:166:10
error: BadResource: Bad resource ID
Deno.close(conn.rid)
^
at Object.close (ext:core/01_core.js:396:25)
at ItNode.fn (file:///home/v1rtl/Coding/deno-libs/tinyhttp/tests/core/request.test.ts:29:12)
at async wrappedFn (https://deno.land/x/tincan@1.0.1/src/runner.ts:141:9)
or this, if I uncomment that line:
error: AssertionError: Test case is leaking 1 resource:
- An inbound HTTP connection (rid 8) was accepted during the test, but not closed during the test. Close the inbound HTTP connection by calling `httpConn.close()`.
at assert (ext:deno_web/00_infra.js:353:11)
at resourceSanitizer (ext:cli/40_testing.js:414:5)
at async Object.exitSanitizer [as fn] (ext:cli/40_testing.js:432:7)
error: AssertionError: Test case is leaking 1 resource:
- An inbound HTTP connection (rid 8) was accepted during the test, but not closed during the test. Close the inbound HTTP connection by calling `httpConn.close()`.
at assert (ext:deno_web/00_infra.js:353:11)
at resourceSanitizer (ext:cli/40_testing.js:414:5)
at async Object.exitSanitizer [as fn] (ext:cli/40_testing.js:432:7)
GitHub
Build software better, together
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 330 million projects.
8 Replies
v1rtl
v1rtl•2y ago
what I'm trying to do is to close the server once a request has been done
AapoAlas
AapoAlas•2y ago
conn.close() would be the normal API to close the connection, I think.
v1rtl
v1rtl•2y ago
conn.close emits the same error as Deno.close(conn.rid)
AapoAlas
AapoAlas•2y ago
Hmm... Maybe await the respondWith ... Not sure though.
v1rtl
v1rtl•2y ago
I fixed with Deno.close(conn.rid + 1)... weird but it works
AapoAlas
AapoAlas•2y ago
That sounds absolutely dirty, unsafe and unsound 😄 Not to kick you down of course, just that calculating another rid from one is probably really easy to break semi-randomly.
v1rtl
v1rtl•2y ago
well nothing else worked lol
AapoAlas
AapoAlas•2y ago
Yeah.