Skip to content

Commit

Permalink
Remove one dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Aug 25, 2024
1 parent 2e58fc6 commit 5b26c6f
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 135 deletions.
6 changes: 3 additions & 3 deletions add-sync-map/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
loguxProcessed,
loguxSubscribed
} from '@logux/actions'
import { delay } from 'nanodelay'
import { setTimeout } from 'node:timers/promises'
import { afterEach, expect, it } from 'vitest'

import {
Expand Down Expand Up @@ -446,15 +446,15 @@ it('does not load data on creating', async () => {
channel: 'comments/new',
type: 'logux/subscribe'
})
await delay(10)
await setTimeout(10)
expect(loaded).toBe(1)

await client.log.add({
channel: 'comments/new',
creating: true,
type: 'logux/subscribe'
})
await delay(10)
await setTimeout(10)
expect(loaded).toBe(1)
})

Expand Down
40 changes: 20 additions & 20 deletions base-server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
type TestLog,
TestTime
} from '@logux/core'
import { delay } from 'nanodelay'
import { restoreAll, spy, type Spy, spyOn } from 'nanospy'
import { readFileSync } from 'node:fs'
import http from 'node:http'
import https from 'node:https'
import { join } from 'node:path'
import { setTimeout } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'
import { afterEach, expect, it } from 'vitest'
import WebSocket from 'ws'
Expand Down Expand Up @@ -595,7 +595,7 @@ it('processes actions', async () => {
async process(ctx, action, meta) {
expect(meta.added).toEqual(1)
expect(ctx.isServer).toBe(true)
await delay(25)
await setTimeout(25)
processed.push(action)
}
})
Expand All @@ -608,7 +608,7 @@ it('processes actions', async () => {
await test.app.log.add({ type: 'FOO' }, { reasons: ['test'] })
expect(fired).toEqual([])
expect(test.app.log.entries()[0][1].status).toEqual('waiting')
await delay(30)
await setTimeout(30)
expect(test.app.log.entries()[0][1].status).toEqual('processed')
expect(processed).toEqual([{ type: 'FOO' }])
expect(fired).toEqual([{ type: 'FOO' }])
Expand All @@ -624,7 +624,7 @@ it('processes regex matching action', async () => {
async process(ctx, action, meta) {
expect(meta.added).toEqual(1)
expect(ctx.isServer).toBe(true)
await delay(25)
await setTimeout(25)
processed.push(action)
}
})
Expand All @@ -637,7 +637,7 @@ it('processes regex matching action', async () => {
await test.app.log.add({ type: 'ADD_TODO' }, { reasons: ['test'] })
expect(fired).toEqual([])
expect(test.app.log.entries()[0][1].status).toEqual('waiting')
await delay(30)
await setTimeout(30)
expect(test.app.log.entries()[0][1].status).toEqual('processed')
expect(processed).toEqual([{ type: 'ADD_TODO' }])
expect(fired).toEqual([{ type: 'ADD_TODO' }])
Expand Down Expand Up @@ -680,7 +680,7 @@ it('waits for last processing before destroy', async () => {
app.destroy().then(() => {
destroyed = true
})
await delay(1)
await setTimeout(1)

expect(destroyed).toBe(false)
expect(privateMethods(app).processing).toEqual(1)
Expand All @@ -689,7 +689,7 @@ it('waits for last processing before destroy', async () => {
expect(started).toEqual(1)
if (typeof process === 'undefined') throw new Error('process is not set')
process()
await delay(1)
await setTimeout(1)

expect(destroyed).toBe(true)
})
Expand All @@ -706,7 +706,7 @@ it('reports about error during action processing', async () => {
})

await test.app.log.add({ type: 'FOO' }, { reasons: ['test'] })
await delay(1)
await setTimeout(1)

expect(test.names).toEqual(['add', 'error', 'add'])
expect(test.reports[1]).toEqual([
Expand Down Expand Up @@ -920,7 +920,7 @@ it('checks channel access', async () => {
{ channel: 'user/10', type: 'logux/subscribe' },
{ id: '1 10:uuid 0' }
)
await delay(1)
await setTimeout(1)

expect(test.names).toEqual(['addClean', 'denied', 'addClean'])
expect(test.reports[1][1]).toEqual({ actionId: '1 10:uuid 0' })
Expand Down Expand Up @@ -1009,7 +1009,7 @@ it('subscribes clients', async () => {
{ channel: 'user/10', type: 'logux/subscribe' },
{ id: '1 10:a:uuid 0' }
)
await delay(1)
await setTimeout(1)
expect(events).toEqual(1)
expect(userSubsriptions).toEqual(1)
expect(test.names).toEqual(['addClean', 'subscribed', 'addClean'])
Expand All @@ -1032,7 +1032,7 @@ it('subscribes clients', async () => {
{ channel: 'posts', type: 'logux/subscribe' },
{ id: '2 10:a:uuid 0' }
)
await delay(1)
await setTimeout(1)

expect(events).toEqual(2)
expect(test.app.subscribers).toEqual({
Expand Down Expand Up @@ -1104,7 +1104,7 @@ it('subscribes clients with multiple filters', async () => {
{ channel: 'posts', filter: { category: 'b' }, type: 'logux/subscribe' },
{ id: '1 10:a:uuid 0' }
)
await delay(1)
await setTimeout(1)
expect(test.app.subscribers).toEqual({
posts: {
'10:a:uuid': {
Expand All @@ -1125,7 +1125,7 @@ it('subscribes clients with multiple filters', async () => {
{ channel: 'posts', filter: { category: 'b' }, type: 'logux/unsubscribe' },
{ id: '2 10:a:uuid 0' }
)
await delay(1)
await setTimeout(1)
expect(test.app.subscribers).toEqual({
posts: {
'10:a:uuid': {
Expand Down Expand Up @@ -1166,7 +1166,7 @@ it('cancels subscriptions on disconnect', async () => {
{ channel: 'test', type: 'logux/subscribe' },
{ id: '1 10:uuid 0' }
)
await delay(10)
await setTimeout(10)

expect(cancels).toEqual(1)
})
Expand All @@ -1191,7 +1191,7 @@ it('reports about errors during channel initialization', async () => {
{ channel: 'user/10', type: 'logux/subscribe' },
{ id: '1 10:uuid 0' }
)
await delay(1)
await setTimeout(1)

expect(test.names).toEqual([
'addClean',
Expand Down Expand Up @@ -1242,7 +1242,7 @@ it('loads initial actions during subscription', async () => {
{ channel: 'user/10', type: 'logux/subscribe' },
{ id: '1 10:uuid 0' }
)
await delay(1)
await setTimeout(1)
expect(userLoaded).toEqual(1)
expect(test.app.subscribers).toEqual({
'user/10': {
Expand All @@ -1256,7 +1256,7 @@ it('loads initial actions during subscription', async () => {
throw new Error('callback is not set')
}
initializating()
await delay(1)
await setTimeout(1)

expect(test.app.log.actions()).toEqual([
{ channel: 'user/10', type: 'logux/subscribe' },
Expand Down Expand Up @@ -1412,7 +1412,7 @@ it('has shortcuts for resend arrays', async () => {
}
]
])
await delay(10)
await setTimeout(10)
expect(test.app.log.entries()).toEqual([
[
{ type: 'A' },
Expand Down Expand Up @@ -1510,7 +1510,7 @@ it('subscribes clients manually', async () => {
})

app.subscribe('test:1:1', 'users/10')
await delay(10)
await setTimeout(10)
expect(app.subscribers).toEqual({
'users/10': {
'test:1:1': { filters: { '{}': true } }
Expand All @@ -1519,7 +1519,7 @@ it('subscribes clients manually', async () => {
expect(actions).toEqual([{ channel: 'users/10', type: 'logux/subscribed' }])

app.subscribe('test:1:1', 'users/10')
await delay(10)
await setTimeout(10)
expect(actions).toEqual([{ channel: 'users/10', type: 'logux/subscribed' }])
})

Expand Down
56 changes: 28 additions & 28 deletions bind-backend-proxy/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { delay } from 'nanodelay'
import http from 'node:http'
import { setTimeout } from 'node:timers/promises'
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from 'vitest'

import { BaseServer, TestServer, type TestServerOptions } from '../index.js'
Expand Down Expand Up @@ -135,7 +135,7 @@ let httpServer = http.createServer((req, res) => {
let id = `"authId":"${command.authId}"`
if (command.userId === '10' && command.token === 'good') {
res.write('[{"answer":"authent')
await delay(100)
await setTimeout(100)
res.end(`icated",${id},"subprotocol":"1.0.0"}]`)
} else if (command.userId === '30' && command.cookie.token === 'good') {
res.end(`[{"answer":"authenticated",${id},"subprotocol":"1.0.0"}]`)
Expand Down Expand Up @@ -163,7 +163,7 @@ let httpServer = http.createServer((req, res) => {
res.end(`[{"answer":"error",${id},"details":"stack"}]`)
} else if (command.action.type === 'PERROR') {
res.write(`[{"answer":"approved",${id}}`)
await delay(100)
await setTimeout(100)
res.end(`,{"answer":"error",${id},"details":"stack"}]`)
} else if (command.action.type === 'BROKEN1') {
res.end(`[{"answer":"approved",${id}}`)
Expand Down Expand Up @@ -191,9 +191,9 @@ let httpServer = http.createServer((req, res) => {
]`)
} else if (command.action.channel === 'a') {
res.write('[{"answer":"appro')
await delay(1)
await setTimeout(1)
res.write(`ved",${id}}`)
await delay(100)
await setTimeout(100)
res.write(
`,{"answer":"action",${id},` +
'"action":{"type":"a/load1"},"meta":{"user":10}}'
Expand All @@ -205,9 +205,9 @@ let httpServer = http.createServer((req, res) => {
res.end(`,{"answer":"processed",${id}}]`)
} else {
res.write('[{"answer":"appro')
await delay(1)
await setTimeout(1)
res.write(`ved",${id}}`)
await delay(100)
await setTimeout(100)
res.end(`,{"answer":"processed",${id}}]`)
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ it('reports about network errors', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'A' })
await delay(100)
await setTimeout(100)

expect(errors).toEqual(['connect ECONNREFUSED 127.0.0.1:7111'])
expect(app.log.actions()).toEqual([
Expand All @@ -320,7 +320,7 @@ it('reports bad HTTP answers', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'NO' })
await delay(100)
await setTimeout(100)

expect(errors).toEqual(['Backend responded with 404 code'])
expect(app.log.actions()).toEqual([
Expand Down Expand Up @@ -471,7 +471,7 @@ it('notifies about actions and subscriptions', async () => {
let client = await app.connect('10', { headers: { lang: 'fr' } })
client.log.add({ type: 'A' })
client.log.add({ channel: 'a', type: 'logux/subscribe' })
await delay(280)
await setTimeout(400)

expect(app.log.actions()).toEqual([
{ type: 'A' },
Expand Down Expand Up @@ -533,7 +533,7 @@ it('notifies about actions and subscriptions', async () => {
}
]
])
await delay(150)
await setTimeout(150)

expect(app.log.actions()).toEqual([
{ type: 'A' },
Expand Down Expand Up @@ -562,7 +562,7 @@ it('asks about action access', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'BAD' })
await delay(50)
await setTimeout(50)

expect(app.log.actions()).toEqual([
{
Expand All @@ -582,7 +582,7 @@ it('reacts on unknown action', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'UNKNOWN' })
await delay(100)
await setTimeout(100)
expect(app.log.actions()).toEqual([
{
action: { type: 'UNKNOWN' },
Expand All @@ -603,7 +603,7 @@ it('reacts on unknown channel', async () => {
})
let client = await app.connect('10')
client.log.add({ channel: 'unknown', type: 'logux/subscribe' })
await delay(100)
await setTimeout(100)
expect(app.log.actions()).toEqual([
{ channel: 'unknown', type: 'logux/subscribe' },
{
Expand All @@ -627,23 +627,23 @@ it('reacts on wrong backend answer', async () => {
let client = await app.connect('10')

client.log.add({ type: 'EMPTY' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN1' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN2' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN3' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN4' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN5' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN6' })
await delay(20)
await setTimeout(20)
client.log.add({ type: 'BROKEN7' })
await delay(20)
await setTimeout(20)
client.log.add({ channel: 'resend', type: 'logux/subscribe' })
await delay(20)
await setTimeout(20)

expect(errors).toEqual([
'Empty back-end answer',
Expand Down Expand Up @@ -727,9 +727,9 @@ it('reacts on backend error', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'AERROR' })
await delay(150)
await setTimeout(150)
client.log.add({ type: 'PERROR' })
await delay(150)
await setTimeout(150)

expect(errors).toEqual([
'Error on back-end server',
Expand Down Expand Up @@ -767,7 +767,7 @@ it('has bruteforce protection', async () => {
code = await send({ commands: [], secret: 'wrong', version: 4 })

expect(code).toEqual(429)
await delay(3050)
await setTimeout(3050)

code = await send({ commands: [], secret: 'wrong', version: 4 })

Expand All @@ -781,7 +781,7 @@ it('sets meta to resend', async () => {
})
let client = await app.connect('10')
client.log.add({ type: 'RESEND' })
await delay(50)
await setTimeout(50)
expect(app.log.actions()).toEqual([
{ type: 'RESEND' },
{ id: '1 10:1:1 0', type: 'logux/processed' }
Expand All @@ -804,7 +804,7 @@ it('processes server actions', async () => {
throw e
})
app.log.add({ type: 'RESEND' })
await delay(50)
await setTimeout(50)
expect(app.log.actions()).toEqual([{ type: 'RESEND' }])
expect(app.log.entries()[0][1].status).toEqual('processed')
})
Loading

0 comments on commit 5b26c6f

Please sign in to comment.