Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Bella committed Mar 18, 2016
2 parents a11932d + 86f704d commit baa2c90
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
31 changes: 18 additions & 13 deletions library/src/main/java/com/deltadna/android/sdk/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import com.deltadna.android.sdk.net.NetworkManager;
import com.deltadna.android.sdk.net.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -198,22 +198,27 @@ private final class Upload implements Runnable {
@Override
public void run() {
store.swap();

final Iterator<String> it = store.read().iterator();
final JSONArray events = new JSONArray();
while (it.hasNext()) {
events.put(it.next());
it.remove();
}

if (events.length() == 0) {

final List<String> events = store.read();
if (events.isEmpty()) {
Log.d(TAG, "No events to upload");
return;
}

final JSONObject payload = new JSONObject();

final StringBuilder builder = new StringBuilder("{\"eventList\":[");
final Iterator<String> it = events.iterator();
while (it.hasNext()) {
builder.append(it.next());
builder.append(',');

it.remove();
}
builder.deleteCharAt(builder.length() - 1);
builder.append("]}");

final JSONObject payload;
try {
payload.put("eventList", events);
payload = new JSONObject(builder.toString());
} catch (JSONException e) {
Log.w(TAG, e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.deltadna.android.sdk.net.NetworkManager;
import com.deltadna.android.sdk.net.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
Expand Down Expand Up @@ -93,10 +92,8 @@ public void act(RequestListener listener) {
uut.start(0, 1);
Thread.sleep(2200);

final JSONObject expected1 = new JSONObject().put("eventList", new JSONArray()
.put(events1.get(0)).put(events1.get(1)));
final JSONObject expected2 = new JSONObject().put("eventList", new JSONArray()
.put(events2.get(0)));
final String expected1 = "{\"eventList\":[{\"value\":0},{\"value\":1}]}";
final String expected2 = "{\"eventList\":[{\"value\":0}]}";

verify(store, times(3)).swap();
verify(store, times(3)).read();
Expand All @@ -109,7 +106,7 @@ public boolean matches(Object argument) {
if (run != 2) { // no idea why we get a 3 runs and times(2) works
assertThat(argument).isInstanceOf(JSONObject.class);
assertThat(argument.toString()).isEqualTo(
(run == 0 ? expected1 : expected2).toString());
run == 0 ? expected1 : expected2);
}
run++;
return true;
Expand Down

0 comments on commit baa2c90

Please sign in to comment.