aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java
blob: 58ed7850b10cf01a8d5573d52a6155a8890c6a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * 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) {
			// not implemented
		}

		@Override
		public void beginTask(String title, int totalWork) {
			// not implemented
		}

		@Override
		public void update(int completed) {
			// not implemented
		}

		@Override
		public void endTask() {
			// not implemented
		}

		@Override
		public boolean isCancelled() {
			return cancelled;
		}

		@Override
		public void showDuration(boolean enabled) {
			// not implemented
		}
	}

	@Test
	public void testCancelInProcess() throws Exception {
		CancelledTestMonitor m = new CancelledTestMonitor();
		try (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();
		try (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());
		}
	}
}