Skip to content

Commit

Permalink
chore: fix appengine.libraries unit test (#3747)
Browse files Browse the repository at this point in the history
* chore: fix appengine.libraries unit test

* add logger ID for concurrency debugging

* add inter-file-change grace period to prevent flakiness

* explain what the logger uuid is for
  • Loading branch information
diegomarquezp authored Feb 14, 2024
1 parent dfd3a10 commit 30d58f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void testObjectifyLibraryConfig() throws URISyntaxException {
assertThat(objectifyMavenCoordinates.getArtifactId(), is("objectify"));
DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(
objectifyMavenCoordinates.getVersion());
assertEquals(new DefaultArtifactVersion("6.0.9"), artifactVersion);
assertEquals(new DefaultArtifactVersion("6.1.1"), artifactVersion);
assertThat(objectifyMavenCoordinates.getType(), is("jar"));
assertNull(objectifyMavenCoordinates.getClassifier());

Expand All @@ -184,7 +184,7 @@ public void testObjectifyLibraryConfig() throws URISyntaxException {
assertThat(guavaMavenCoordinates.getRepository(), is("central"));
assertThat(guavaMavenCoordinates.getGroupId(), is("com.google.guava"));
assertThat(guavaMavenCoordinates.getArtifactId(), is("guava"));
assertThat(guavaMavenCoordinates.getVersion(), is("31.1-jre"));
assertThat(guavaMavenCoordinates.getVersion(), is("32.1.2-jre"));
assertThat(guavaMavenCoordinates.getType(), is("jar"));
assertNull(guavaMavenCoordinates.getClassifier());

Expand Down Expand Up @@ -242,7 +242,7 @@ public void testObjectify6LibraryConfig() throws URISyntaxException {
assertThat(guavaMavenCoordinates.getRepository(), is("central"));
assertThat(guavaMavenCoordinates.getGroupId(), is("com.google.guava"));
assertThat(guavaMavenCoordinates.getArtifactId(), is("guava"));
assertThat(guavaMavenCoordinates.getVersion(), is("31.1-jre"));
assertThat(guavaMavenCoordinates.getVersion(), is("32.1.2-jre"));
assertThat(guavaMavenCoordinates.getType(), is("jar"));
assertNull(guavaMavenCoordinates.getClassifier());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -58,13 +60,18 @@ public class DefaultAccountProviderTest {
private Runnable listenerFunction;

static final String TEMP_ADC_FILENAME = "test_adc.json";
private final Logger LOGGER = Logger.getLogger(this.getClass().getName());
private Logger LOGGER;
private static final long FILE_CHANGE_WAIT_MS = 500;

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Before
public void setup() throws IOException {
public void setup() throws IOException, InterruptedException {
// we add a small ID to the logger statements to distinguish among test cases
String uuid = UUID.randomUUID().toString();
String uuidShort = uuid.substring(uuid.lastIndexOf('-'));
LOGGER = Logger.getLogger(this.getClass().getName() + uuidShort);
LOGGER.info("setup()");
LOGGER.info("Temp folder location: " + tempFolder.getRoot().toPath().toString());
provider = new TestDefaultAccountProvider(tempFolder.newFile(TEMP_ADC_FILENAME).toPath(), tempFolder);
Expand All @@ -78,6 +85,11 @@ public void setup() throws IOException {
provider.addCredentialChangeListener(listenerFunction);
}

@After
public void tearDown() {
LOGGER.info("Test finished");
}

@Test
public void testFileWriteTriggersListeners() throws InterruptedException {
login(TOKEN_1);
Expand All @@ -102,7 +114,7 @@ public void testFileWriteTriggersListeners() throws InterruptedException {
}

@Test
public void testFileWriteChangesReturnedAccount() {
public void testFileWriteChangesReturnedAccount() throws InterruptedException {
Optional<Account> acct = provider.getAccount();
assertFalse(acct.isPresent());

Expand All @@ -127,7 +139,7 @@ public void testFileWriteChangesReturnedAccount() {
}

@Test
public void testFileWriteChangesReturnedCredential() {
public void testFileWriteChangesReturnedCredential() throws InterruptedException {
Optional<Credential> cred = provider.getCredential();
assertFalse(cred.isPresent());

Expand Down Expand Up @@ -156,7 +168,7 @@ private Path getTempAdcPath() {
return result;
}

private void login(String token) {
private void login(String token) throws InterruptedException {
LOGGER.info("login(" + token + ")");
try {
File adcFile = getTempAdcPath().toFile();
Expand All @@ -175,17 +187,19 @@ private void login(String token) {
LOGGER.info(
"login() post-write contents: " + Files.readAllLines(adcFile.toPath())
.stream().collect(Collectors.joining()));
Thread.sleep(FILE_CHANGE_WAIT_MS);
} catch (IOException ex) {
fail(ex.getMessage());
}
}

private void logout() {
private void logout() throws InterruptedException {
LOGGER.info("logout()");
File adcFile = getTempAdcPath().toFile();
if (adcFile.exists()) {
LOGGER.info("logout() delete file");
adcFile.delete();
Thread.sleep(FILE_CHANGE_WAIT_MS);
}
}

Expand Down

0 comments on commit 30d58f2

Please sign in to comment.