Skip to content

Commit 933fb60

Browse files
authored
fix: main thread fix (#130)
* fix: main thread fix * fix: adding test
1 parent 6a93d5d commit 933fb60

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ static void initPool(Start start, boolean shouldWait) throws DbInitException {
133133
if (isAlreadyInitialised(start)) {
134134
return;
135135
}
136-
if (Thread.currentThread() != start.mainThread) {
137-
throw new DbInitException("Should not come here");
138-
}
139136
Logging.info(start, "Setting up PostgreSQL connection pool.", true);
140137
boolean longMessagePrinted = false;
141138
long maxTryTime = System.currentTimeMillis() + getTimeToWaitToInit(start);

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public class Start
120120
private static final String REFRESH_TOKEN_KEY_NAME = "refresh_token_key";
121121
public static boolean isTesting = false;
122122
boolean enabled = true;
123-
Thread mainThread = Thread.currentThread();
123+
static Thread mainThread = Thread.currentThread();
124124
private Thread shutdownHook;
125125

126126
private boolean isBaseTenant = false;
@@ -2810,4 +2810,9 @@ public String[] getAllTablesInTheDatabaseThatHasDataForAppId(String appId) throw
28102810
throw new StorageQueryException(e);
28112811
}
28122812
}
2813+
2814+
@TestOnly
2815+
public Thread getMainThread() {
2816+
return mainThread;
2817+
}
28132818
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
3+
*
4+
* This software is licensed under the Apache License, Version 2.0 (the
5+
* "License") as published by the Apache Software Foundation.
6+
*
7+
* You may not use this file except in compliance with the License. You may
8+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.supertokens.storage.postgresql.test;
18+
19+
import com.google.gson.JsonObject;
20+
import io.supertokens.ProcessState;
21+
import io.supertokens.featureflag.EE_FEATURES;
22+
import io.supertokens.featureflag.FeatureFlagTestContent;
23+
import io.supertokens.pluginInterface.STORAGE_TYPE;
24+
import io.supertokens.pluginInterface.Storage;
25+
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
26+
import io.supertokens.storage.postgresql.Start;
27+
import io.supertokens.storage.postgresql.test.httpRequest.HttpRequestForTesting;
28+
import io.supertokens.storageLayer.StorageLayer;
29+
import org.junit.AfterClass;
30+
import org.junit.Before;
31+
import org.junit.Rule;
32+
import org.junit.Test;
33+
import org.junit.rules.TestRule;
34+
35+
import static org.junit.Assert.*;
36+
37+
public class TestMainThread {
38+
@Rule
39+
public TestRule watchman = Utils.getOnFailure();
40+
41+
@AfterClass
42+
public static void afterTesting() {
43+
Utils.afterTesting();
44+
}
45+
46+
@Before
47+
public void beforeEach() {
48+
Utils.reset();
49+
}
50+
51+
@Test
52+
public void testThatMainThreadIsSameThroughout() throws Exception {
53+
String[] args = {"../"};
54+
55+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
56+
FeatureFlagTestContent.getInstance(process.getProcess())
57+
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
58+
process.startProcess();
59+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
60+
61+
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
62+
return;
63+
}
64+
65+
if (StorageLayer.isInMemDb(process.getProcess())) {
66+
return;
67+
}
68+
69+
Storage storage = StorageLayer.getBaseStorage(process.getProcess());
70+
Thread mainThread = ((Start) storage).getMainThread();
71+
72+
// Create a new app
73+
JsonObject config = new JsonObject();
74+
storage.modifyConfigToAddANewUserPoolForTesting(config, 1);
75+
JsonObject requestBody = new JsonObject();
76+
requestBody.addProperty("appId", "a1");
77+
requestBody.add("coreConfig", config);
78+
HttpRequestForTesting.sendJsonPUTRequest(process.getProcess(), "", "http://localhost:3567/recipe/multitenancy/app",
79+
requestBody, 1000, 2500, null, "3.0", "multitenancy");
80+
81+
Storage storage2 = StorageLayer.getStorage(new TenantIdentifier(null, "a1", null), process.getProcess());
82+
83+
assertNotEquals(storage, storage2);
84+
assertEquals(mainThread, ((Start) storage2).getMainThread());
85+
86+
process.kill();
87+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
88+
}
89+
}

0 commit comments

Comments
 (0)