@@ -114,109 +114,122 @@ public void testActiveConnectionsWithTenants() throws Exception {
114114 public void testDownTimeWhenChangingConnectionPoolSize () throws Exception {
115115 String [] args = {"../" };
116116
117- TestingProcessManager .TestingProcess process = TestingProcessManager .start (args , false );
118- FeatureFlagTestContent .getInstance (process .getProcess ())
119- .setKeyValue (FeatureFlagTestContent .ENABLED_FEATURES , new EE_FEATURES []{EE_FEATURES .MULTI_TENANCY });
120- process .startProcess ();
121- assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STARTED ));
122-
123- Start start = (Start ) StorageLayer .getBaseStorage (process .getProcess ());
124- assertEquals (10 , start .getDbActivityCount ("supertokens" ));
125-
126- JsonObject config = new JsonObject ();
127- start .modifyConfigToAddANewUserPoolForTesting (config , 1 );
128- config .addProperty ("postgresql_connection_pool_size" , 300 );
129- AtomicLong firstErrorTime = new AtomicLong (-1 );
130- AtomicLong successAfterErrorTime = new AtomicLong (-1 );
131- AtomicInteger errorCount = new AtomicInteger (0 );
132-
133- Multitenancy .addNewOrUpdateAppOrTenant (process .getProcess (), new TenantConfig (
134- new TenantIdentifier (null , null , "t1" ),
135- new EmailPasswordConfig (true ),
136- new ThirdPartyConfig (true , null ),
137- new PasswordlessConfig (true ),
138- config
139- ), false );
140-
141- Thread .sleep (5000 ); // let the new tenant be ready
142-
143- assertEquals (300 , start .getDbActivityCount ("st1" ));
144-
145- ExecutorService es = Executors .newFixedThreadPool (100 );
146-
147- for (int i = 0 ; i < 10000 ; i ++) {
148- int finalI = i ;
149- es .execute (() -> {
150- try {
151- TenantIdentifier t1 = new TenantIdentifier (null , null , "t1" );
152- TenantIdentifierWithStorage t1WithStorage = t1 .withStorage (StorageLayer .getStorage (t1 , process .getProcess ()));
153- ThirdParty .signInUp (t1WithStorage , process .getProcess (), "google" , "googleid" + finalI , "user" +
154- finalI + "@example.com" );
155-
156- if (firstErrorTime .get () != -1 && successAfterErrorTime .get () == -1 ) {
157- successAfterErrorTime .set (System .currentTimeMillis ());
158- }
159- } catch (StorageQueryException e ) {
160- if (e .getMessage ().contains ("Connection is closed" ) || e .getMessage ().contains ("has been closed" )) {
161- if (firstErrorTime .get () == -1 ) {
162- firstErrorTime .set (System .currentTimeMillis ());
117+ for (int t = 0 ; t < 5 ; t ++) {
118+ TestingProcessManager .TestingProcess process = TestingProcessManager .start (args , false );
119+ FeatureFlagTestContent .getInstance (process .getProcess ())
120+ .setKeyValue (FeatureFlagTestContent .ENABLED_FEATURES , new EE_FEATURES []{EE_FEATURES .MULTI_TENANCY });
121+ process .startProcess ();
122+ assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STARTED ));
123+
124+ Start start = (Start ) StorageLayer .getBaseStorage (process .getProcess ());
125+ assertEquals (10 , start .getDbActivityCount ("supertokens" ));
126+
127+ JsonObject config = new JsonObject ();
128+ start .modifyConfigToAddANewUserPoolForTesting (config , 1 );
129+ config .addProperty ("postgresql_connection_pool_size" , 300 );
130+ AtomicLong firstErrorTime = new AtomicLong (-1 );
131+ AtomicLong successAfterErrorTime = new AtomicLong (-1 );
132+ AtomicInteger errorCount = new AtomicInteger (0 );
133+
134+ Multitenancy .addNewOrUpdateAppOrTenant (process .getProcess (), new TenantConfig (
135+ new TenantIdentifier (null , null , "t1" ),
136+ new EmailPasswordConfig (true ),
137+ new ThirdPartyConfig (true , null ),
138+ new PasswordlessConfig (true ),
139+ config
140+ ), false );
141+
142+ Thread .sleep (5000 ); // let the new tenant be ready
143+
144+ assertEquals (300 , start .getDbActivityCount ("st1" ));
145+
146+ ExecutorService es = Executors .newFixedThreadPool (100 );
147+
148+ for (int i = 0 ; i < 10000 ; i ++) {
149+ int finalI = i ;
150+ es .execute (() -> {
151+ try {
152+ TenantIdentifier t1 = new TenantIdentifier (null , null , "t1" );
153+ TenantIdentifierWithStorage t1WithStorage = t1 .withStorage (StorageLayer .getStorage (t1 , process .getProcess ()));
154+ ThirdParty .signInUp (t1WithStorage , process .getProcess (), "google" , "googleid" + finalI , "user" +
155+ finalI + "@example.com" );
156+
157+ if (firstErrorTime .get () != -1 && successAfterErrorTime .get () == -1 ) {
158+ successAfterErrorTime .set (System .currentTimeMillis ());
159+ }
160+ } catch (StorageQueryException e ) {
161+ if (e .getMessage ().contains ("Connection is closed" ) || e .getMessage ().contains ("has been closed" )) {
162+ if (firstErrorTime .get () == -1 ) {
163+ firstErrorTime .set (System .currentTimeMillis ());
164+ }
165+ } else {
166+ errorCount .incrementAndGet ();
167+ throw new RuntimeException (e );
163168 }
164- } else {
169+ } catch ( EmailChangeNotAllowedException e ) {
165170 errorCount .incrementAndGet ();
166171 throw new RuntimeException (e );
167- }
168- } catch (EmailChangeNotAllowedException e ) {
169- errorCount .incrementAndGet ();
170- throw new RuntimeException (e );
171- } catch (TenantOrAppNotFoundException e ) {
172- errorCount .incrementAndGet ();
173- throw new RuntimeException (e );
174- } catch (BadPermissionException e ) {
175- errorCount .incrementAndGet ();
176- throw new RuntimeException (e );
177- } catch (IllegalStateException e ) {
178- if (e .getMessage ().contains ("Please call initPool before getConnection" )) {
179- if (firstErrorTime .get () == -1 ) {
180- firstErrorTime .set (System .currentTimeMillis ());
181- }
182- } else {
172+ } catch (TenantOrAppNotFoundException e ) {
173+ errorCount .incrementAndGet ();
174+ throw new RuntimeException (e );
175+ } catch (BadPermissionException e ) {
183176 errorCount .incrementAndGet ();
184- throw e ;
177+ throw new RuntimeException (e );
178+ } catch (IllegalStateException e ) {
179+ if (e .getMessage ().contains ("Please call initPool before getConnection" )) {
180+ if (firstErrorTime .get () == -1 ) {
181+ firstErrorTime .set (System .currentTimeMillis ());
182+ }
183+ } else {
184+ errorCount .incrementAndGet ();
185+ throw e ;
186+ }
185187 }
186- }
187- });
188- }
188+ });
189+ }
189190
190- // change connection pool size
191- config .addProperty ("postgresql_connection_pool_size" , 200 );
191+ // change connection pool size
192+ config .addProperty ("postgresql_connection_pool_size" , 200 );
192193
193- Multitenancy .addNewOrUpdateAppOrTenant (process .getProcess (), new TenantConfig (
194- new TenantIdentifier (null , null , "t1" ),
195- new EmailPasswordConfig (true ),
196- new ThirdPartyConfig (true , null ),
197- new PasswordlessConfig (true ),
198- config
199- ), false );
194+ Multitenancy .addNewOrUpdateAppOrTenant (process .getProcess (), new TenantConfig (
195+ new TenantIdentifier (null , null , "t1" ),
196+ new EmailPasswordConfig (true ),
197+ new ThirdPartyConfig (true , null ),
198+ new PasswordlessConfig (true ),
199+ config
200+ ), false );
200201
201- Thread .sleep (3000 ); // let the new tenant be ready
202+ Thread .sleep (3000 ); // let the new tenant be ready
202203
203- es .shutdown ();
204- es .awaitTermination (2 , TimeUnit .MINUTES );
204+ es .shutdown ();
205+ es .awaitTermination (2 , TimeUnit .MINUTES );
205206
206- assertEquals (0 , errorCount .get ());
207+ assertEquals (0 , errorCount .get ());
207208
208- assertEquals (200 , start .getDbActivityCount ("st1" ));
209+ assertEquals (200 , start .getDbActivityCount ("st1" ));
209210
210- // delete tenant
211- Multitenancy .deleteTenant (new TenantIdentifier (null , null , "t1" ), process .getProcess ());
212- Thread .sleep (3000 ); // let the tenant be deleted
211+ // delete tenant
212+ Multitenancy .deleteTenant (new TenantIdentifier (null , null , "t1" ), process .getProcess ());
213+ Thread .sleep (3000 ); // let the tenant be deleted
213214
214- assertEquals (0 , start .getDbActivityCount ("st1" ));
215+ assertEquals (0 , start .getDbActivityCount ("st1" ));
215216
216- System .out .println (successAfterErrorTime .get () - firstErrorTime .get () + "ms" );
217- assertTrue (successAfterErrorTime .get () - firstErrorTime .get () < 250 );
218- assertTrue (successAfterErrorTime .get () - firstErrorTime .get () > 0 );
219- process .kill ();
220- assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STOPPED ));
217+ System .out .println (successAfterErrorTime .get () - firstErrorTime .get () + "ms" );
218+ assertTrue (successAfterErrorTime .get () - firstErrorTime .get () < 250 );
219+
220+ if (successAfterErrorTime .get () - firstErrorTime .get () == 0 ) {
221+ process .kill ();
222+ assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STOPPED ));
223+ continue ; // retry
224+ }
225+
226+ assertTrue (successAfterErrorTime .get () - firstErrorTime .get () > 0 );
227+ process .kill ();
228+ assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STOPPED ));
229+
230+ return ;
231+ }
232+
233+ fail (); // tried 5 times
221234 }
222235}
0 commit comments