DonalEvans commented on a change in pull request #7067:
URL: https://github.com/apache/geode/pull/7067#discussion_r741233211



##########
File path: 
geode-core/src/test/java/org/apache/geode/cache/client/internal/ConnectionFactoryImplTest.java
##########
@@ -100,4 +107,32 @@ public void authenticateIfRequired() {
     verify(pool).executeOn(any(Connection.class), any(Op.class));
     verify(server).setUserId(123L);
   }
+
+  @Rule
+  public ExecutorServiceRule executor = new ExecutorServiceRule();
+
+  @Test
+  public void getSetServerIdShouldBeSynchronizedToAvoidExtraLoginRequest() 
throws Exception {
+    when(pool.isUsedByGateway()).thenReturn(false);
+    when(pool.getMultiuserAuthentication()).thenReturn(false);
+    when(pool.executeOn(any(Connection.class), any())).thenReturn(123L);
+    when(server.getRequiresCredentials()).thenReturn(true);
+    when(connection.getServer()).thenReturn(server);
+
+    doCallRealMethod().when(server).setUserId(anyLong());
+    doCallRealMethod().when(server).getUserId();
+    // set up the initial userId to be -1
+    server.setUserId(-1);
+
+    CompletableFuture<Void> future1 =
+        executor.runAsync(() -> factory.authenticateIfRequired(connection));
+    CompletableFuture<Void> future2 =
+        executor.runAsync(() -> factory.authenticateIfRequired(connection));
+    future1.get();
+    future2.get();
+
+    // setUserId should only be called once by this two threads. The other time
+    // is called by the test setup.
+    verify(server, times(2)).setUserId(anyLong());

Review comment:
       I don't know if it's actually better, since clearing invocations on 
mocks is not a best practice, but to make this verification more intuitive, you 
can call `Mockito.clearInvocations(server);` on line 126 and then have this 
verification just be: `verify(server).setUserId(anyLong());` and remove the 
comment explaining why it's 2.

##########
File path: 
geode-junit/src/main/java/org/apache/geode/security/templates/TrackableUserPasswordAuthInit.java
##########
@@ -0,0 +1,38 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ *
+ */
+
+package org.apache.geode.security.templates;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.geode.LogWriter;
+import org.apache.geode.security.AuthenticationFailedException;
+
+public class TrackableUserPasswordAuthInit extends UserPasswordAuthInit {
+  public static AtomicInteger timeInitialized = new AtomicInteger(0);

Review comment:
       This might be better named "timesInitialized" to avoid confusion, since 
its current name might imply that this is the time that the class was 
initialized.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to