Skip to content

Commit

Permalink
Compute test table size based on default high water mark
Browse files Browse the repository at this point in the history
  • Loading branch information
valeneiko committed Jan 23, 2025
1 parent 77c820e commit 2797ff4
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { t, nt, ot } from './test.js' // eslint-disable-line
import net from 'net'
import fs from 'fs'
import crypto from 'crypto'
import stream from 'stream'

import postgres from '../src/index.js'
const delay = ms => new Promise(r => setTimeout(r, ms))
Expand Down Expand Up @@ -1919,11 +1920,12 @@ t('Copy read with back-pressure', async() => {

// Make sure there are enough rows in the table to fill the buffer
// so that `CopyDone` message is handled while the socket is paused
await sql`insert into test select * from generate_series(1,12774)`
const bufferSize = Math.ceil((stream.getDefaultHighWaterMark || (() => 16384))() / 6)
await sql`insert into test select * from generate_series(10000,${9999 + bufferSize})`

let result = 0
const readable = await sql`copy test to stdout`.readable()
readable.on('data', _ => result++)
readable.on('data', () => result++)

// Pause the stream so that the entire buffer fills up
readable.pause()
Expand All @@ -1934,7 +1936,7 @@ t('Copy read with back-pressure', async() => {
(async() => {
// Wait until the entire buffer fills up,
await new Promise(r => readable.on('readable', () => {
if (readable.readableBuffer.length === 12774)
if (readable.readableBuffer.length === bufferSize)
r()
}))
// Switch the stream back to flowing mode (allowing it to be consumed)
Expand All @@ -1948,7 +1950,7 @@ t('Copy read with back-pressure', async() => {

return [
result,
12774,
bufferSize,
await sql`drop table test`
]
})
Expand Down

0 comments on commit 2797ff4

Please sign in to comment.