@@ -4,6 +4,7 @@ import { t, nt, ot } from './test.js' // eslint-disable-line
4
4
import net from 'net'
5
5
import fs from 'fs'
6
6
import crypto from 'crypto'
7
+ import stream from 'stream'
7
8
8
9
import postgres from '../src/index.js'
9
10
const delay = ms => new Promise ( r => setTimeout ( r , ms ) )
@@ -1919,11 +1920,12 @@ t('Copy read with back-pressure', async() => {
1919
1920
1920
1921
// Make sure there are enough rows in the table to fill the buffer
1921
1922
// so that `CopyDone` message is handled while the socket is paused
1922
- await sql `insert into test select * from generate_series(1,12774)`
1923
+ const bufferSize = Math . ceil ( ( stream . getDefaultHighWaterMark || ( ( ) => 16384 ) ) ( ) / 6 )
1924
+ await sql `insert into test select * from generate_series(10000,${ 9999 + bufferSize } )`
1923
1925
1924
1926
let result = 0
1925
1927
const readable = await sql `copy test to stdout` . readable ( )
1926
- readable . on ( 'data' , _ => result ++ )
1928
+ readable . on ( 'data' , ( ) => result ++ )
1927
1929
1928
1930
// Pause the stream so that the entire buffer fills up
1929
1931
readable . pause ( )
@@ -1934,7 +1936,7 @@ t('Copy read with back-pressure', async() => {
1934
1936
( async ( ) => {
1935
1937
// Wait until the entire buffer fills up,
1936
1938
await new Promise ( r => readable . on ( 'readable' , ( ) => {
1937
- if ( readable . readableBuffer . length === 12774 )
1939
+ if ( readable . readableBuffer . length === bufferSize )
1938
1940
r ( )
1939
1941
} ) )
1940
1942
// Switch the stream back to flowing mode (allowing it to be consumed)
@@ -1948,7 +1950,7 @@ t('Copy read with back-pressure', async() => {
1948
1950
1949
1951
return [
1950
1952
result ,
1951
- 12774 ,
1953
+ bufferSize ,
1952
1954
await sql `drop table test`
1953
1955
]
1954
1956
} )
0 commit comments