Skip to main content
summaryrefslogtreecommitdiffstats
blob: 147c8c5461dc5905fb004438cd69d1a5f80eeb5c (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*******************************************************************************
 * Copyright (c) 2011 EclipseSource 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:
 *     EclipseSource - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.artifact.repository;

import java.io.File;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.artifact.repository.Activator;
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor;
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.repository.IRunnableWithProgress;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class ArtifactLockingTest extends AbstractProvisioningTest {

	private File targetLocation;
	private SimpleArtifactRepository repo1 = null;
	private SimpleArtifactRepository repo2 = null;
	private boolean lockingValue = false;

	public void setUp() throws Exception {
		super.setUp();
		lockingValue = Activator.getInstance().enableArtifactLocking();
		System.setProperty(Activator.ENABLE_ARTIFACT_LOCKING, "true");

		targetLocation = File.createTempFile("bundlepool", ".repo");
		targetLocation.delete();
		targetLocation.mkdirs();
		repo1 = new SimpleArtifactRepository(getAgent(), "TargetRepo", targetLocation.toURI(), null);
		Thread.sleep(1000);
		repo2 = new SimpleArtifactRepository(getAgent(), "TargetRepo", targetLocation.toURI(), null);
	}

	@Override
	protected void tearDown() throws Exception {
		System.setProperty(Activator.ENABLE_ARTIFACT_LOCKING, "" + lockingValue);
		super.tearDown();
	}

	boolean canContinue = false;

	public void testCancelLoad() throws InterruptedException, ProvisionException {
		this.canContinue = false;
		final IProgressMonitor progressMonitor = new NullProgressMonitor();
		new Thread(new Runnable() {
			public void run() {
				status = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						try {
							canContinue = true;
							Thread.sleep(3 * 1000);
							repo1.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), new NullProgressMonitor());
							progressMonitor.setCanceled(true);
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// Do nothing
						}
					}
				}, new NullProgressMonitor());
			}
		}).start();

		while (!canContinue) {
			Thread.sleep(100);
		}

		IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) getAgent().getService(IArtifactRepositoryManager.SERVICE_NAME);
		boolean expected = false;
		try {
			artifactManager.loadRepository(targetLocation.toURI(), progressMonitor);
		} catch (OperationCanceledException e) {
			expected = true;
		}
		assertTrue("Expected an Operation Cancled Exception", expected);

	}

	public void testWaitForLoad() throws InterruptedException, ProvisionException {
		this.canContinue = false;
		new Thread(new Runnable() {
			public void run() {
				status = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						try {
							canContinue = true;
							Thread.sleep(6 * 1000);
							repo1.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), new NullProgressMonitor());

						} catch (InterruptedException e) {
							// Do nothing
						}
					}
				}, new NullProgressMonitor());
			}
		}).start();

		while (!canContinue) {
			Thread.sleep(100);
		}
		IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) getAgent().getService(IArtifactRepositoryManager.SERVICE_NAME);
		SimpleArtifactRepository artifactRepository = (SimpleArtifactRepository) artifactManager.loadRepository(targetLocation.toURI(), new NullProgressMonitor());
		assertEquals(1, artifactRepository.getDescriptors().size());

	}

	// Helper variables for the following test case
	private boolean keepRunning = true;
	IStatus status = null;

	/**
	 * Creates 1 thread that does a long running execute batch. We then try to add descriptors to another
	 * instance of the same repository. This will block.  We then cancel the progress monitor to test
	 * that the block terminates.
	 * @throws InterruptedException
	 */
	public void testCancel() throws InterruptedException {
		final IProgressMonitor progressMonitor = new NullProgressMonitor();
		this.keepRunning = true;

		new Thread(new Runnable() {
			public void run() {
				status = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						long start = System.currentTimeMillis();
						while (keepRunning) {
							long current = System.currentTimeMillis();
							if (current - start > 1000 * 10) {
								fail("Test case never finished. Likely keep running was never set to false.");
								return;
							}
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// Do nothing
							}
						}
					}
				}, new NullProgressMonitor());
			}
		}).start();

		// Give the execute batch thread a chance to start
		Thread.sleep(1000);

		// Create a thread that will stop our progress monitor
		Thread t = new Thread(new Runnable() {

			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// Do nothing
				}
				progressMonitor.setCanceled(true);
			}
		});
		t.start();

		// This should block
		repo2.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), progressMonitor);
		keepRunning = false;
		if (status != null && !status.isOK()) {
			fail(status.getMessage());
		}
		assertEquals(0, repo2.getDescriptors().size()); // The descriptor should not have been added
	}

	public void testCancelRead() throws InterruptedException {
		final IProgressMonitor progressMonitor = new NullProgressMonitor();
		this.keepRunning = true;

		new Thread(new Runnable() {
			public void run() {
				status = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						long start = System.currentTimeMillis();
						while (keepRunning) {
							long current = System.currentTimeMillis();
							if (current - start > 1000 * 10) {
								fail("Test case never finished. Likely keep running was never set to false.");
								return;
							}
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// Do nothing
							}
						}
					}
				}, new NullProgressMonitor());
			}
		}).start();

		// Give the execute batch thread a chance to start
		Thread.sleep(1000);

		// Create a thread that will stop our progress monitor
		Thread t = new Thread(new Runnable() {

			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// Do nothing
				}
				progressMonitor.setCanceled(true);
			}
		});
		t.start();

		// This should block
		repo2.getDescriptors();
		keepRunning = false;
		if (status != null && !status.isOK()) {
			fail(status.getMessage());
		}
	}

	// The following variables are used in the following test case

	IStatus status1 = null;
	IStatus status2 = null;
	boolean lockAcquired = false;

	/**
	 * This tests that two 'executeBatch' operations are not executed in 
	 * parallel, but rather, the second one waits for the first to complete.
	 * @throws InterruptedException
	 */
	public void testMultipleExecuteBatch() throws InterruptedException {
		this.lockAcquired = false;
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				status1 = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						try {
							if (lockAcquired)
								throw new RuntimeException("Lock already acquired");
							lockAcquired = true;
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// Do nothing
							}
						} finally {
							lockAcquired = false;
						}
					}
				}, new NullProgressMonitor());
			}
		});
		t1.start();

		Thread t2 = new Thread(new Runnable() {
			public void run() {
				status2 = repo2.executeBatch(new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						try {
							if (lockAcquired)
								throw new RuntimeException("Lock already acquired");
							lockAcquired = true;
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// Do nothing
							}
						} finally {
							lockAcquired = false;
						}
					}
				}, new NullProgressMonitor());
			}
		});
		t2.start();

		t1.join();
		t2.join();

		if (!status1.isOK() || !status2.isOK())
			fail("Test failed, a lock acquired simultaneously by both execute batch operations");
	}

	/**
	 * This tests that we wait for the lock to come off, we then add a descriptor
	 * @throws InterruptedException
	 */
	public void testWait() throws InterruptedException {

		new Thread(new Runnable() {
			public void run() {
				status = repo1.executeBatch(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws OperationCanceledException {
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							// Do nothing
						}

					}
				}, new NullProgressMonitor());
			}
		}).start();

		// Give the execute batch thread a chance to start
		Thread.sleep(1000);

		// This should block for 5 seconds, and then add the descriptor
		repo2.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), new NullProgressMonitor());
		assertEquals(1, repo2.getDescriptors().size()); // The descriptor should not have been added
	}

	public void testMultipleAddDescriptors() throws InterruptedException {
		Thread.sleep(1000);
		repo1.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"))), new NullProgressMonitor());
		assertEquals(1, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo2.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), new NullProgressMonitor());
		assertEquals(2, repo2.getDescriptors().size());
	}

	public void testContainsDescriptor() throws InterruptedException {
		ArtifactKey k = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		SimpleArtifactDescriptor d = new SimpleArtifactDescriptor(k);
		Thread.sleep(1000);
		repo1.addDescriptor(d, new NullProgressMonitor());
		Thread.sleep(1000);
		assertTrue(repo2.contains(d));
	}

	public void testContainsKey() throws InterruptedException {
		ArtifactKey k = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		SimpleArtifactDescriptor d = new SimpleArtifactDescriptor(k);
		Thread.sleep(1000);
		repo1.addDescriptor(d, new NullProgressMonitor());
		Thread.sleep(1000);
		assertTrue(repo2.contains(k));
	}

	public void testMultipleRemoveDescriptors() throws InterruptedException {
		SimpleArtifactDescriptor d1 = new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0")));
		SimpleArtifactDescriptor d2 = new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0")));

		Thread.sleep(1000);
		repo1.addDescriptor(d1, new NullProgressMonitor());
		repo1.addDescriptor(d2, new NullProgressMonitor());
		assertEquals(2, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo1.removeDescriptor(d1, new NullProgressMonitor());
		assertEquals(1, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo2.removeDescriptor(d2, new NullProgressMonitor());
		assertEquals(0, repo2.getDescriptors().size());
	}

	public void testMultipleRemoveKeys() throws InterruptedException {
		ArtifactKey k1 = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		ArtifactKey k2 = new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"));
		SimpleArtifactDescriptor d1 = new SimpleArtifactDescriptor(k1);
		SimpleArtifactDescriptor d2 = new SimpleArtifactDescriptor(k2);

		Thread.sleep(1000);
		repo1.addDescriptor(d1, new NullProgressMonitor());
		repo1.addDescriptor(d2, new NullProgressMonitor());
		assertEquals(2, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo1.removeDescriptor(k1, new NullProgressMonitor());
		assertEquals(1, repo1.getDescriptors().size());
		Thread.sleep(1000);
		assertEquals(1, repo2.getDescriptors().size());
		repo2.removeDescriptor(k2, new NullProgressMonitor());
		assertEquals(0, repo2.getDescriptors().size());
	}

	public void testRemoveBulkKeys() throws InterruptedException {
		ArtifactKey k1 = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		ArtifactKey k2 = new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"));
		ArtifactKey k3 = new ArtifactKey("org.eclipse.test", "test3", Version.create("1.0.0"));
		SimpleArtifactDescriptor d1 = new SimpleArtifactDescriptor(k1);
		SimpleArtifactDescriptor d2 = new SimpleArtifactDescriptor(k2);
		SimpleArtifactDescriptor d3 = new SimpleArtifactDescriptor(k3);

		Thread.sleep(1000);
		repo1.addDescriptor(d1, new NullProgressMonitor());
		repo1.addDescriptor(d2, new NullProgressMonitor());
		repo1.addDescriptor(d3, new NullProgressMonitor());
		assertEquals(3, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo2.removeDescriptors(new IArtifactKey[] {k1, k2}, new NullProgressMonitor());
		Thread.sleep(1000);
		assertEquals(1, repo2.getDescriptors().size());
		assertEquals(1, repo1.getDescriptors().size());
	}

	public void testRemoveBulkDescriptors() throws InterruptedException {
		ArtifactKey k1 = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		ArtifactKey k2 = new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"));
		ArtifactKey k3 = new ArtifactKey("org.eclipse.test", "test3", Version.create("1.0.0"));
		SimpleArtifactDescriptor d1 = new SimpleArtifactDescriptor(k1);
		SimpleArtifactDescriptor d2 = new SimpleArtifactDescriptor(k2);
		SimpleArtifactDescriptor d3 = new SimpleArtifactDescriptor(k3);

		Thread.sleep(1000);
		repo1.addDescriptor(d1, new NullProgressMonitor());
		repo1.addDescriptor(d2, new NullProgressMonitor());
		repo1.addDescriptor(d3, new NullProgressMonitor());
		assertEquals(3, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo2.removeDescriptors(new IArtifactDescriptor[] {d1, d2}, new NullProgressMonitor());
		Thread.sleep(1000);
		assertEquals(1, repo2.getDescriptors().size());
		assertEquals(1, repo1.getDescriptors().size());
	}

	public void testRemoveAll() throws InterruptedException {
		ArtifactKey k1 = new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"));
		ArtifactKey k2 = new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"));
		SimpleArtifactDescriptor d1 = new SimpleArtifactDescriptor(k1);
		SimpleArtifactDescriptor d2 = new SimpleArtifactDescriptor(k2);

		Thread.sleep(1000);
		repo1.addDescriptor(d1, new NullProgressMonitor());
		repo1.addDescriptor(d2, new NullProgressMonitor());
		assertEquals(2, repo1.getDescriptors().size());
		Thread.sleep(1000);
		repo2.removeAll(new NullProgressMonitor());
		Thread.sleep(1000);
		assertEquals(0, repo2.getDescriptors().size());
		assertEquals(0, repo1.getDescriptors().size());
	}

	public void testReloadAdds() throws InterruptedException {
		// Delay 1 second because some operating systems only give 1 second precision
		Thread.sleep(1000);
		repo1.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test1", Version.create("1.0.0"))), new NullProgressMonitor());
		Thread.sleep(1000);
		repo2.addDescriptor(new SimpleArtifactDescriptor(new ArtifactKey("org.eclipse.test", "test2", Version.create("1.0.0"))), new NullProgressMonitor());

		assertEquals(2, repo2.getDescriptors().size());
		// Does the fist repo get reloaded when reading the descriptors
		assertEquals(2, repo1.getDescriptors().size());
	}

	public void _testSetProperty() throws InterruptedException {
		// Delay 1 second because some operating systems only give 1 second precision
		Thread.sleep(1000);
		repo1.setProperty("foo", "bar", new NullProgressMonitor());
		Thread.sleep(1000);
		assertEquals("bar", repo1.getProperty("foo"));
		assertEquals("bar", repo2.getProperty("foo"));
	}

	public void _testGetProperties() throws InterruptedException {
		// Delay 1 second because some operating systems only give 1 second precision
		Thread.sleep(1000);
		repo1.setProperty("foo", "bar", new NullProgressMonitor());
		Thread.sleep(1000);
		assertEquals("bar", repo1.getProperties().get("foo"));
		assertEquals("bar", repo2.getProperties().get("foo"));
	}

	public void _testSetName() throws InterruptedException {
		// Delay 1 second because some operating systems only give 1 second precision
		Thread.sleep(1000);
		repo1.setName("Foo");
		Thread.sleep(1000);
		assertEquals("Foo", repo1.getName());
		assertEquals("Foo", repo2.getName());
	}

	public void _testSetDescription() throws InterruptedException {
		// Delay 1 second because some operating systems only give 1 second precision
		Thread.sleep(1000);
		repo1.setDescription("Foo Bar");
		Thread.sleep(1000);
		assertEquals("Foo Bar", repo1.getDescription());
		assertEquals("Foo Bar", repo2.getDescription());
	}

}

Back to the top