From 52fef356f4af5b20cd0f0aa36e6d3a65d5b58ef1 Mon Sep 17 00:00:00 2001 From: Alex Buynytskyy Date: Tue, 6 Sep 2022 14:46:41 -0700 Subject: [PATCH] Flaky test fixes. - don't crash the whole suite in case of test failure, - wait for 100ms for a second update call. Bug: 244589479 Fixes: 244589479 Test: atest --iterations 100 DeviceConfigTest Change-Id: Ia985a51d097919bc2885f63d0baf5a77d41ca786 --- .../android/provider/DeviceConfigTest.java | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/core/tests/coretests/src/android/provider/DeviceConfigTest.java b/core/tests/coretests/src/android/provider/DeviceConfigTest.java index 5b3be58ae0de2..352c6a736af14 100644 --- a/core/tests/coretests/src/android/provider/DeviceConfigTest.java +++ b/core/tests/coretests/src/android/provider/DeviceConfigTest.java @@ -39,8 +39,11 @@ import org.junit.runner.RunWith; import java.util.HashMap; import java.util.Map; import java.util.Objects; -import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; /** Tests that ensure appropriate settings are backed up. */ @Presubmit @@ -710,13 +713,12 @@ public class DeviceConfigTest { @Test public void onPropertiesChangedListener_setPropertyCallback() throws InterruptedException { - final CountDownLatch countDownLatch = new CountDownLatch(1); + final AtomicReference changedProperties = new AtomicReference<>(); + final CompletableFuture completed = new CompletableFuture<>(); DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { - assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); - assertThat(properties.getKeyset()).contains(KEY); - assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE); - countDownLatch.countDown(); + changedProperties.set(properties); + completed.complete(true); }; try { @@ -724,9 +726,15 @@ public class DeviceConfigTest { Objects.requireNonNull(ActivityThread.currentApplication()).getMainExecutor(), changeListener); DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); - assertThat(countDownLatch.await( + + assertThat(completed.get( WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); - } catch (InterruptedException e) { + + Properties properties = changedProperties.get(); + assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); + assertThat(properties.getKeyset()).contains(KEY); + assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE); + } catch (ExecutionException | TimeoutException | InterruptedException e) { Assert.fail(e.getMessage()); } finally { DeviceConfig.removeOnPropertiesChangedListener(changeListener); @@ -735,7 +743,6 @@ public class DeviceConfigTest { @Test public void onPropertiesChangedListener_setPropertiesCallback() { - final CountDownLatch countDownLatch = new CountDownLatch(1); DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); DeviceConfig.setProperty(NAMESPACE, KEY2, VALUE2, false); @@ -744,16 +751,12 @@ public class DeviceConfigTest { keyValues.put(KEY3, VALUE3); Properties setProperties = new Properties(NAMESPACE, keyValues); + final AtomicReference changedProperties = new AtomicReference<>(); + final CompletableFuture completed = new CompletableFuture<>(); + DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { - assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); - assertThat(properties.getKeyset()).containsExactly(KEY, KEY2, KEY3); - // KEY updated from VALUE to VALUE2 - assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE2); - // KEY2 deleted (returns default_value) - assertThat(properties.getString(KEY2, "default_value")).isEqualTo("default_value"); - //KEY3 added with VALUE3 - assertThat(properties.getString(KEY3, "default_value")).isEqualTo(VALUE3); - countDownLatch.countDown(); + changedProperties.set(properties); + completed.complete(true); }; try { @@ -761,9 +764,28 @@ public class DeviceConfigTest { Objects.requireNonNull(ActivityThread.currentApplication()).getMainExecutor(), changeListener); DeviceConfig.setProperties(setProperties); - assertThat(countDownLatch.await( + + assertThat(completed.get( WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); - } catch (InterruptedException | DeviceConfig.BadConfigException e) { + + if (changedProperties.get().getKeyset().size() != 3) { + // Sometimes there are a few onChanged callback calls. Let's wait a bit more. + final int oneMoreOnChangedDelayMs = 100; + Thread.currentThread().sleep(oneMoreOnChangedDelayMs); + } + + Properties properties = changedProperties.get(); + assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); + assertThat(properties.getKeyset()).containsExactly(KEY, KEY2, KEY3); + // KEY updated from VALUE to VALUE2 + assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE2); + // KEY2 deleted (returns default_value) + assertThat(properties.getString(KEY2, "default_value")).isEqualTo("default_value"); + // KEY3 added with VALUE3 + assertThat(properties.getString(KEY3, "default_value")).isEqualTo(VALUE3); + } catch (ExecutionException | TimeoutException | InterruptedException e) { + Assert.fail(e.getMessage()); + } catch (DeviceConfig.BadConfigException e) { Assert.fail(e.getMessage()); } finally { DeviceConfig.removeOnPropertiesChangedListener(changeListener);