Skip to content

Commit 4524ae7

Browse files
authored
Various updates to datatransport (#658)
* Fix data race in initialize(). The issue is that we were not doing the double-checked locking properly, which could manifest in instantiating multiple instances of TransportRuntime due to concurrent calls to initialize(). * Set custom user-agent on requests to CCT.
1 parent 4751124 commit 4524ae7

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

transport/transport-backend-cct/gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
version=2.0.1
16-
latestReleasedVersion=2.0.0
15+
version=2.0.2
16+
latestReleasedVersion=2.0.1
1717
firebaseSkipPreguard=false

transport/transport-backend-cct/src/main/java/com/google/android/datatransport/cct/CctTransportBackend.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.net.NetworkInfo;
2020
import android.os.Build;
2121
import androidx.annotation.VisibleForTesting;
22+
import com.google.android.datatransport.backend.cct.BuildConfig;
2223
import com.google.android.datatransport.cct.proto.AndroidClientInfo;
2324
import com.google.android.datatransport.cct.proto.BatchedLogRequest;
2425
import com.google.android.datatransport.cct.proto.ClientInfo;
@@ -217,6 +218,8 @@ private BackendResponse doSend(BatchedLogRequest requestBody) throws IOException
217218
connection.setDoOutput(true);
218219
connection.setInstanceFollowRedirects(false);
219220
connection.setRequestMethod("POST");
221+
connection.setRequestProperty(
222+
"User-Agent", String.format("datatransport/%s android/", BuildConfig.VERSION_NAME));
220223
connection.setRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
221224
connection.setRequestProperty(CONTENT_TYPE_HEADER_KEY, PROTOBUF_CONTENT_TYPE);
222225

transport/transport-backend-cct/src/test/java/com/google/android/datatransport/cct/CctTransportBackendTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.net.NetworkInfo;
3232
import com.github.tomakehurst.wiremock.http.Request;
3333
import com.github.tomakehurst.wiremock.junit.WireMockRule;
34+
import com.google.android.datatransport.backend.cct.BuildConfig;
3435
import com.google.android.datatransport.cct.ProtoMatchers.PredicateMatcher;
3536
import com.google.android.datatransport.cct.proto.BatchedLogRequest;
3637
import com.google.android.datatransport.cct.proto.LogEvent;
@@ -126,6 +127,9 @@ public void testSuccessLoggingRequest() {
126127

127128
verify(
128129
postRequestedFor(urlEqualTo("/api"))
130+
.withHeader(
131+
"User-Agent",
132+
equalTo(String.format("datatransport/%s android/", BuildConfig.VERSION_NAME)))
129133
.withHeader("Content-Type", equalTo("application/x-protobuf"))
130134
.andMatching(batchRequestMatcher.test(batch -> batch.getLogRequestCount() == 1))
131135
.andMatching(

transport/transport-backend-cct/transport-backend-cct.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ android {
5454
minSdkVersion 14
5555
targetSdkVersion 28
5656
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
57+
versionName version
5758
}
5859
compileOptions {
5960
sourceCompatibility = '1.8'

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/TransportRuntime.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@Singleton
3737
public class TransportRuntime implements TransportInternal {
3838

39-
private static volatile TransportRuntimeComponent INSTANCE = null;
39+
private static volatile TransportRuntimeComponent instance = null;
4040

4141
private final Clock eventClock;
4242
private final Clock uptimeClock;
@@ -61,12 +61,14 @@ public class TransportRuntime implements TransportInternal {
6161
* <p>This method must be called before {@link #getInstance()}.
6262
*/
6363
public static void initialize(Context applicationContext) {
64-
if (INSTANCE == null) {
64+
if (instance == null) {
6565
synchronized (TransportRuntime.class) {
66-
INSTANCE =
67-
DaggerTransportRuntimeComponent.builder()
68-
.setApplicationContext(applicationContext)
69-
.build();
66+
if (instance == null) {
67+
instance =
68+
DaggerTransportRuntimeComponent.builder()
69+
.setApplicationContext(applicationContext)
70+
.build();
71+
}
7072
}
7173
}
7274
// send warning
@@ -78,7 +80,7 @@ public static void initialize(Context applicationContext) {
7880
* @throws IllegalStateException if {@link #initialize(Context)} is not called before this method.
7981
*/
8082
public static TransportRuntime getInstance() {
81-
TransportRuntimeComponent localRef = INSTANCE;
83+
TransportRuntimeComponent localRef = instance;
8284
if (localRef == null) {
8385
throw new IllegalStateException("Not initialized!");
8486
}
@@ -91,14 +93,14 @@ static void withInstance(TransportRuntimeComponent component, Callable<Void> cal
9193
throws Throwable {
9294
TransportRuntimeComponent original;
9395
synchronized (TransportRuntime.class) {
94-
original = INSTANCE;
95-
INSTANCE = component;
96+
original = instance;
97+
instance = component;
9698
}
9799
try {
98100
callable.call();
99101
} finally {
100102
synchronized (TransportRuntime.class) {
101-
INSTANCE = original;
103+
instance = original;
102104
}
103105
}
104106
}

0 commit comments

Comments
 (0)