diff options
author | Ivan Frade <ifrade@google.com> | 2022-01-27 11:42:54 -0500 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2022-01-27 11:42:54 -0500 |
commit | 27e554e465d043ed12914fd42554164c9333fc44 (patch) | |
tree | 12278e0a072e5b9886577fc10be73c0f54210b7f /org.eclipse.jgit.test | |
parent | 75e7d08480eb368436458504c103fe3b68bd6089 (diff) | |
parent | 1a86c1044dfb8300241728571a865971aa9f1a66 (diff) | |
download | jgit-27e554e465d043ed12914fd42554164c9333fc44.tar.gz jgit-27e554e465d043ed12914fd42554164c9333fc44.zip |
Merge "PackOutputStream: Extract cancellation and digest to superclass"
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java new file mode 100644 index 0000000000..a05ab0c44a --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2022, Tencent. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.internal.storage.io; + +import static org.eclipse.jgit.internal.storage.io.CancellableDigestOutputStream.BYTES_TO_WRITE_BEFORE_CANCEL_CHECK; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.io.InterruptedIOException; + +import org.eclipse.jgit.lib.ProgressMonitor; +import org.eclipse.jgit.util.io.NullOutputStream; +import org.junit.Test; + +public class CancellableDigestOutputStreamTest { + private static class CancelledTestMonitor implements ProgressMonitor { + + private boolean cancelled = false; + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public void start(int totalTasks) { + } + + @Override + public void beginTask(String title, int totalWork) { + } + + @Override + public void update(int completed) { + } + + @Override + public void endTask() { + } + + @Override + public boolean isCancelled() { + return cancelled; + } + } + + @Test + public void testCancelInProcess() throws Exception { + CancelledTestMonitor m = new CancelledTestMonitor(); + CancellableDigestOutputStream out = new CancellableDigestOutputStream(m, + NullOutputStream.INSTANCE); + + byte[] KB = new byte[1024]; + int triggerCancelWriteCnt = BYTES_TO_WRITE_BEFORE_CANCEL_CHECK + / KB.length; + for (int i = 0; i < triggerCancelWriteCnt + 1; i++) { + out.write(KB); + } + assertTrue(out.length() > BYTES_TO_WRITE_BEFORE_CANCEL_CHECK); + m.setCancelled(true); + + for (int i = 0; i < triggerCancelWriteCnt - 1; i++) { + out.write(KB); + } + + long lastLength = out.length(); + assertThrows(InterruptedIOException.class, () -> { + out.write(1); + }); + assertEquals(lastLength, out.length()); + + assertThrows(InterruptedIOException.class, () -> { + out.write(new byte[1]); + }); + assertEquals(lastLength, out.length()); + } + + @Test + public void testTriggerCheckAfterSingleBytes() throws Exception { + CancelledTestMonitor m = new CancelledTestMonitor(); + CancellableDigestOutputStream out = new CancellableDigestOutputStream(m, + NullOutputStream.INSTANCE); + + byte[] bytes = new byte[BYTES_TO_WRITE_BEFORE_CANCEL_CHECK + 1]; + m.setCancelled(true); + + assertThrows(InterruptedIOException.class, () -> { + out.write(bytes); + }); + assertEquals(BYTES_TO_WRITE_BEFORE_CANCEL_CHECK, out.length()); + } +} |