Skip to content

Commit

Permalink
fix: deadlock and storage tests (#78)
Browse files Browse the repository at this point in the history
* fix: deadlock tests

* fix: storage test
  • Loading branch information
sattvikc authored Sep 20, 2023
1 parent ee75b46 commit 116216e
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
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));
}
}

0 comments on commit 116216e

Please sign in to comment.