Skip to content

Commit

Permalink
Use count latch to delay request
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedol committed Apr 4, 2016
1 parent 58c85ca commit 23a9604
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;


/**
* @author Henry Addo
*/
Expand Down Expand Up @@ -101,7 +102,7 @@ public void shouldThrowExceptionWhenIdIsNull() throws IOException {
}

@Test
public void shouldSuccessfullyCreateSubscriber() throws IOException {
public void shouldSuccessfullyCreateSubscriber() throws IOException, InterruptedException {
mAsyncVotoApiClient
.createSubscriber("0243555333889", null, new Callback<CreateSubscriberResponse>() {
@Override
Expand All @@ -110,24 +111,22 @@ public void onResponse(Call<CreateSubscriberResponse> call,
CreateSubscriberResponse createSubscriberResponse = response.body();
assertNotNull(createSubscriberResponse);
assertNotNull(createSubscriberResponse.data);
assertEquals(430l, (long) createSubscriberResponse.data.id);
assertEquals(11, (long) createSubscriberResponse.data.id);
mCountDownLatch.countDown();
}

@Override
public void onFailure(Call<CreateSubscriberResponse> call, Throwable t) {
mCountDownLatch.countDown();

}
});

try {
mCountDownLatch.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}
mCountDownLatch.await(30, TimeUnit.SECONDS);
}

@Test
public void shouldSuccessfullyCreateBulkSubscribers() throws IOException {
public void shouldSuccessfullyCreateBulkSubscribers() throws IOException, InterruptedException {

mAsyncVotoApiClient.createBulkSubscribers("02345", null, null,
new Callback<CreateBulkSubscribersResponse>() {
Expand All @@ -153,15 +152,11 @@ public void onFailure(Call<CreateBulkSubscribersResponse> call,
}
});

try {
mCountDownLatch.await(40, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}
mCountDownLatch.await(40, TimeUnit.SECONDS);
}

@Test
public void shouldSuccessfullyDeleteSubscriber() throws IOException {
public void shouldSuccessfullyDeleteSubscriber() throws IOException, InterruptedException {

mAsyncVotoApiClient.deleteSubscriber(1l,
new Callback<DeleteSubscriberResponse>() {
Expand All @@ -172,17 +167,19 @@ public void onResponse(Call<DeleteSubscriberResponse> call,
assertNotNull(response);
assertEquals(200, (int) response.status);
assertEquals("Successfully deleted subscriber", response.message);
mCountDownLatch.countDown();
}

@Override
public void onFailure(Call<DeleteSubscriberResponse> call, Throwable t) {

mCountDownLatch.countDown();
}
});
mCountDownLatch.await(40, TimeUnit.SECONDS);
}

@Test
public void shouldSuccessfullyListSubscribers() throws IOException {
public void shouldSuccessfullyListSubscribers() throws IOException, InterruptedException {

mAsyncVotoApiClient.listSubscribers(10,
new Callback<ListSubscribersResponse>() {
Expand Down Expand Up @@ -226,49 +223,57 @@ public void onResponse(Call<ListSubscribersResponse> call,
assertEquals("Ejisu", response.data.subscribers.get(0).properties.location);
assertEquals("Out flying kites",
response.data.subscribers.get(0).properties.comments);
mCountDownLatch.countDown();
}

@Override
public void onFailure(Call<ListSubscribersResponse> call, Throwable t) {

mCountDownLatch.countDown();
}
});
mCountDownLatch.await(40, TimeUnit.SECONDS);
}

@Test
public void shouldSuccessfullyModifySubscriber() throws IOException {
public void shouldSuccessfullyModifySubscriber() throws IOException, InterruptedException {
mAsyncVotoApiClient
.modifySubscriberDetails(1l, null, new Callback<CreateSubscriberResponse>() {
@Override
public void onResponse(Call<CreateSubscriberResponse> call,
Response<CreateSubscriberResponse> resp) {
CreateSubscriberResponse createSubscriberResponse = resp.body();
assertNotNull(createSubscriberResponse);
mCountDownLatch.countDown();
}

@Override
public void onFailure(Call<CreateSubscriberResponse> call, Throwable t) {
// Do nothing
mCountDownLatch.countDown();
}
});
mCountDownLatch.await(40, TimeUnit.SECONDS);
}

@Test
public void shouldSuccessfullyListSubscriberDetails() throws IOException {
public void shouldSuccessfullyListSubscriberDetails() throws IOException, InterruptedException {
mAsyncVotoApiClient
.listSubscriberDetails(1l, new Callback<SubscriberDetailsResponse>() {
@Override
public void onResponse(Call<SubscriberDetailsResponse> call,
Response<SubscriberDetailsResponse> resp) {
SubscriberDetailsResponse createSubscriberResponse = resp.body();
assertNotNull(createSubscriberResponse);
mCountDownLatch.countDown();
}

@Override
public void onFailure(Call<SubscriberDetailsResponse> call, Throwable t) {
// Do nothing
mCountDownLatch.countDown();
}
});
mCountDownLatch.await(40, TimeUnit.SECONDS);
}

@Test
Expand Down
10 changes: 9 additions & 1 deletion voto/src/test/java/com/addhen/voto/sdk/test/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Executor;

import retrofit2.Retrofit;
import retrofit2.mock.BehaviorDelegate;
Expand Down Expand Up @@ -69,7 +70,6 @@ protected static String formatShowingDate(Date date) {
}

protected MockVotoService getMockVotoService() {
// Create a very simple Retrofit adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.demo.com")
.build();
Expand All @@ -86,4 +86,12 @@ protected MockVotoService getMockVotoService() {
.create(VotoService.class);
return new MockVotoService(votoServiceBehaviorDelegate, gsonDeserializer);
}

private static class Synchronous implements Executor {

@Override
public void execute(Runnable command) {
command.run();
}
}
}

0 comments on commit 23a9604

Please sign in to comment.