Skip to content

Commit 37c46c5

Browse files
kaibolayvkryachkodaymxn
authored
Update with latest changes from master (#4091)
* Deflake firebase_common HeartBeat tests. (#4083) The tests relied on `TestOnCompleteListener` that was not safe to call more than once since it was based on a count down latch. So reusing it multiple times would cause await() to return immediately. This change makes it so that a new latch is created for every await() call, making all await() calls work. Fixes: http://b/245956774 * Add Javadoc support to the DackkaPlugin (#4082) * Add util method for copying directories * Add javadoc support to our dackka plugin * Remove the extension check on fromDirectory * Add a note about cache compliance and the javadoc task * Add reference to kotlin stdlib package list (#4093) Co-authored-by: Vladimir Kryachko <[email protected]> Co-authored-by: Daymon <[email protected]>
1 parent a6c709b commit 37c46c5

File tree

7 files changed

+470
-186
lines changed

7 files changed

+470
-186
lines changed

buildSrc/src/main/java/com/google/firebase/gradle/plugins/DackkaGenerationTask.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ abstract class GenerateDocumentationTask @Inject constructor(
127127
"android" to "https://developer.android.com/reference/kotlin/",
128128
"google" to "https://developers.google.com/android/reference/",
129129
"firebase" to "https://firebase.google.com/docs/reference/kotlin/",
130-
"coroutines" to "https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/"
130+
"coroutines" to "https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/",
131+
"kotlin" to "https://kotlinlang.org/api/latest/jvm/stdlib/"
131132
)
132133

133134
return packageLists.get().map {

buildSrc/src/main/java/com/google/firebase/gradle/plugins/DackkaPlugin.kt

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,23 @@ tasks above). While we do not currently offer any configuration for the Dackka
9090
plugin, this could change in the future as needed. Currently, the DackkaPlugin
9191
provides sensible defaults to output directories, package lists, and so forth.
9292
93-
The DackkaPlugin also provides two extra tasks:
94-
[cleanDackkaDocumentation][registerCleanDackkaDocumentation] and
95-
[deleteDackkaGeneratedJavaReferences][registerDeleteDackkaGeneratedJavaReferencesTask].
93+
The DackkaPlugin also provides three extra tasks:
94+
[cleanDackkaDocumentation][registerCleanDackkaDocumentation],
95+
[copyJavaDocToCommonDirectory][registerCopyJavaDocToCommonDirectoryTask] and
96+
[copyKotlinDocToCommonDirectory][registerCopyKotlinDocToCommonDirectoryTask].
9697
9798
_cleanDackkaDocumentation_ is exactly what it sounds like, a task to clean up (delete)
9899
the output of Dackka. This is useful when testing Dackka outputs itself- and
99100
shouldn't be apart of the normal flow. The reasoning is that it would otherwise
100101
invalidate the gradle cache.
101102
102-
_deleteDackkaGeneratedJavaReferences_ is a temporary addition. Dackka generates
103-
two separate styles of docs for every source set: Java & Kotlin. Regardless of
104-
whether the source is in Java or Kotlin. The Java output is how the source looks
105-
from Java, and the Kotlin output is how the source looks from Kotlin. We publish
106-
these under two separate categories, which you can see here:
107-
[Java](https://firebase.google.com/docs/reference/android/packages)
108-
or
109-
[Kotlin](https://firebase.google.com/docs/reference/kotlin/packages).
110-
Although, we do not currently publish Java packages with Dackka- and will wait
111-
until we are more comfortable with the output of Dackka to do so. So until then,
112-
this task will remove all generate Java references from the Dackka output.
103+
_copyJavaDocToCommonDirectory_ copies the JavaDoc variant of the Dackka output for each sdk,
104+
and pastes it in a common directory under the root project's build directory. This makes it easier
105+
to zip the doc files for staging.
106+
107+
_copyKotlinDocToCommonDirectory_ copies the KotlinDoc variant of the Dackka output for each sdk,
108+
and pastes it in a common directory under the root project's build directory. This makes it easier
109+
to zip the doc files for staging.
113110
114111
Currently, the DackkaPlugin builds Java sources separate from Kotlin Sources. There is an open bug
115112
for Dackka in which hidden parent classes and annotations do not hide themselves from children classes.
@@ -125,16 +122,16 @@ abstract class DackkaPlugin : Plugin<Project> {
125122
val generateDocumentation = registerGenerateDackkaDocumentationTask(project)
126123
val outputDirectory = generateDocumentation.flatMap { it.outputDirectory }
127124
val firesiteTransform = registerFiresiteTransformTask(project, outputDirectory)
128-
val deleteJavaReferences = registerDeleteDackkaGeneratedJavaReferencesTask(project, outputDirectory)
129-
val copyOutputToCommonDirectory = registerCopyDackkaOutputToCommonDirectoryTask(project, outputDirectory)
125+
val copyJavaDocToCommonDirectory = registerCopyJavaDocToCommonDirectoryTask(project, outputDirectory)
126+
val copyKotlinDocToCommonDirectory = registerCopyKotlinDocToCommonDirectoryTask(project, outputDirectory)
130127

131128
project.tasks.register("kotlindoc") {
132129
group = "documentation"
133130
dependsOn(
134131
generateDocumentation,
135132
firesiteTransform,
136-
deleteJavaReferences,
137-
copyOutputToCommonDirectory
133+
copyJavaDocToCommonDirectory,
134+
copyKotlinDocToCommonDirectory
138135
)
139136
}
140137
} else {
@@ -234,38 +231,44 @@ abstract class DackkaPlugin : Plugin<Project> {
234231
// TODO(b/243833009): Make task cacheable
235232
private fun registerFiresiteTransformTask(project: Project, outputDirectory: Provider<File>) =
236233
project.tasks.register<FiresiteTransformTask>("firesiteTransform") {
234+
mustRunAfter("generateDackkaDocumentation")
235+
237236
dackkaFiles.set(outputDirectory)
238237
}
239238

240-
// If we decide to publish java variants, we'll need to address the generated format as well
241-
// TODO(b/243833009): Make task cacheable
242-
private fun registerDeleteDackkaGeneratedJavaReferencesTask(project: Project, outputDirectory: Provider<File>) =
243-
project.tasks.register<Delete>("deleteDackkaGeneratedJavaReferences") {
244-
mustRunAfter("generateDackkaDocumentation")
245-
246-
val filesWeDoNotNeed = listOf(
247-
"reference/client",
248-
"reference/com"
249-
)
250-
val filesToDelete = outputDirectory.map { dir ->
251-
filesWeDoNotNeed.map {
252-
project.files("${dir.path}/$it")
253-
}
239+
// TODO(b/246593212): Migrate doc files to single directory
240+
private fun registerCopyJavaDocToCommonDirectoryTask(project: Project, outputDirectory: Provider<File>) =
241+
project.tasks.register<Copy>("copyJavaDocToCommonDirectory") {
242+
/**
243+
* This is not currently cache compliant. The need for this property is
244+
* temporary while we test it alongside the current javaDoc task. Since it's such a
245+
* temporary behavior, losing cache compliance is fine for now.
246+
*/
247+
if (project.rootProject.findProperty("dackkaJavadoc") == "true") {
248+
mustRunAfter("firesiteTransform")
249+
250+
val outputFolder = project.file("${project.rootProject.buildDir}/firebase-kotlindoc/android")
251+
val clientFolder = outputDirectory.map { project.file("${it.path}/reference/client") }
252+
val comFolder = outputDirectory.map { project.file("${it.path}/reference/com") }
253+
254+
fromDirectory(clientFolder)
255+
fromDirectory(comFolder)
256+
257+
into(outputFolder)
254258
}
255-
256-
delete(filesToDelete)
257259
}
258260

259-
private fun registerCopyDackkaOutputToCommonDirectoryTask(project: Project, outputDirectory: Provider<File>) =
260-
project.tasks.register<Copy>("copyDackkaOutputToCommonDirectory") {
261-
mustRunAfter("deleteDackkaGeneratedJavaReferences")
261+
// TODO(b/246593212): Migrate doc files to single directory
262+
private fun registerCopyKotlinDocToCommonDirectoryTask(project: Project, outputDirectory: Provider<File>) =
263+
project.tasks.register<Copy>("copyKotlinDocToCommonDirectory") {
262264
mustRunAfter("firesiteTransform")
263265

264-
val referenceFolder = outputDirectory.map { project.file("${it.path}/reference") }
265266
val outputFolder = project.file("${project.rootProject.buildDir}/firebase-kotlindoc")
267+
val kotlinFolder = outputDirectory.map { project.file("${it.path}/reference/kotlin") }
268+
269+
fromDirectory(kotlinFolder)
266270

267-
from(referenceFolder)
268-
destinationDir = outputFolder
271+
into(outputFolder)
269272
}
270273

271274
// Useful for local testing, but may not be desired for standard use (that's why it's not depended on)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.google.firebase.gradle.plugins
2+
3+
import java.io.File
4+
import org.gradle.api.provider.Provider
5+
import org.gradle.api.tasks.Copy
6+
7+
fun Copy.fromDirectory(directory: Provider<File>) =
8+
from(directory) {
9+
into(directory.map { it.name })
10+
}

firebase-common/src/test/java/com/google/firebase/heartbeatinfo/DefaultHeartBeatControllerTest.java

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.heartbeatinfo;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.firebase.heartbeatinfo.TaskWaiter.await;
1819
import static java.nio.charset.StandardCharsets.UTF_8;
1920
import static org.mockito.ArgumentMatchers.anyLong;
2021
import static org.mockito.ArgumentMatchers.anyString;
@@ -26,7 +27,7 @@
2627
import android.content.Context;
2728
import android.content.SharedPreferences;
2829
import androidx.test.core.app.ApplicationProvider;
29-
import androidx.test.runner.AndroidJUnit4;
30+
import androidx.test.ext.junit.runners.AndroidJUnit4;
3031
import com.google.firebase.platforminfo.UserAgentPublisher;
3132
import java.io.ByteArrayOutputStream;
3233
import java.io.IOException;
@@ -35,27 +36,23 @@
3536
import java.util.Collections;
3637
import java.util.HashSet;
3738
import java.util.Set;
38-
import java.util.concurrent.ExecutionException;
39-
import java.util.concurrent.ExecutorService;
40-
import java.util.concurrent.LinkedBlockingQueue;
41-
import java.util.concurrent.ThreadPoolExecutor;
42-
import java.util.concurrent.TimeUnit;
39+
import java.util.concurrent.Executor;
40+
import java.util.concurrent.Executors;
41+
import java.util.concurrent.TimeoutException;
4342
import java.util.zip.GZIPOutputStream;
44-
import org.json.JSONException;
4543
import org.junit.Before;
4644
import org.junit.Test;
4745
import org.junit.runner.RunWith;
4846
import org.robolectric.annotation.Config;
4947

5048
@RunWith(AndroidJUnit4.class)
5149
public class DefaultHeartBeatControllerTest {
52-
private ExecutorService executor;
53-
private TestOnCompleteListener<Void> storeOnCompleteListener;
54-
private TestOnCompleteListener<String> getOnCompleteListener;
55-
private final String DEFAULT_USER_AGENT = "agent1";
56-
private HeartBeatInfoStorage storage = mock(HeartBeatInfoStorage.class);
57-
private UserAgentPublisher publisher = mock(UserAgentPublisher.class);
58-
private static Context applicationContext = ApplicationProvider.getApplicationContext();
50+
private static final String DEFAULT_USER_AGENT = "agent1";
51+
private final Executor executor = Executors.newSingleThreadExecutor();
52+
53+
private final HeartBeatInfoStorage storage = mock(HeartBeatInfoStorage.class);
54+
private final UserAgentPublisher publisher = mock(UserAgentPublisher.class);
55+
private final Context applicationContext = ApplicationProvider.getApplicationContext();
5956
private final Set<HeartBeatConsumer> logSources =
6057
new HashSet<HeartBeatConsumer>() {
6158
{
@@ -66,22 +63,18 @@ public class DefaultHeartBeatControllerTest {
6663

6764
@Before
6865
public void setUp() {
69-
executor = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
7066
when(publisher.getUserAgent()).thenReturn(DEFAULT_USER_AGENT);
71-
storeOnCompleteListener = new TestOnCompleteListener<>();
72-
getOnCompleteListener = new TestOnCompleteListener<>();
7367
heartBeatController =
7468
new DefaultHeartBeatController(
7569
() -> storage, logSources, executor, () -> publisher, applicationContext);
7670
}
7771

7872
@Test
79-
public void whenNoSource_dontStoreHeartBeat() throws ExecutionException, InterruptedException {
73+
public void whenNoSource_dontStoreHeartBeat() throws InterruptedException, TimeoutException {
8074
DefaultHeartBeatController controller =
8175
new DefaultHeartBeatController(
8276
() -> storage, new HashSet<>(), executor, () -> publisher, applicationContext);
83-
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
84-
storeOnCompleteListener.await();
77+
await(controller.registerHeartBeat());
8578
verify(storage, times(0)).storeHeartBeat(anyLong(), anyString());
8679
}
8780

@@ -99,31 +92,24 @@ public void getHeartBeatCode_noHeartBeat() {
9992

10093
@Config(sdk = 29)
10194
@Test
102-
public void generateHeartBeat_oneHeartBeat()
103-
throws ExecutionException, InterruptedException, JSONException, IOException {
95+
public void generateHeartBeat_oneHeartBeat() throws InterruptedException, TimeoutException {
10496
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
10597
returnResults.add(
106-
HeartBeatResult.create(
107-
"test-agent", new ArrayList<String>(Collections.singleton("2015-02-03"))));
98+
HeartBeatResult.create("test-agent", Collections.singletonList("2015-02-03")));
10899
when(storage.getAllHeartBeats()).thenReturn(returnResults);
109-
heartBeatController
110-
.registerHeartBeat()
111-
.addOnCompleteListener(executor, storeOnCompleteListener);
112-
storeOnCompleteListener.await();
100+
await(heartBeatController.registerHeartBeat());
113101
verify(storage, times(1)).storeHeartBeat(anyLong(), anyString());
114-
heartBeatController
115-
.getHeartBeatsHeader()
116-
.addOnCompleteListener(executor, getOnCompleteListener);
117102
String str =
118103
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-02-03\"]}],\"version\":\"2\"}";
119104
String expected = compress(str);
120-
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
105+
assertThat(await(heartBeatController.getHeartBeatsHeader()).replace("\n", ""))
106+
.isEqualTo(expected);
121107
}
122108

123109
@Config(sdk = 29)
124110
@Test
125111
public void firstNewThenOld_synchronizedCorrectly()
126-
throws ExecutionException, InterruptedException {
112+
throws InterruptedException, TimeoutException {
127113
Context context = ApplicationProvider.getApplicationContext();
128114
SharedPreferences heartBeatSharedPreferences =
129115
context.getSharedPreferences("testHeartBeat", Context.MODE_PRIVATE);
@@ -136,10 +122,8 @@ public void firstNewThenOld_synchronizedCorrectly()
136122
Base64.getUrlEncoder()
137123
.withoutPadding()
138124
.encodeToString("{\"heartbeats\":[],\"version\":\"2\"}".getBytes());
139-
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
140-
storeOnCompleteListener.await();
141-
controller.getHeartBeatsHeader().addOnCompleteListener(executor, getOnCompleteListener);
142-
String output = getOnCompleteListener.await();
125+
await(controller.registerHeartBeat());
126+
String output = await(controller.getHeartBeatsHeader());
143127
assertThat(output.replace("\n", "")).isNotEqualTo(emptyString);
144128
int heartBeatCode = controller.getHeartBeatCode("test").getCode();
145129
assertThat(heartBeatCode).isEqualTo(0);
@@ -148,7 +132,7 @@ public void firstNewThenOld_synchronizedCorrectly()
148132
@Config(sdk = 29)
149133
@Test
150134
public void firstOldThenNew_synchronizedCorrectly()
151-
throws ExecutionException, InterruptedException, IOException {
135+
throws InterruptedException, TimeoutException {
152136
Context context = ApplicationProvider.getApplicationContext();
153137
SharedPreferences heartBeatSharedPreferences =
154138
context.getSharedPreferences("testHeartBeat", Context.MODE_PRIVATE);
@@ -158,46 +142,36 @@ public void firstOldThenNew_synchronizedCorrectly()
158142
new DefaultHeartBeatController(
159143
() -> heartBeatInfoStorage, logSources, executor, () -> publisher, context);
160144
String emptyString = compress("{\"heartbeats\":[],\"version\":\"2\"}");
161-
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
162-
storeOnCompleteListener.await();
145+
await(controller.registerHeartBeat());
163146
int heartBeatCode = controller.getHeartBeatCode("test").getCode();
164147
assertThat(heartBeatCode).isEqualTo(2);
165-
controller.getHeartBeatsHeader().addOnCompleteListener(executor, getOnCompleteListener);
166-
String output = getOnCompleteListener.await();
148+
String output = await(controller.getHeartBeatsHeader());
167149
assertThat(output.replace("\n", "")).isEqualTo(emptyString);
168-
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
169-
storeOnCompleteListener.await();
170-
controller.getHeartBeatsHeader().addOnCompleteListener(executor, getOnCompleteListener);
171-
output = getOnCompleteListener.await();
150+
151+
await(controller.registerHeartBeat());
152+
await(controller.getHeartBeatsHeader());
172153
assertThat(output.replace("\n", "")).isEqualTo(emptyString);
173154
}
174155

175156
@Config(sdk = 29)
176157
@Test
177158
public void generateHeartBeat_twoHeartBeatsSameUserAgent()
178-
throws ExecutionException, InterruptedException, JSONException, IOException {
159+
throws InterruptedException, TimeoutException {
179160
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
180161
ArrayList<String> dateList = new ArrayList<>();
181162
dateList.add("2015-03-02");
182163
dateList.add("2015-03-01");
183164
returnResults.add(HeartBeatResult.create("test-agent", dateList));
184165
when(storage.getAllHeartBeats()).thenReturn(returnResults);
185-
heartBeatController
186-
.registerHeartBeat()
187-
.addOnCompleteListener(executor, storeOnCompleteListener);
188-
storeOnCompleteListener.await();
189-
heartBeatController
190-
.registerHeartBeat()
191-
.addOnCompleteListener(executor, storeOnCompleteListener);
192-
storeOnCompleteListener.await();
166+
await(heartBeatController.registerHeartBeat());
167+
await(heartBeatController.registerHeartBeat());
193168
verify(storage, times(2)).storeHeartBeat(anyLong(), anyString());
194-
heartBeatController
195-
.getHeartBeatsHeader()
196-
.addOnCompleteListener(executor, getOnCompleteListener);
169+
197170
String str =
198171
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\",\"2015-03-01\"]}],\"version\":\"2\"}";
199172
String expected = compress(str);
200-
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
173+
assertThat(await(heartBeatController.getHeartBeatsHeader()).replace("\n", ""))
174+
.isEqualTo(expected);
201175
}
202176

203177
private static String base64Encode(byte[] input) {
@@ -218,38 +192,28 @@ private static byte[] gzip(String input) {
218192
}
219193
}
220194

221-
private String compress(String str) throws IOException {
195+
private String compress(String str) {
222196
return base64Encode(gzip(str));
223197
}
224198

225199
@Config(sdk = 29)
226200
@Test
227201
public void generateHeartBeat_twoHeartBeatstwoUserAgents()
228-
throws ExecutionException, InterruptedException, JSONException, IOException {
202+
throws InterruptedException, TimeoutException {
229203
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
230204
returnResults.add(
231-
HeartBeatResult.create(
232-
"test-agent", new ArrayList<String>(Collections.singleton("2015-03-02"))));
205+
HeartBeatResult.create("test-agent", Collections.singletonList("2015-03-02")));
233206
returnResults.add(
234-
HeartBeatResult.create(
235-
"test-agent-1", new ArrayList<String>(Collections.singleton("2015-03-03"))));
207+
HeartBeatResult.create("test-agent-1", Collections.singletonList("2015-03-03")));
236208
when(storage.getAllHeartBeats()).thenReturn(returnResults);
237-
heartBeatController
238-
.registerHeartBeat()
239-
.addOnCompleteListener(executor, storeOnCompleteListener);
240-
storeOnCompleteListener.await();
241-
heartBeatController
242-
.registerHeartBeat()
243-
.addOnCompleteListener(executor, storeOnCompleteListener);
244-
storeOnCompleteListener.await();
245-
Thread.sleep(1000);
209+
await(heartBeatController.registerHeartBeat());
210+
await(heartBeatController.registerHeartBeat());
211+
246212
verify(storage, times(2)).storeHeartBeat(anyLong(), anyString());
247-
heartBeatController
248-
.getHeartBeatsHeader()
249-
.addOnCompleteListener(executor, getOnCompleteListener);
250213
String str =
251214
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\"]},{\"agent\":\"test-agent-1\",\"dates\":[\"2015-03-03\"]}],\"version\":\"2\"}";
252215
String expected = compress(str);
253-
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
216+
assertThat(await(heartBeatController.getHeartBeatsHeader()).replace("\n", ""))
217+
.isEqualTo(expected);
254218
}
255219
}

0 commit comments

Comments
 (0)