Skip to main content
summaryrefslogtreecommitdiffstats
blob: 73a9591ec8124412fc70dbba6325cfbd56eaf749 (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2017 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core.data;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;

public class TaskDataStoreTest {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	private static final int THREAD_COUNT = 10;

	private static final long DELAY = 100;

	private static final String DATA_XML_CONTENT = "test";

	private static final TaskData TASK_DATA = newTaskData();

	private static final TaskDataState TEST_STATE = new TaskDataState("connectorKind", "repositoryUrl", "taskId");

	@Test
	public void getTaskDataStateByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(getTaskDataState(file));

		assertNoFailures(failures);
	}

	@Test
	public void discardEditsByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(discardEdits(file));

		assertNoFailures(failures);
	}

	@Test
	public void putEditsByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(putEdits(file));

		assertNoFailures(failures);
	}

	@Test
	public void putTaskDataSetLastReadUserByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(putTaskData(file));

		assertNoFailures(failures);
	}

	@Test
	public void setTaskDataByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(setTaskData(file));

		assertNoFailures(failures);
	}

	@Test
	public void putTaskDataByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(putTaskDataState(file));

		assertNoFailures(failures);
	}

	@Test
	public void deleteTaskDataByMultipleThreads() throws Exception {
		File file = newTaskDataZipFile();

		List<Throwable> failures = runSameByMultipleThreads(deleteTaskData(file));

		assertNoFailures(failures);
	}

	@Test
	public void manipulateTaskDataConcurrently() throws Exception {
		File file = newTaskDataZipFile();

		// run multiple times to increase chance of a failure
		for (int i = 0; i <= 10; i++) {
			List<Throwable> failures = runConcurrently( //
					getTaskDataState(file), //
					discardEdits(file), //
					putEdits(file), //
					putTaskData(file), //
					setTaskData(file), //
					putTaskDataState(file), //
					deleteTaskData(file));
			assertNoFailures(failures);
		}
	}

	@Test
	public void manipulateTaskDataByFixedNumberOfThreads() throws Exception {
		File file = newTaskDataZipFile();
		TaskDataStore store = newTaskDataStore();
		List<Throwable> failures = new ArrayList<>();
		ExecutorService executor = Executors.newFixedThreadPool(4);

		List<Thread> threads = new ArrayList<>();
		threads.addAll(Collections.nCopies(3, thread(getTaskDataState(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(discardEdits(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(putEdits(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(putTaskData(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(setTaskData(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(putTaskDataState(file), store, Optional.empty(), failures).get()));
		threads.addAll(Collections.nCopies(3, thread(deleteTaskData(file), store, Optional.empty(), failures).get()));
		Collections.shuffle(threads);

		threads.stream().forEach(thread -> executor.submit(thread));

		executor.shutdown();

		assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));
		assertNoFailures(failures);
	}

	private static TaskDataStore newTaskDataStore() {
		TaskRepositoryManager manager = new TaskRepositoryManager();
		TaskDataExternalizer externalizer = new TaskDataExternalizer(manager) {
			@Override
			public TaskDataState readState(InputStream in) throws IOException, SAXException {
				// read the input stream fully but do not return the result, not relevant for the purpose of the test
				assertEquals(DATA_XML_CONTENT, new String(ByteStreams.toByteArray(in), Charsets.UTF_8));
				in.close();
				// pretend that reading state takes more than the blink of an eye
				sleep();
				return TEST_STATE;
			};

			@Override
			public void writeState(OutputStream out, ITaskDataWorkingCopy state) throws IOException {
				out.write(DATA_XML_CONTENT.getBytes(Charsets.UTF_8));
				// pretend that writing state takes more than the blink of an eye
				sleep();
			}
		};
		return new TaskDataStore(externalizer);
	}

	private static void sleep() {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new RuntimeException(e);
		}
	}

	private File newTaskDataZipFile() throws IOException {
		File file = folder.newFile("test.zip");
		try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(file))) {
			ZipEntry entry = new ZipEntry("data.xml");
			outputStream.putNextEntry(entry);
			byte[] data = DATA_XML_CONTENT.getBytes();
			outputStream.write(data, 0, data.length);
			outputStream.closeEntry();
		}
		return file;
	}

	private static TaskData newTaskData() {
		TaskRepository repository = new TaskRepository("connectorKind", "repositoryUrl");
		TaskAttributeMapper mapper = new TaskAttributeMapper(repository);
		return new TaskData(mapper, "connectorKind", "repositoryUrl", "taskId");
	}

	@FunctionalInterface
	public interface ConsumerWithCoreException<T> {
		public void accept(T t) throws CoreException;
	}

	private ConsumerWithCoreException<TaskDataStore> getTaskDataState(File file) {
		return store -> store.getTaskDataState(file);
	}

	private ConsumerWithCoreException<TaskDataStore> discardEdits(File file) {
		return store -> store.discardEdits(file);
	}

	private ConsumerWithCoreException<TaskDataStore> putEdits(File file) {
		return store -> store.putEdits(file, TASK_DATA);
	}

	private ConsumerWithCoreException<TaskDataStore> putTaskData(File file) {
		return store -> store.putTaskData(file, TASK_DATA, true, true);
	}

	private ConsumerWithCoreException<TaskDataStore> setTaskData(File file) {
		return store -> store.setTaskData(file, TASK_DATA);
	}

	private ConsumerWithCoreException<TaskDataStore> putTaskDataState(File file) {
		return store -> store.putTaskData(file, TEST_STATE);
	}

	private ConsumerWithCoreException<TaskDataStore> deleteTaskData(File file) {
		return store -> store.deleteTaskData(file);
	}

	private static List<Throwable> runSameByMultipleThreads(ConsumerWithCoreException<TaskDataStore> consumer)
			throws Exception {
		TaskDataStore store = newTaskDataStore();
		CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
		List<Throwable> failures = new ArrayList<>(THREAD_COUNT);

		List<Thread> threads = Stream.generate(thread(consumer, store, Optional.of(barrier), failures))
				.limit(THREAD_COUNT)
				.collect(toList());

		startThreadsSimultaneously(barrier, threads);
		return failures;
	}

	@SafeVarargs
	private static List<Throwable> runConcurrently(ConsumerWithCoreException<TaskDataStore>... consumers)
			throws Exception {
		TaskDataStore store = newTaskDataStore();
		CyclicBarrier barrier = new CyclicBarrier(consumers.length + 1);
		List<Throwable> failures = new ArrayList<>(consumers.length);

		List<Thread> threads = Arrays.asList(consumers)
				.stream()
				.map(c -> thread(c, store, Optional.of(barrier), failures).get())
				.collect(toList());

		startThreadsSimultaneously(barrier, threads);
		return failures;
	}

	private static void startThreadsSimultaneously(CyclicBarrier barrier, List<Thread> threads) throws Exception {
		threads.stream().forEach(Thread::start);
		barrier.await();
		for (Thread thread : threads) {
			thread.join();
		}
	}

	private static Supplier<Thread> thread(ConsumerWithCoreException<TaskDataStore> consumer, TaskDataStore store,
			Optional<CyclicBarrier> barrier, List<Throwable> failures) {
		return new Supplier<Thread>() {

			@Override
			public Thread get() {
				return new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							barrier.ifPresent(b -> {
								try {
									b.await();
								} catch (InterruptedException | BrokenBarrierException e) {
									throw new RuntimeException(e);
								}
							});
							consumer.accept(store);
						} catch (Throwable e) {
							failures.add(e);
						}
					}
				});
			}
		};
	}

	private static void assertNoFailures(List<Throwable> failures) {
		assertTrue(format("expected no failures but found %d:\n%s", failures.size(), collectMessages(failures)),
				failures.isEmpty());
	}

	private static String collectMessages(List<Throwable> failures) {
		return failures.stream().map(e -> e.getClass().getSimpleName()).collect(Collectors.joining("\n"));
	}

}

Back to the top