Skip to content

Commit 5161285

Browse files
Fix timeout test
1 parent 9677e21 commit 5161285

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/destination-actions/src/destinations/sftp/__tests__/client.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEFAULT_REQUEST_TIMEOUT } from '@segment/actions-core'
12
import { normalizeSSHKey, testSFTPConnection, uploadSFTP } from '../client'
23
import { SFTP_DEFAULT_PORT } from '../constants'
34
import { Settings } from '../generated-types'
@@ -309,6 +310,7 @@ MIIEpAIBAAKCAQEA1234567890
309310

310311
describe('testSFTPConnection', () => {
311312
beforeEach(() => {
313+
jest.useRealTimers()
312314
jest.clearAllMocks()
313315
jest.clearAllTimers()
314316
})
@@ -440,16 +442,20 @@ MN
440442
expect(Client.prototype.end).toHaveBeenCalled()
441443
})
442444

443-
// This test is skipped because it can slow down the CI
444-
it.skip('should throw timeout error when operation takes too long', async () => {
445+
it('should throw timeout error when operation takes too long', async () => {
446+
jest.useFakeTimers()
445447
Client.prototype.connect = jest.fn().mockImplementation(() => new Promise((r) => setTimeout(r, 11500)))
446448
Client.prototype.list = jest.fn().mockResolvedValue([])
447449
Client.prototype.end = jest.fn().mockResolvedValue(undefined)
448450

449-
await expect(testSFTPConnection(passwordSettings)).rejects.toThrow(
450-
'Request timed out before receiving a response'
451-
)
452-
}, 12000)
451+
// Start the async operation
452+
const promise = testSFTPConnection(passwordSettings)
453+
454+
// Advance timers to trigger the timeout
455+
jest.advanceTimersByTime(DEFAULT_REQUEST_TIMEOUT)
456+
457+
await expect(promise).rejects.toThrow('SFTP connection timed out')
458+
})
453459

454460
it('should handle connection errors with cleanup', async () => {
455461
const connectionError = new Error('Connection refused')

packages/destination-actions/src/destinations/sftp/sftp-wrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class SFTPWrapper {
107107

108108
processWrites()
109109
.then(resolve)
110-
.catch(resolve)
110+
.catch(reject)
111111
.finally(() => {
112112
// we don't care if close fails here because we expect the caller to call sftp.end() eventually
113113
this.client?.close(handle, (closeErr) => {

0 commit comments

Comments
 (0)