aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

AWS Cognito returns ResourceNotFoundException and GroupExistsException in test cases

Open zirkelc opened this issue 3 years ago • 1 comments

Describe the bug

I'm writing test cases that repeatedly call CreateGroup, UpdateGroup and DeleteGroup for AWS Cognito and validate if these changes were successful by reading the respective group via GetGroup via the AWS SDK for JS v3.

However, I noticed a strange behavior that a call to DeleteGroup yield an ResourceNotFoundException but a subsequent call to CreateGroup yields an GroupExistsException for the same group name.

It seems like the AWS Cognito API is eventually consistent, but I could't find any information online.

Expected Behavior

CreateGroup, UpdateGroup, DeleteGroup should not yield any exception.

Current Behavior

$ npx ts-node index

create group

get group

delete group

create group
GroupExistsException A group with the name already exists.

get group
ResourceNotFoundException Group not found.

delete group
ResourceNotFoundException Group not found.

create group
GroupExistsException A group with the name already exists.

get group
ResourceNotFoundException Group not found.

delete group
ResourceNotFoundException Group not found.
...

Reproduction Steps

I created a test repository to reproduce this issue: https://github.com/zirkelc/aws-cognito-groups The UserPoolId and region must be changed to your own resources. Then this example can be started with npx ts-node index and prints the exceptions as they occur.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

3.131.0

Environment details (OS name and version, etc.)

macOS 12.1 (21C52)

zirkelc avatar Jul 26 '22 08:07 zirkelc

@zirkelc thanks for opening this issue and providing with a repo for repro as well. I was able to reproduce this, would need to dive deeper to check other scenarios too. I'll post more findings on it as I go through with it thanks

ajredniwja avatar Aug 08 '22 15:08 ajredniwja

Hi @zirkelc , From what I can see you are not spacing your request in a reasonable order and artificially creating a race condition service side.

However, I noticed a strange behavior that a call to DeleteGroup yield an ResourceNotFoundException but a subsequent call to CreateGroup yields an GroupExistsException for the same group name.

You need to pay attention to the order. Delete succeeds, creates fails right after

create group

get group

delete group <-- success

create group
GroupExistsException A group with the name already exists. <-- failure using the same name.

You are expecting your code to be synchronous, but await only pauses the current async function. This means it does not block the execution of the rest of the script.

Even with this refactor:

for (let i = 0 ; i < 10; i++){
    try {
        console.log("DELETE GROUP")
        const res = await cognito.send(new DeleteGroupCommand({UserPoolId ,GroupName}))
        // check deletion was successful.
        console.log("delete status:",res.$metadata.httpStatusCode)
        await sleep(15000)
        console.log("CREATE GROUP")
        const createGroupRes = await cognito.send(new CreateGroupCommand({UserPoolId, GroupName}))
        console.log("create status:",createGroupRes.$metadata.httpStatusCode)
    } catch (error) { 
        console.log(error)
    }
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

yields:

DELETE GROUP
delete status: 200
CREATE GROUP
create status: 200
DELETE GROUP
delete status: 200
CREATE GROUP
GroupExistsException: A group with the name already exists.
...
...

You are creating and immediately deleting the same record over and over. This definitely will lead to a conflict server side. You might want to think of a different testing strategy.

Since this does not appear to be an SDK issue, more like how the service handles idempotent requests I feel confident we can close this.

If you still think this issue should be considered please open a new one. Thanks, Ran~

RanVaknin avatar Feb 10 '23 02:02 RanVaknin

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

github-actions[bot] avatar Feb 25 '23 00:02 github-actions[bot]