Skip to content

How to avoid errors on rapid queries? #36

@good-idea

Description

@good-idea

I'm setting up some integration tests, and coming across the "Error: Transaction has been aborted. Please retry" error. There's a function that resets the data I am mutating before and after each test. It works about 80% of the time, but otherwise is throwing the error.

If I put a short async 'sleep' delay in the beforeEach/afterEach functions, it works as expected. But, this doesn't leave me confident that my code can handle requests which might fail because they are submitted in rapid succession. Simply retrying the query once more doesn't feel good either.

Am I missing something?

Here's a simplified version of the test.

// @flow
/* eslint-disable no-undef */
import { request } from '../../../__tests__/utils'
import { sampleUser } from '../../../database/stubs/users'
import { updateUser, getUser } from '../UserModel'

const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const resetUserData = async () => {
	await updateUser(sampleUser.uid, { input: sampleUser })
}

describe('[updateViewerProfile]', () => {
	beforeAll(async () => {
		await resetUserData()
		// await sleep(100) // <-- uncommenting these two gets it working
	})
	afterAll(async () => {
		// await sleep(100) // <--
		await resetUserData()
	})

	it('should update existing data', async () => {
		const newBioLine = 'Test bio line'

		// Note: this is an actual GraphQL mutation that goes through my resolvers.
		// This is not querying dgraph directly.
		const mutation = /* GraphQL */ `
		mutation updateProfile($bioLine: String) {
			updateViewerProfile(input: {bioLine: $bioLine }) {
				username
				bioLine
			}
		}
		`
		const variables = {
			bioLine: newBioLine,
		}
		const context = { viewer: joseph }
		const result = await request(mutation, { variables, context })
		expect(result.data.updateViewerProfile.bioLine).toBe(newBioLine)
	})
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions