Skip to content

Fixes a bug in transport context selection. #2357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.android.datatransport.runtime.scheduling.persistence;

import static com.google.android.datatransport.runtime.scheduling.persistence.SQLiteEventStore.tryWithCursor;
import static com.google.android.datatransport.runtime.scheduling.persistence.SchemaManager.SCHEMA_VERSION;
import static com.google.common.truth.Truth.assertThat;

Expand All @@ -31,6 +32,8 @@
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -370,6 +373,7 @@ public void loadActiveContexts_whenTwoContextsAvailable_shouldReturnThem() {
.setExtras("e1".getBytes(Charset.defaultCharset()))
.build();

// persist to ctx1 before ctx2
store.persist(ctx1, EVENT);
store.persist(ctx1, EVENT);
store.persist(ctx2, EVENT);
Expand All @@ -378,6 +382,47 @@ public void loadActiveContexts_whenTwoContextsAvailable_shouldReturnThem() {
assertThat(store.loadActiveContexts()).containsExactly(ctx1, ctx2);
}

@Test
public void loadActiveContexts_whenTwoContextsDifferInExtras_shouldReturnThem() {
TransportContext ctx1 =
TransportContext.builder().setBackendName("backend1").setExtras(null).build();
TransportContext ctx2 =
TransportContext.builder()
.setBackendName("backend1")
.setExtras("e1".getBytes(Charset.defaultCharset()))
.build();

// persist to ctx2 before ctx1
store.persist(ctx2, EVENT);
store.persist(ctx2, EVENT);
store.persist(ctx1, EVENT);
store.persist(ctx1, EVENT);

assertThat(store.loadActiveContexts()).containsExactly(ctx1, ctx2);

Map<Integer, Integer> eventsPerContext =
store.inTransaction(
db ->
tryWithCursor(
db.query(
"events",
new String[] {"context_id", "COUNT(_id)"},
null,
null,
"context_id",
null,
null),
cursor -> {
Map<Integer, Integer> results = new HashMap<>();
while (cursor.moveToNext()) {
results.put(cursor.getInt(0), cursor.getInt(1));
}
return results;
}));

assertThat(eventsPerContext).containsExactly(1, 2, 2, 2);
}

@Test
public void loadActiveContexts_whenTwoContextsWithOneAvailable_shouldReturnIt() {
TransportContext ctx1 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private Long getTransportContextId(SQLiteDatabase db, TransportContext transport
if (transportContext.getExtras() != null) {
selection.append(" and extras = ?");
selectionArgs.add(Base64.encodeToString(transportContext.getExtras(), Base64.DEFAULT));
} else {
selection.append(" and extras is null");
}

return tryWithCursor(
Expand Down Expand Up @@ -557,7 +559,8 @@ public <T> T runCriticalSection(CriticalSection<T> criticalSection) {
}
}

private <T> T inTransaction(Function<SQLiteDatabase, T> function) {
@VisibleForTesting
<T> T inTransaction(Function<SQLiteDatabase, T> function) {
SQLiteDatabase db = getDb();
db.beginTransaction();
try {
Expand Down Expand Up @@ -603,7 +606,8 @@ private long getPageCount() {
return getDb().compileStatement("PRAGMA page_count").simpleQueryForLong();
}

private static <T> T tryWithCursor(Cursor c, Function<Cursor, T> function) {
@VisibleForTesting
static <T> T tryWithCursor(Cursor c, Function<Cursor, T> function) {
try {
return function.apply(c);
} finally {
Expand Down