Skip to content

Commit

Permalink
✔️ test: improved test for expiration, timeout and delays on channel …
Browse files Browse the repository at this point in the history
…operations

Signed-off-by: Marco Aurélio da Silva <marcoonroad@gmail.com>
  • Loading branch information
marcoonroad committed Feb 17, 2019
1 parent 358f88f commit 6b98c70
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
40 changes: 35 additions & 5 deletions __tests__/channels-plus.js
Expand Up @@ -2,12 +2,18 @@

'use strict'

jest.useFakeTimers()

const sporadic = require('../support').sporadic
const { open, send, receive, sendAfter, receiveAfter } = sporadic.channels
const timeoutError = { message: 'Timeout while listening channel!' }

beforeEach(() => {
jest.useFakeTimers()
})

afterEach(() => {
jest.clearAllTimers()
})

it('should handle timeouts on receive calls', async () => {
expect.assertions(6)

Expand Down Expand Up @@ -50,9 +56,33 @@ it('should handle expirations on send calls', async () => {
await expect(result1).resolves.toBe('Hi Mia!')
})

/*
it('should mix both timeouts & expirations', async () => {
it('should mix timeouts, expirations & delays', async () => {
const channel = await open()

const result1 = receiveAfter(1500, channel)
const wasReceived1 = sendAfter(3000, channel, 'Hey you!', 2000)
const result2 = receive(channel, 2500)
const result3 = receiveAfter(500, channel, 2000)
const wasReceived2 = sendAfter(5000, channel, 'Oh no!')
const wasReceived3 = sendAfter(10000, channel, 'No way!', 1000)
const result4 = receiveAfter(5000, channel, 2000)

jest.advanceTimersByTime(50) // 0 = t
jest.advanceTimersByTime(500) // 500 = t
jest.advanceTimersByTime(1000) // 1500 = t
jest.advanceTimersByTime(1000) // 2500 = t
await expect(result2).rejects.toMatchObject(timeoutError)
await expect(result3).rejects.toMatchObject(timeoutError)

jest.advanceTimersByTime(500) // 3000 = t
await expect(result1).resolves.toBe('Hey you!')
await expect(wasReceived1).resolves.toBe(true)

jest.advanceTimersByTime(2000) // 5000 = t
await expect(result4).resolves.toBe('Oh no!')
await expect(wasReceived2).resolves.toBe(true)

jest.advanceTimersByTime(5000) // 10000 = t
jest.advanceTimersByTime(1500) // 11500 = t
await expect(wasReceived3).resolves.toBe(false)
})
*/
4 changes: 2 additions & 2 deletions build/sporadic/channels/index.js
Expand Up @@ -131,15 +131,15 @@ var closed = function closed(channel) {
var sendAfter = function sendAfter(delay, channel, message, expiration) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
_send(channel, message, expiration).then(resolve).catch(reject);
_send(channel, message, expiration).then(resolve, reject);
}, Math.floor(Math.max(0, delay)));
});
};

var receiveAfter = function receiveAfter(delay, channel, timeout) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
_receive(channel, timeout).then(resolve).catch(reject);
_receive(channel, timeout).then(resolve, reject);
}, Math.floor(Math.max(0, delay)));
});
};
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/sporadic/channels/index.js
Expand Up @@ -136,14 +136,14 @@ const closed = channel =>
const sendAfter = (delay, channel, message, expiration) =>
new Promise((resolve, reject) => {
setTimeout(() => {
send(channel, message, expiration).then(resolve).catch(reject)
send(channel, message, expiration).then(resolve, reject)
}, Math.floor(Math.max(0, delay)))
})

const receiveAfter = (delay, channel, timeout) =>
new Promise((resolve, reject) => {
setTimeout(() => {
receive(channel, timeout).then(resolve).catch(reject)
receive(channel, timeout).then(resolve, reject)
}, Math.floor(Math.max(0, delay)))
})

Expand Down

0 comments on commit 6b98c70

Please sign in to comment.