Skip to content

Commit

Permalink
refactor: refactor test code using sinon
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 21, 2024
1 parent 4a14510 commit bcef9b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 65 deletions.
51 changes: 0 additions & 51 deletions test/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,30 +1024,13 @@ describe('request', function () {
})

describe('non-confirmable retries', function () {
let clock

beforeEach(function () {
clock = useFakeTimers()
})

afterEach(function () {
clock.restore()
})

function doReq (): OutgoingMessage {
return request({
port,
confirmable: false
}).end()
}

function fastForward (increase, max): void {
clock.tick(increase)
if (increase < max) {
originalSetImmediate(fastForward.bind(null, increase, max - increase))
}
}

it('should timeout after ~202 seconds', function (done) {
const req = doReq()

Expand Down Expand Up @@ -1137,30 +1120,13 @@ describe('request', function () {
})

describe('confirmable retries', function () {
let clock

beforeEach(function () {
clock = useFakeTimers()
})

afterEach(function () {
clock.restore()
})

function doReq (): OutgoingMessage {
return request({
port,
confirmable: true
}).end()
}

function fastForward (increase, max): void {
clock.tick(increase)
if (increase < max) {
originalSetImmediate(fastForward.bind(null, increase, max - increase))
}
}

it('should error after ~247 seconds', function (done) {
const req = doReq()

Expand Down Expand Up @@ -1632,23 +1598,6 @@ describe('request', function () {
})

describe('token', function () {
let clock

beforeEach(function () {
clock = useFakeTimers()
})

afterEach(function () {
clock.restore()
})

function fastForward (increase, max): void {
clock.tick(increase)
if (increase < max) {
originalSetImmediate(fastForward.bind(null, increase, max - increase))
}
}

it('should timeout if the response token size doesn\'t match the request\'s', function (done) {
const req = request({
port
Expand Down
7 changes: 2 additions & 5 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('server', function () {
})

afterEach(function () {
if (clock != null) {
clock.restore()
}
clock?.restore()
client.close()
server.close()
tk.reset()
Expand Down Expand Up @@ -1253,7 +1251,6 @@ describe('server block cache', function () {
}

beforeEach(function (done) {
clock = sinon.useFakeTimers()
port = nextPort()
server = createServer()
server.listen(port, done)
Expand All @@ -1266,7 +1263,7 @@ describe('server block cache', function () {
})

afterEach(function () {
clock.restore()
clock?.restore()
client.close()
server.close()
tk.reset()
Expand Down
19 changes: 10 additions & 9 deletions test/share-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ describe('share-socket', function () {
let server: Server
let port: number
let originalGlobalAgent: Agent
let clock: sinon.SinonFakeTimers

beforeEach(function (done) {
clock = sinon.useFakeTimers()
port = nextPort()
server = createServer()
originalGlobalAgent = globalAgent
Expand All @@ -41,6 +43,7 @@ describe('share-socket', function () {
})

afterEach(function (done) {
clock.restore()
this.timeout(500)
setTimeout(() => {
server.close(done)
Expand All @@ -57,15 +60,15 @@ describe('share-socket', function () {
request(`coap://localhost:${port}/abcd/ef/gh/?foo=bar&beep=bop`).end()
server.on('request', (req) => {
expect(req.url).to.eql('/abcd/ef/gh?foo=bar&beep=bop')
setImmediate(done)
done()
})
})

it('should return code 2.05 by default', function (done) {
const req = request(`coap://localhost:${port}/abcd/ef/gh/?foo=bar&beep=bop`).end()
req.on('response', (res: IncomingMessage) => {
expect(res.code).to.eql('2.05')
setImmediate(done)
done()
})

server.on('request', (req, res) => {
Expand All @@ -77,7 +80,7 @@ describe('share-socket', function () {
request(`coap://localhost:${port}`)
.on('response', (res: IncomingMessage) => {
expect(res.code).to.eql('4.04')
setImmediate(done)
done()
})
.end()

Expand All @@ -91,7 +94,7 @@ describe('share-socket', function () {
request(`coap://localhost:${port}`)
.on('response', (res: IncomingMessage) => {
expect(res.code).to.eql('4.04')
setImmediate(done)
done()
})
.end()

Expand Down Expand Up @@ -334,9 +337,9 @@ describe('share-socket', function () {

server.on('request', (req, res) => {
res.write('hello')
setTimeout(() => {
originalSetImmediate(() => {
res.end('world')
}, 10)
})
})

;[req1, req2].forEach((req) => {
Expand Down Expand Up @@ -400,7 +403,7 @@ describe('share-socket', function () {
})

req1.on('response', () => {
setImmediate(() => {
originalSetImmediate(() => {
request({
port,
method: 'GET',
Expand Down Expand Up @@ -438,7 +441,6 @@ describe('share-socket', function () {
})

it('should error after ~247 seconds', function (done) {
const clock = sinon.useFakeTimers()
const req = request(`coap://localhost:${port + 1}`)
req.end()

Expand All @@ -451,7 +453,6 @@ describe('share-socket', function () {

req.on('error', (err) => {
expect(err).to.have.property('message', 'No reply in 247 seconds.')
clock.restore()
done()
})

Expand Down

0 comments on commit bcef9b0

Please sign in to comment.