]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
Transaction saver task: add 30% probability of exception when simulating network...
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / SendTransactionTask.java
index f4c7ce1fdd48ced4e421e6ea0edad468fc66a0f5..4c3bc477b8ddfeb43c82bd5e9268cb6f0105b232 100644 (file)
@@ -60,6 +60,19 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         mProfile = profile;
     }
     private boolean sendOK() throws IOException {
+//        if (BuildConfig.DEBUG) {
+//            try {
+//                Thread.sleep(1500);
+//                if (Math.random() > 0.3)
+//                    throw new RuntimeException("Simulated test exception");
+//            }
+//            catch (InterruptedException ex) {
+//                Logger.debug("network", ex.toString());
+//            }
+//
+//            return true;
+//        }
+
         HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("PUT");
         http.setRequestProperty("Content-Type", "application/json");
@@ -76,7 +89,8 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         http.setDoInput(true);
         http.addRequestProperty("Content-Length", String.valueOf(bodyBytes.length));
 
-        debug("network", "request header: " + http.getRequestProperties().toString());
+        debug("network", "request header: " + http.getRequestProperties()
+                                                  .toString());
 
         try (OutputStream req = http.getOutputStream()) {
             debug("network", "Request body: " + body);
@@ -119,20 +133,23 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
 
         UrlEncodedFormData params = new UrlEncodedFormData();
         params.addPair("_formid", "identify-add");
-        if (token != null) params.addPair("_token", token);
+        if (token != null)
+            params.addPair("_token", token);
         params.addPair("date", Globals.formatLedgerDate(ltr.getDate()));
         params.addPair("description", ltr.getDescription());
         for (LedgerTransactionAccount acc : ltr.getAccounts()) {
             params.addPair("account", acc.getAccountName());
             if (acc.isAmountSet())
                 params.addPair("amount", String.format(Locale.US, "%1.2f", acc.getAmount()));
-            else params.addPair("amount", "");
+            else
+                params.addPair("amount", "");
         }
 
         String body = params.toString();
         http.addRequestProperty("Content-Length", String.valueOf(body.length()));
 
-        debug("network", "request header: " + http.getRequestProperties().toString());
+        debug("network", "request header: " + http.getRequestProperties()
+                                                  .toString());
 
         try (OutputStream req = http.getOutputStream()) {
             debug("network", "Request body: " + body);
@@ -202,10 +219,10 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
             if (!sendOK()) {
                 int tried = 0;
                 while (!legacySendOK()) {
-                        tried++;
-                        if (tried >= 2)
-                            throw new IOException(String.format("aborting after %d tries", tried));
-                        sleep(100);
+                    tried++;
+                    if (tried >= 2)
+                        throw new IOException(String.format("aborting after %d tries", tried));
+                    sleep(100);
                 }
             }
         }