|
| 1 | +import { subscribe, CommitmentLevel, SubscribeUpdate, LaserstreamConfig } from '../client'; |
| 2 | + |
| 3 | +const credentials = require('../test-config'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Test for DataSliceOverlap bug fix |
| 7 | + * |
| 8 | + * This test verifies that calling write() multiple times with accountsDataSlice |
| 9 | + * does NOT cause duplication, which would lead to "DataSliceOverlap" errors |
| 10 | + * during reconnection. |
| 11 | + * |
| 12 | + * Before fix: accountsDataSlice would extend/accumulate: [slice, slice, slice, slice] |
| 13 | + * After fix: accountsDataSlice replaces: [slice] |
| 14 | + */ |
| 15 | +async function testDataSliceOverlapBugFix() { |
| 16 | + |
| 17 | + const config: LaserstreamConfig = { |
| 18 | + apiKey: credentials.laserstream.apiKey, |
| 19 | + endpoint: credentials.laserstream.endpoint, |
| 20 | + replay: true, |
| 21 | + }; |
| 22 | + |
| 23 | + let reconnectionAttempts = 0; |
| 24 | + let dataSliceOverlapError = false; |
| 25 | + let reconnectionSucceeded = false; |
| 26 | + let firstReconnectDetected = false; |
| 27 | + |
| 28 | + const USDC_MINT = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'; |
| 29 | + |
| 30 | + const stream = await subscribe( |
| 31 | + config, |
| 32 | + { |
| 33 | + accounts: { |
| 34 | + "usdc-accounts": { |
| 35 | + account: [], |
| 36 | + owner: ["TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"], |
| 37 | + filters: [ |
| 38 | + { datasize: 165 }, |
| 39 | + { memcmp: { offset: 0, base58: USDC_MINT } } |
| 40 | + ] |
| 41 | + } |
| 42 | + }, |
| 43 | + commitment: CommitmentLevel.PROCESSED, |
| 44 | + slots: {}, |
| 45 | + transactions: {}, |
| 46 | + transactionsStatus: {}, |
| 47 | + blocks: {}, |
| 48 | + blocksMeta: {}, |
| 49 | + entry: {}, |
| 50 | + accountsDataSlice: [ |
| 51 | + { |
| 52 | + offset: 0, |
| 53 | + length: 165 |
| 54 | + } |
| 55 | + ] |
| 56 | + }, |
| 57 | + async (update: SubscribeUpdate) => { |
| 58 | + // Track that we're receiving data after reconnection |
| 59 | + if (firstReconnectDetected && reconnectionAttempts > 0) { |
| 60 | + reconnectionSucceeded = true; |
| 61 | + } |
| 62 | + }, |
| 63 | + (error) => { |
| 64 | + if (error.message.includes('DataSliceOverlap')) { |
| 65 | + dataSliceOverlapError = true; |
| 66 | + } |
| 67 | + |
| 68 | + if (error.message.includes('Connection error')) { |
| 69 | + if (error.message.includes('attempt 1')) { |
| 70 | + firstReconnectDetected = true; |
| 71 | + } |
| 72 | + const match = error.message.match(/attempt (\d+)/); |
| 73 | + if (match) { |
| 74 | + reconnectionAttempts = Math.max(reconnectionAttempts, parseInt(match[1])); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 81 | + |
| 82 | + for (let i = 1; i <= 3; i++) { |
| 83 | + await stream.write({ |
| 84 | + accounts: { |
| 85 | + "usdc-accounts": { |
| 86 | + account: [], |
| 87 | + owner: ["TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"], |
| 88 | + filters: [ |
| 89 | + { datasize: 165 }, |
| 90 | + { memcmp: { offset: 0, base58: USDC_MINT } } |
| 91 | + ] |
| 92 | + } |
| 93 | + }, |
| 94 | + commitment: CommitmentLevel.PROCESSED, |
| 95 | + slots: {}, |
| 96 | + transactions: {}, |
| 97 | + transactionsStatus: {}, |
| 98 | + blocks: {}, |
| 99 | + blocksMeta: {}, |
| 100 | + entry: {}, |
| 101 | + accountsDataSlice: [ |
| 102 | + { |
| 103 | + offset: 0, |
| 104 | + length: 165 |
| 105 | + } |
| 106 | + ] |
| 107 | + }); |
| 108 | + await new Promise(resolve => setTimeout(resolve, 500)); |
| 109 | + } |
| 110 | + |
| 111 | + await new Promise(resolve => setTimeout(resolve, 60000)); |
| 112 | + |
| 113 | + stream.cancel(); |
| 114 | + |
| 115 | + if (dataSliceOverlapError) { |
| 116 | + throw new Error('DataSliceOverlap error occurred'); |
| 117 | + } |
| 118 | + |
| 119 | + if (reconnectionAttempts > 0 && !reconnectionSucceeded) { |
| 120 | + throw new Error('Reconnection attempted but did not succeed'); |
| 121 | + } |
| 122 | + |
| 123 | + if (reconnectionAttempts === 0) { |
| 124 | + console.log('Warning: No reconnection triggered. Chaos proxy may not be running.'); |
| 125 | + } |
| 126 | + |
| 127 | + console.log('Test passed'); |
| 128 | +} |
| 129 | + |
| 130 | +testDataSliceOverlapBugFix().catch(err => { |
| 131 | + console.error('Test failed:', err.message); |
| 132 | + process.exit(1); |
| 133 | +}); |
0 commit comments