Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deadlock and storage tests #78

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/test/java/io/supertokens/storage/mysql/test/DeadlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
package io.supertokens.storage.mysql.test;

import io.supertokens.ProcessState;
import io.supertokens.authRecipe.AuthRecipe;
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlagTestContent;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.KeyValueInfo;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
Expand Down Expand Up @@ -593,6 +598,96 @@ public void testConcurrentDeleteAndUpdate() throws Exception {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}


@Test
public void testLinkAccountsInParallel() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{
EE_FEATURES.ACCOUNT_LINKING, EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

ExecutorService es = Executors.newFixedThreadPool(1000);

AtomicBoolean pass = new AtomicBoolean(true);

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password");
AuthRecipeUserInfo user2 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password");

AuthRecipe.createPrimaryUser(process.getProcess(), user1.getSupertokensUserId());

for (int i = 0; i < 3000; i++) {
es.execute(() -> {
try {
AuthRecipe.linkAccounts(process.getProcess(), user2.getSupertokensUserId(), user1.getSupertokensUserId());
AuthRecipe.unlinkAccounts(process.getProcess(), user2.getSupertokensUserId());
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("the transaction might succeed if retried")) {
pass.set(false);
}
}
});
}

es.shutdown();
es.awaitTermination(2, TimeUnit.MINUTES);

assert (pass.get());
assertNull(process
.checkOrWaitForEventInPlugin(io.supertokens.storage.mysql.ProcessState.PROCESS_STATE.DEADLOCK_NOT_RESOLVED));
assertNotNull(process
.checkOrWaitForEventInPlugin(io.supertokens.storage.mysql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testCreatePrimaryInParallel() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{
EE_FEATURES.ACCOUNT_LINKING, EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

ExecutorService es = Executors.newFixedThreadPool(1000);

AtomicBoolean pass = new AtomicBoolean(true);

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password");

for (int i = 0; i < 3000; i++) {
es.execute(() -> {
try {
AuthRecipe.createPrimaryUser(process.getProcess(), user1.getSupertokensUserId());
AuthRecipe.unlinkAccounts(process.getProcess(), user1.getSupertokensUserId());
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("the transaction might succeed if retried")) {
pass.set(false);
}
}
});
}

es.shutdown();
es.awaitTermination(2, TimeUnit.MINUTES);

assert (pass.get());
assertNull(process
.checkOrWaitForEventInPlugin(io.supertokens.storage.mysql.ProcessState.PROCESS_STATE.DEADLOCK_NOT_RESOLVED));
assertNotNull(process
.checkOrWaitForEventInPlugin(io.supertokens.storage.mysql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package io.supertokens.storage.mysql.test;

import io.supertokens.ProcessState;
import io.supertokens.authRecipe.AuthRecipe;
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlagTestContent;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeStorage;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.totp.TOTPDevice;
Expand All @@ -13,13 +21,14 @@
import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage;
import io.supertokens.storageLayer.StorageLayer;

import io.supertokens.thirdparty.ThirdParty;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

public class StorageLayerTest {

Expand Down Expand Up @@ -90,4 +99,51 @@ public void totpCodeLengthTest() throws Exception {
insertUsedCodeUtil(storage, code);
}

@Test
public void testLinkedAccountUser() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{
EE_FEATURES.ACCOUNT_LINKING, EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password");
Thread.sleep(50);
AuthRecipeUserInfo user2 = ThirdParty.signInUp(process.getProcess(), "google", "googleid", "[email protected]").user;
Thread.sleep(50);
Passwordless.CreateCodeResponse code1 = Passwordless.createCode(process.getProcess(), "[email protected]", null, null, null);
AuthRecipeUserInfo user3 = Passwordless.consumeCode(process.getProcess(), code1.deviceId, code1.deviceIdHash, code1.userInputCode, null).user;
Thread.sleep(50);
Passwordless.CreateCodeResponse code2 = Passwordless.createCode(process.getProcess(), null, "+919876543210", null, null);
AuthRecipeUserInfo user4 = Passwordless.consumeCode(process.getProcess(), code2.deviceId, code2.deviceIdHash, code2.userInputCode, null).user;

AuthRecipe.createPrimaryUser(process.getProcess(), user3.getSupertokensUserId());
AuthRecipe.linkAccounts(process.getProcess(), user1.getSupertokensUserId(), user3.getSupertokensUserId());
AuthRecipe.linkAccounts(process.getProcess(), user2.getSupertokensUserId(), user3.getSupertokensUserId());
AuthRecipe.linkAccounts(process.getProcess(), user4.getSupertokensUserId(), user3.getSupertokensUserId());

String[] userIds = new String[]{
user1.getSupertokensUserId(),
user2.getSupertokensUserId(),
user3.getSupertokensUserId(),
user4.getSupertokensUserId()
};

for (String userId : userIds){
AuthRecipeUserInfo primaryUser = ((AuthRecipeStorage) StorageLayer.getStorage(process.getProcess())).getPrimaryUserById(
new AppIdentifier(null, null), userId);
assertEquals(user3.getSupertokensUserId(), primaryUser.getSupertokensUserId());
assertEquals(4, primaryUser.loginMethods.length);
assertTrue(primaryUser.loginMethods[0].timeJoined < primaryUser.loginMethods[1].timeJoined);
assertTrue(primaryUser.loginMethods[1].timeJoined < primaryUser.loginMethods[2].timeJoined);
assertTrue(primaryUser.loginMethods[2].timeJoined < primaryUser.loginMethods[3].timeJoined);
assertEquals(primaryUser.timeJoined, primaryUser.loginMethods[0].timeJoined);
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}