Skip to content

Commit

Permalink
refactor: simply use of sinon fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 20, 2024
1 parent 8cf3b20 commit 0812d8d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 103 deletions.
2 changes: 1 addition & 1 deletion test/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('proxy', function () {
let targetPort: number

beforeEach(function (done) {
clock = sinon.useFakeTimers()
clock = sinon.useFakeTimers({ shouldClearNativeTimers: true })
port = nextPort()
server = createServer({
proxy: true
Expand Down
56 changes: 2 additions & 54 deletions test/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useFakeTimers } from 'sinon'
import BufferListStream from 'bl'
import OutgoingMessage from '../lib/outgoing_message'
import { AddressInfo } from 'net'
const originalSetImmediate = setImmediate

describe('request', function () {
let server: Socket | Server | null
Expand All @@ -28,7 +27,7 @@ describe('request', function () {
port = nextPort()
server = createSocket('udp4')
server.bind(port, done)
clock = useFakeTimers()
clock = useFakeTimers({ shouldClearNativeTimers: true })
})

afterEach(function () {
Expand All @@ -48,7 +47,7 @@ describe('request', function () {
function fastForward (increase, max): void {
clock.tick(increase)
if (increase < max) {
originalSetImmediate(fastForward.bind(null, increase, max - increase))
fastForward(increase, max - increase)
}
}

Expand Down Expand Up @@ -1022,30 +1021,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 @@ -1135,30 +1117,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 @@ -1627,23 +1592,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
42 changes: 12 additions & 30 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import { request, createServer } from '../index'
import { createSocket } from 'dgram'
import BufferListStream = require('bl')
import tk from 'timekeeper'
import sinon from 'sinon'
import sinon, { useFakeTimers } from 'sinon'
import { EventEmitter } from 'events'
import { parameters } from '../lib/parameters'
import IncomingMessage from '../lib/incoming_message'

const originalSetImmediate = setImmediate

describe('server', function () {
let server,
port,
Expand All @@ -32,6 +30,7 @@ describe('server', function () {
port = nextPort()
server = createServer()
server.listen(port, done)
clock = useFakeTimers({ shouldClearNativeTimers: true })
})

beforeEach(function (done) {
Expand All @@ -53,10 +52,10 @@ describe('server', function () {
client.send(message, 0, message.length, port, '127.0.0.1')
}

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

Expand Down Expand Up @@ -595,8 +594,6 @@ describe('server', function () {
})

it('should not retry sending the response', function (done) {
clock = sinon.useFakeTimers()

let messages = 0

send(generate(packet))
Expand Down Expand Up @@ -655,8 +652,6 @@ describe('server', function () {
})

it('should reply with a confirmable after an ack', function (done) {
clock = sinon.useFakeTimers()

send(generate(packet))
server.on('request', (req, res) => {
setTimeout(() => {
Expand All @@ -681,8 +676,6 @@ describe('server', function () {
})

it('should retry sending the response if it does not receive an ack four times before 45s', function (done) {
clock = sinon.useFakeTimers()

let messages = 0

send(generate(packet))
Expand All @@ -703,7 +696,8 @@ describe('server', function () {
// original one plus 4 retries
expect(messages).to.eql(5)
} catch (err) {
return done(err)
done(err)
return
}
done()
}, 45 * 1000)
Expand All @@ -712,8 +706,6 @@ describe('server', function () {
})

it('should stop resending after it receives an ack', function (done) {
clock = sinon.useFakeTimers()

let messages = 0

send(generate(packet))
Expand Down Expand Up @@ -744,8 +736,6 @@ describe('server', function () {
})

it('should not resend with a piggyback response', function (done) {
clock = sinon.useFakeTimers()

let messages = 0

send(generate(packet))
Expand All @@ -766,8 +756,6 @@ describe('server', function () {
})

it('should error if it does not receive an ack four times before ~247s', function (done) {
clock = sinon.useFakeTimers()

send(generate(packet))
server.on('request', (req, res) => {
// needed to avoid sending a piggyback response
Expand Down Expand Up @@ -860,7 +848,7 @@ describe('server', function () {

server.on('request', (req, res) => {
res.write('hello')
originalSetImmediate(function () {
setImmediate(function () {
res.end('world')
})
})
Expand Down Expand Up @@ -889,8 +877,6 @@ describe('server', function () {
})

it('should emit a \'finish\' if the client do not ack for ~247s', function (done) {
clock = sinon.useFakeTimers()

doObserve()

server.on('request', (req, res) => {
Expand Down Expand Up @@ -956,9 +942,7 @@ describe('server', function () {
res._counter = 4242

res.write('hello')
originalSetImmediate(function () {
res.end('world')
})
res.end('world')
})

// the first one is an ack
Expand All @@ -980,9 +964,7 @@ describe('server', function () {
res._counter = 65535

res.write('hello')
originalSetImmediate(function () {
res.end('world')
})
res.end('world')
})

// the first one is an ack
Expand Down Expand Up @@ -1194,7 +1176,7 @@ describe('server LRU', function () {
}

beforeEach(function (done) {
clock = sinon.useFakeTimers()
clock = useFakeTimers({ shouldClearNativeTimers: true })
port = nextPort()
server = createServer()
server.listen(port, done)
Expand Down Expand Up @@ -1249,7 +1231,7 @@ describe('server block cache', function () {
}

beforeEach(function (done) {
clock = sinon.useFakeTimers()
clock = useFakeTimers({ shouldClearNativeTimers: true })
port = nextPort()
server = createServer()
server.listen(port, done)
Expand Down Expand Up @@ -1310,7 +1292,7 @@ describe('Client Identifier', function () {
}

beforeEach(function (done) {
clock = sinon.useFakeTimers()
clock = useFakeTimers({ shouldClearNativeTimers: true })
port = nextPort()

server = createServer({
Expand Down
Loading

0 comments on commit 0812d8d

Please sign in to comment.