Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e80be78 authored by Garfield Tan's avatar Garfield Tan
Browse files

Deflake service tests.

Wait on write finish before proceeding.

Test: Medium tests pass.

Bug: 35796537
Change-Id: I0c3542dd83fe3115f2e4065f8ccdf758e8399fe4
parent e4ffd06b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -141,6 +141,7 @@ public class DocumentsProviderHelper {
        try (AutoCloseOutputStream out = new AutoCloseOutputStream(file)) {
            out.write(contents, 0, contents.length);
        }
        mClient.call("waitForWrite", null, null);
    }

    public byte[] readDocument(Uri documentUri) throws RemoteException, IOException {
+56 −39
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

public class StubProvider extends DocumentsProvider {

@@ -428,9 +429,7 @@ public class StubProvider extends DocumentsProvider {
        final ParcelFileDescriptor readPipe = pipe[0];
        final ParcelFileDescriptor writePipe = pipe[1];

        new Thread() {
            @Override
            public void run() {
        postToMainThread(() -> {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
@@ -469,8 +468,7 @@ public class StubProvider extends DocumentsProvider {
                        DocumentsContract.buildDocumentUri(mAuthority, document.documentId),
                        null, false);
            }
            }
        }.start();
        });

        return writePipe;
    }
@@ -509,6 +507,9 @@ public class StubProvider extends DocumentsProvider {
            case "setLoadingDuration":
                mLoadingDuration = extras.getLong(DocumentsContract.EXTRA_LOADING);
                return null;
            case "waitForWrite":
                waitForWrite();
                return null;
        }

        return null;
@@ -553,6 +554,22 @@ public class StubProvider extends DocumentsProvider {
        return out;
    }

    private void waitForWrite() {
        try {
            CountDownLatch latch = new CountDownLatch(1);
            postToMainThread(latch::countDown);
            latch.await();
            Log.d(TAG, "All writing is done.");
        } catch (InterruptedException e) {
            // should never happen
            throw new RuntimeException(e);
        }
    }

    private void postToMainThread(Runnable r) {
        new Handler(Looper.getMainLooper()).post(r);
    }

    public String createDocument(String parentId, String mimeType, String displayName, int flags,
            List<String> streamTypes) throws FileNotFoundException {