Skip to content

Storage: Add TestUtil.await #487

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 5 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -21,7 +21,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

Expand Down Expand Up @@ -58,19 +57,12 @@ public void tearDown() {
@SuppressWarnings("ConstantConditions")
@Test
public void deleteBlob() throws Exception {
final MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("deleteBlob", false);

MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("deleteBlob", false);
Task<StringBuilder> task = TestCommandHelper.deleteBlob();
for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("deleteBlob", task.getResult().toString());
return;
}
Thread.sleep(1);
}
assert (false);

TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("deleteBlob", task.getResult().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -74,8 +74,7 @@ public void tearDown() {
public void streamDownload() throws Exception {
System.out.println("Starting test streamDownload.");

final MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("streamDownload", true);
final boolean[] completeHandlerInvoked = new boolean[] {false};

Task<StreamDownloadResponse> task =
Expand All @@ -90,18 +89,11 @@ public void streamDownload() throws Exception {
"image.jpg",
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownload", task.getResult());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownload", task.getResult());
assertTrue(completeHandlerInvoked[0]);
}

@Test
Expand Down Expand Up @@ -158,7 +150,7 @@ public void streamDownloadStateVerification() throws Exception {
public void streamDownloadWithResume() throws Exception {
System.out.println("Starting test streamDownloadWithResume.");

final MockConnectionFactory factory =
MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownloadWithResume", true);
final boolean[] completeHandlerInvoked = new boolean[] {false};

Expand All @@ -174,97 +166,67 @@ public void streamDownloadWithResume() throws Exception {
"image.jpg",
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithResume", task.getResult());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithResume", task.getResult());
assertTrue(completeHandlerInvoked[0]);
}

@Test
public void streamDownloadWithResumeAndCancel() throws Exception {
System.out.println("Starting test streamDownloadWithResumeAndCancel.");

final MockConnectionFactory factory =
MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownloadWithResumeAndCancel", true);

Task<StreamDownloadResponse> task =
TestDownloadHelper.streamDownload(
bitmap -> fail("Should not get called since we cancelled."), null, "image.jpg", 260000);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithResumeAndCancel", task.getResult());
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithResumeAndCancel", task.getResult());
}

@Test
public void streamDownloadCanceled() throws Exception {
System.out.println("Starting test streamDownloadCanceled.");

final MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("streamDownload", true);

Task<StreamDownloadResponse> task =
TestDownloadHelper.streamDownload(
bitmap -> fail("Should not get called since we cancelled."), null, "image.jpg", 0);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadCanceled", task.getResult());
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadCanceled", task.getResult());
}

@Test
public void streamDownloadWithETagChange() throws Exception {
System.out.println("Starting test streamDownloadWithETagChange.");

final MockConnectionFactory factory =
MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownloadWithETagChange", true);

Task<StreamDownloadResponse> task =
TestDownloadHelper.streamDownload(null, null, "image.jpg", -1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithETagChange", task.getResult());
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("streamDownloadWithETagChange", task.getResult());
}

@Test
public void emptyStreamDownload() throws Exception {
System.out.println("Starting test emptyStreamDownload.");

final MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("emptyStreamDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("emptyStreamDownload", true);
final boolean[] completeHandlerInvoked = new boolean[] {false};

Task<StreamDownloadResponse> task =
Expand All @@ -277,26 +239,18 @@ public void emptyStreamDownload() throws Exception {
"empty.dat",
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("emptyStreamDownload", task.getResult());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("emptyStreamDownload", task.getResult());
assertTrue(completeHandlerInvoked[0]);
}

@Test
public void byteDownload() throws Exception {
System.out.println("Starting test byteDownload.");

final MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("streamDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("streamDownload", true);

Semaphore semaphore =
TestDownloadHelper.byteDownload(
Expand All @@ -322,7 +276,7 @@ public void fileDownload() throws Exception {
outputFile.delete();
}
Uri destinationUri = Uri.fromFile(outputFile);
final MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("fileDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("fileDownload", true);

final boolean[] completeHandlerInvoked = new boolean[] {false};

Expand All @@ -336,18 +290,11 @@ public void fileDownload() throws Exception {
},
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownload", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownload", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
}

@Test
Expand All @@ -360,8 +307,7 @@ public void fileDownloadResume() throws Exception {
outputFile.delete();
}
Uri destinationUri = Uri.fromFile(outputFile);
final MockConnectionFactory factory =
NetworkLayerMock.ensureNetworkMock("fileDownloadResume", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("fileDownloadResume", true);

final boolean[] completeHandlerInvoked = new boolean[] {false};

Expand All @@ -375,18 +321,11 @@ public void fileDownloadResume() throws Exception {
},
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownloadResume", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownloadResume", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
}

@Test
Expand All @@ -399,7 +338,7 @@ public void emptyDownload() throws Exception {
outputFile.delete();
}
Uri destinationUri = Uri.fromFile(outputFile);
final MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("emptyDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("emptyDownload", true);
final boolean[] completeHandlerInvoked = new boolean[] {false};

Task<StringBuilder> task =
Expand All @@ -412,18 +351,11 @@ public void emptyDownload() throws Exception {
},
-1);

for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("emptyDownload", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
return;
}
Thread.sleep(1);
}
fail();
TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("emptyDownload", task.getResult().toString());
assertTrue(completeHandlerInvoked[0]);
}

@Test
Expand All @@ -436,21 +368,15 @@ public void fileDownloadCanceledImmediately() throws Exception {
outputFile.delete();
}
Uri destinationUri = Uri.fromFile(outputFile);
final MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("fileDownload", true);
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("fileDownload", true);

Task<StringBuilder> task =
TestDownloadHelper.fileDownload(
destinationUri, () -> fail("Should not run since we cancelled the task."), 0);
for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
// success!
factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownloadCanceled", task.getResult().toString());
return;
}
Thread.sleep(1);
}
fail();

TestUtil.await(task);

factory.verifyOldMock();
TestUtil.verifyTaskStateChanges("fileDownloadCanceled", task.getResult().toString());
}
}
Loading