|
1 |
| -// only newer versions of node support async iterator |
2 |
| -if (!process.version.startsWith('v8')) { |
3 |
| - require('./async-iterator.es6') |
4 |
| -} |
| 1 | +const QueryStream = require('../') |
| 2 | +const pg = require('pg') |
| 3 | +const assert = require('assert') |
| 4 | + |
| 5 | +const queryText = 'SELECT * FROM generate_series(0, 200) num' |
| 6 | +describe('Async iterator', () => { |
| 7 | + it('works', async () => { |
| 8 | + const stream = new QueryStream(queryText, []) |
| 9 | + const client = new pg.Client() |
| 10 | + await client.connect() |
| 11 | + const query = client.query(stream) |
| 12 | + const rows = [] |
| 13 | + for await (const row of query) { |
| 14 | + rows.push(row) |
| 15 | + } |
| 16 | + assert.equal(rows.length, 201) |
| 17 | + await client.end() |
| 18 | + }) |
| 19 | + |
| 20 | + it('can async iterate and then do a query afterwards', async () => { |
| 21 | + const stream = new QueryStream(queryText, []) |
| 22 | + const client = new pg.Client() |
| 23 | + await client.connect() |
| 24 | + const query = client.query(stream) |
| 25 | + const iteratorRows = [] |
| 26 | + for await (const row of query) { |
| 27 | + iteratorRows.push(row) |
| 28 | + } |
| 29 | + assert.equal(iteratorRows.length, 201) |
| 30 | + const { rows } = await client.query('SELECT NOW()') |
| 31 | + assert.equal(rows.length, 1) |
| 32 | + await client.end() |
| 33 | + }) |
| 34 | + |
| 35 | + it('can async iterate multiple times with a pool', async () => { |
| 36 | + const pool = new pg.Pool({ max: 1 }) |
| 37 | + |
| 38 | + const allRows = [] |
| 39 | + const run = async () => { |
| 40 | + // get the client |
| 41 | + const client = await pool.connect() |
| 42 | + // stream some rows |
| 43 | + const stream = new QueryStream(queryText, []) |
| 44 | + const iteratorRows = [] |
| 45 | + client.query(stream) |
| 46 | + for await (const row of stream) { |
| 47 | + iteratorRows.push(row) |
| 48 | + allRows.push(row) |
| 49 | + } |
| 50 | + assert.equal(iteratorRows.length, 201) |
| 51 | + client.release() |
| 52 | + } |
| 53 | + await Promise.all([run(), run(), run()]) |
| 54 | + assert.equal(allRows.length, 603) |
| 55 | + await pool.end() |
| 56 | + }) |
| 57 | + |
| 58 | + it('can break out of iteration early', async () => { |
| 59 | + const pool = new pg.Pool({ max: 1 }) |
| 60 | + const client = await pool.connect() |
| 61 | + const rows = [] |
| 62 | + for await (const row of client.query(new QueryStream(queryText, [], { batchSize: 1 }))) { |
| 63 | + rows.push(row) |
| 64 | + break; |
| 65 | + } |
| 66 | + for await (const row of client.query(new QueryStream(queryText, []))) { |
| 67 | + rows.push(row) |
| 68 | + break; |
| 69 | + } |
| 70 | + for await (const row of client.query(new QueryStream(queryText, []))) { |
| 71 | + rows.push(row) |
| 72 | + break; |
| 73 | + } |
| 74 | + assert.strictEqual(rows.length, 3) |
| 75 | + client.release() |
| 76 | + await pool.end() |
| 77 | + }) |
| 78 | + |
| 79 | + it('only returns rows on first iteration', async () => { |
| 80 | + const pool = new pg.Pool({ max: 1 }) |
| 81 | + const client = await pool.connect() |
| 82 | + const rows = [] |
| 83 | + const stream = client.query(new QueryStream(queryText, [])) |
| 84 | + for await (const row of stream) { |
| 85 | + rows.push(row) |
| 86 | + break; |
| 87 | + } |
| 88 | + for await (const row of stream) { |
| 89 | + rows.push(row) |
| 90 | + } |
| 91 | + for await (const row of stream) { |
| 92 | + rows.push(row) |
| 93 | + } |
| 94 | + assert.strictEqual(rows.length, 1) |
| 95 | + client.release() |
| 96 | + await pool.end() |
| 97 | + }) |
| 98 | + |
| 99 | + it('can read with delays', async () => { |
| 100 | + const pool = new pg.Pool({ max: 1 }) |
| 101 | + const client = await pool.connect() |
| 102 | + const rows = [] |
| 103 | + const stream = client.query(new QueryStream(queryText, [], { batchSize: 1 })) |
| 104 | + for await (const row of stream) { |
| 105 | + rows.push(row) |
| 106 | + await new Promise((resolve) => setTimeout(resolve, 1)) |
| 107 | + } |
| 108 | + assert.strictEqual(rows.length, 201) |
| 109 | + client.release() |
| 110 | + await pool.end() |
| 111 | + }) |
| 112 | +}) |
0 commit comments