<commit_before>package org.msf.records.ui.sync;

import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.google.android.apps.common.testing.ui.espresso.Espresso;
import com.google.android.apps.common.testing.ui.espresso.IdlingPolicies;

import org.msf.records.App;
import org.msf.records.events.sync.SyncFailedEvent;
import org.msf.records.net.VolleySingleton;
import org.msf.records.sync.GenericAccountService;
import org.msf.records.sync.PatientDatabase;
import org.msf.records.sync.providers.Contracts;
import org.msf.records.ui.FunctionalTestCase;
import org.msf.records.utils.Logger;

import java.sql.SQLException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * A {@link FunctionalTestCase} that clears the application database as part of set up, allowing for
 * sync behavior to be tested more easily. This class does NOT currently clear ODK forms.
 *
 * <p>WARNING: Syncing requires the transfer of large quantities of data, so {@link SyncTestCase}s
 * will almost always be very large tests.
 */
public class SyncTestCase extends FunctionalTestCase {
    private static final Logger LOG = Logger.create();
    // The database may still be holding a lock after a test, so clearing the database may not
    // be successful right away.
    private static final int MAX_DATABASE_CLEAR_RETRIES = 10;

    @Override
    public void setUp() throws Exception {
        // Clearing the database can be flaky if previous tests are temporarily holding a DB lock,
        // so try a few times before failing.
        boolean cleared = false;
        int retriesRemaining = MAX_DATABASE_CLEAR_RETRIES;
        while (!cleared && retriesRemaining > 0) {
            try {
                clearDatabase();
                clearPreferences();
                cleared = true;
            } catch (SQLException e) {
                Thread.sleep(100);
                retriesRemaining--;
            }
        }

        super.setUp();
    }

    /** Cleans up post-test wifi state. Won't work during tearDown(). */
    public void cleanupWifi() {
        setWifiEnabled(true);
        // Wait until wifi connection has been re-established.<commit_msg>Fixing database lock avoidance logic in SyncTestCase<commit_after>package org.msf.records.ui.sync;

import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.google.android.apps.common.testing.ui.espresso.Espresso;
import com.google.android.apps.common.testing.ui.espresso.IdlingPolicies;

import net.sqlcipher.database.SQLiteException;

import org.msf.records.App;
import org.msf.records.events.sync.SyncFailedEvent;
import org.msf.records.net.VolleySingleton;
import org.msf.records.sync.GenericAccountService;
import org.msf.records.sync.PatientDatabase;
import org.msf.records.sync.providers.Contracts;
import org.msf.records.ui.FunctionalTestCase;
import org.msf.records.utils.Logger;

import java.sql.SQLException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * A {@link FunctionalTestCase} that clears the application database as part of set up, allowing for
 * sync behavior to be tested more easily. This class does NOT currently clear ODK forms.
 *
 * <p>WARNING: Syncing requires the transfer of large quantities of data, so {@link SyncTestCase}s
 * will almost always be very large tests.
 */
public class SyncTestCase extends FunctionalTestCase {
    private static final Logger LOG = Logger.create();
    // The database may still be holding a lock after a test, so clearing the database may not
    // be successful right away.
    private static final int MAX_DATABASE_CLEAR_RETRIES = 10;

    @Override
    public void setUp() throws Exception {
        // Clearing the database can be flaky if previous tests are temporarily holding a DB lock,
        // so try a few times before failing.
        boolean cleared = false;
        int retriesRemaining = MAX_DATABASE_CLEAR_RETRIES;
        while (!cleared && retriesRemaining > 0) {
            try {
                clearDatabase();
                clearPreferences();
                cleared = true;
            } catch (SQLiteException e) {
                Thread.sleep(500);
                retriesRemaining--;
            }
        }

        super.setUp();
    }

    /** Cleans up post-test wifi state. Won't work during tearDown(). */
    public void cleanupWifi() {
        setWifiEnabled(true);
        // Wait until wifi connection has been re-established.
{'fraction_non_alphanumeric': 0.08643006263048017, 'fraction_numerical': 0.0025052192066805845, 'mean_word_length': 3.1624674196351, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5711812', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1313, 'n_words': 401}