Skip to content

Commit

Permalink
fix: s3 e2e tests race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Dec 14, 2023
1 parent 89f2e0b commit d9eb5dd
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions test/s3.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import http from 'node:http'
import {describe} from 'node:test'
import {strict as assert} from 'node:assert'
import {S3, S3ServiceException} from '@aws-sdk/client-s3'
import {randomUUID} from 'crypto'

const STORE_PATH = '/upload'

Expand All @@ -16,7 +17,6 @@ interface S3Options {

const expireTime = 5000
const s3Credentials = {
bucket: process.env.AWS_BUCKET as string,
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
Expand All @@ -26,10 +26,40 @@ const s3Credentials = {

const s3Client = new S3(s3Credentials)

const createStore = (options: S3Options = {}) =>
function createBucket(bucketName: string) {
return s3Client.createBucket({
Bucket: bucketName,
})
}

async function emptyBucket(bucketName: string) {
while (true) {
const listObjects = await s3Client.listObjects({
Bucket: bucketName,
})

const objects = listObjects.Contents
if (!objects || (objects && objects.length === 0)) {
return
}
await s3Client.deleteObjects({
Bucket: bucketName,
Delete: {
Objects: objects.map((o) => ({
Key: o.Key as string,
})),
},
})
}
}

const createStore = (bucketName: string, options: S3Options = {}) =>
new S3Store({
...options,
s3ClientConfig: s3Credentials,
s3ClientConfig: {
bucket: bucketName,
...s3Credentials,
},
})

const createUpload = async (agent: SuperAgentTest, uploadLength: number) => {
Expand Down Expand Up @@ -70,9 +100,13 @@ describe('S3 Store E2E', () => {
let listener: http.Server
let agent: SuperAgentTest
let store: S3Store
let bucketName: string

before((done) => {
store = createStore({
before(async () => {
bucketName = `tus-s3-e2e-${randomUUID()}`
await createBucket(bucketName)

store = createStore(bucketName, {
expirationPeriodInMilliseconds: expireTime,
partSize: 5_242_880,
})
Expand All @@ -82,27 +116,18 @@ describe('S3 Store E2E', () => {
})
listener = server.listen()
agent = request.agent(listener)
done()
})

let startTime: number
beforeEach(() => {
startTime = Date.now()
beforeEach(async () => {
await emptyBucket(bucketName)
})

afterEach(async () => {
const endTime = Date.now()
const elapsedMs = (endTime - startTime) / 10

if (elapsedMs < expireTime) {
await wait(expireTime - elapsedMs + 100)
}

await store.deleteExpired()
})
after(async () => {
await s3Client.deleteBucket({
Bucket: bucketName,
})

after((done) => {
listener.close(done)
await new Promise((resolve) => listener.close(resolve))
})

it('should set Tus-Completed=false when the upload is not completed', async () => {
Expand All @@ -111,7 +136,7 @@ describe('S3 Store E2E', () => {
await patchUpload(agent, uploadId, data.subarray(0, 1024 * 1024 * 6))

const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.info',
})

Expand All @@ -133,7 +158,7 @@ describe('S3 Store E2E', () => {
await patchUpload(agent, uploadId, data.subarray(offset), offset)

const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.info',
})

Expand All @@ -150,11 +175,11 @@ describe('S3 Store E2E', () => {

const [infoFile, partFile] = await Promise.all([
s3Client.getObject({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.info',
}),
s3Client.getObject({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.part',
}),
])
Expand All @@ -165,19 +190,19 @@ describe('S3 Store E2E', () => {
assert(infoFile.$metadata.httpStatusCode === 200)
assert(partFile.$metadata.httpStatusCode === 200)

await wait(expireTime + 100)
await wait(expireTime + 500)

// .info file and .part should be deleted since now they should be expired
const deletedFiles = await store.deleteExpired()
assert(deletedFiles === 2, `not all parts were deleted, deleted ${deletedFiles}`)

const files = await Promise.allSettled([
s3Client.getObject({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.info',
}),
s3Client.getObject({
Bucket: s3Credentials.bucket,
Bucket: bucketName,
Key: uploadId + '.part',
}),
])
Expand Down

0 comments on commit d9eb5dd

Please sign in to comment.