|
| 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