You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by mp...@apache.org on 2018/11/28 14:45:51 UTC

[ambari] branch trunk updated: AMBARI-24796. Intermittent CredentialStoreTest test failure. (mpapirkovskyy) (#2661)

This is an automated email from the ASF dual-hosted git repository.

mpapirkovskyy pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 48bedc1  AMBARI-24796. Intermittent CredentialStoreTest test failure. (mpapirkovskyy) (#2661)
48bedc1 is described below

commit 48bedc1c1878f853f8660106559fb92ff3dfefdd
Author: Myroslav Papirkovskyi <mp...@apache.org>
AuthorDate: Wed Nov 28 16:45:46 2018 +0200

    AMBARI-24796. Intermittent CredentialStoreTest test failure. (mpapirkovskyy) (#2661)
---
 .../security/encryption/CredentialStoreTest.java   | 48 ++++++++++++++++++++--
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
index c9487ed..87406ab 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/encryption/CredentialStoreTest.java
@@ -18,6 +18,7 @@
 package org.apache.ambari.server.security.encryption;
 
 import java.io.File;
+import java.lang.reflect.Field;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.ambari.server.security.credential.Credential;
@@ -28,6 +29,8 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
+import com.google.common.base.Ticker;
+
 import junit.framework.Assert;
 
 public class CredentialStoreTest {
@@ -161,14 +164,35 @@ public class CredentialStoreTest {
     MasterKeyService masterKeyService = masterKeyServiceFactory.create(masterKey);
     CredentialStore credentialStore = credentialStoreServiceFactory.create(directory, masterKeyService);
 
+    // replace cache ticker with manual to avoid timeout issues during check
+    TestTicker ticker = new TestTicker(0);
+
+    Field cacheFiled = InMemoryCredentialStore.class.getDeclaredField("cache");
+    cacheFiled.setAccessible(true);
+
+    Object cache = cacheFiled.get(credentialStore);
+
+    Class localManualCacheClass = Class.forName("com.google.common.cache.LocalCache$LocalManualCache");
+    Field localCacheField = localManualCacheClass.getDeclaredField("localCache");
+    localCacheField.setAccessible(true);
+
+    Class localCacheClass = Class.forName("com.google.common.cache.LocalCache");
+    Object localCache = localCacheField.get(cache);
+
+    Field tickerField = localCacheClass.getDeclaredField("ticker");
+    tickerField.setAccessible(true);
+    tickerField.set(localCache, ticker);
+
     String password = "mypassword";
     credentialStore.addCredential("myalias", new GenericKeyCredential(password.toCharArray()));
     Assert.assertEquals(password, new String(credentialStore.getCredential("myalias").toValue()));
 
-    Thread.sleep(250);
+    // 250 msec
+    ticker.setCurrentNanos(250*1000*1000);
     Assert.assertEquals(password, new String(credentialStore.getCredential("myalias").toValue()));
 
-    Thread.sleep(550);
+    // 550 msec
+    ticker.setCurrentNanos(550*1000*1000);
     Assert.assertNull(password, credentialStore.getCredential("myalias"));
 
   }
@@ -220,7 +244,7 @@ public class CredentialStoreTest {
   private class InMemoryCredentialStoreServiceFactory implements CredentialStoreServiceFactory {
     @Override
     public CredentialStore create(File path, MasterKeyService masterKeyService) {
-      CredentialStore credentialStore = new InMemoryCredentialStore(500, TimeUnit.MILLISECONDS, true);
+      CredentialStore credentialStore = new InMemoryCredentialStore(500, TimeUnit.MILLISECONDS, false);
       credentialStore.setMasterKeyService(masterKeyService);
       return credentialStore;
     }
@@ -246,4 +270,22 @@ public class CredentialStoreTest {
     }
   }
 
+  private class TestTicker extends Ticker {
+
+    private long currentNanos;
+
+    public TestTicker(long currentNanos) {
+      this.currentNanos = currentNanos;
+    }
+
+    @Override
+    public long read() {
+      return currentNanos;
+    }
+
+    public void setCurrentNanos(long currentNanos) {
+      this.currentNanos = currentNanos;
+    }
+  }
+
 }