View Javadoc
1   /*
2    * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lfs.server.fs;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import java.nio.file.Files;
19  import java.nio.file.Path;
20  import java.nio.file.Paths;
21  import java.text.MessageFormat;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.concurrent.CyclicBarrier;
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  import java.util.concurrent.Future;
28  import java.util.concurrent.TimeUnit;
29  
30  import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
31  import org.eclipse.jgit.lfs.lib.LongObjectId;
32  import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
33  import org.junit.Test;
34  
35  public class UploadTest extends LfsServerTest {
36  
37  	@Test
38  	public void testUpload() throws Exception {
39  		String TEXT = "test";
40  		AnyLongObjectId id = putContent(TEXT);
41  		assertTrue("expect object " + id.name() + " to exist",
42  				repository.getSize(id) >= 0);
43  		assertEquals("expected object length " + TEXT.length(), TEXT.length(),
44  				repository.getSize(id));
45  	}
46  
47  	@Test
48  	public void testCorruptUpload() throws Exception {
49  		String TEXT = "test";
50  		AnyLongObjectId id = LongObjectIdTestUtils.hash("wrongHash");
51  		try {
52  			putContent(id, TEXT);
53  			fail("expected RuntimeException(\"Status 400\")");
54  		} catch (RuntimeException e) {
55  			assertEquals("Status: 400. Bad Request", e.getMessage());
56  		}
57  		assertFalse("expect object " + id.name() + " not to exist",
58  				repository.getSize(id) >= 0);
59  	}
60  
61  	@SuppressWarnings("boxing")
62  	@Test
63  	public void testLargeFileUpload() throws Exception {
64  		Path f = Paths.get(getTempDirectory().toString(), "largeRandomFile");
65  		createPseudoRandomContentFile(f, 5 * MiB);
66  		long start = System.nanoTime();
67  		LongObjectId id = putContent(f);
68  		System.out.println(
69  				MessageFormat.format("uploaded 10 MiB random data in {0}ms",
70  						(System.nanoTime() - start) / 1e6));
71  		assertTrue("expect object " + id.name() + " to exist",
72  				repository.getSize(id) >= 0);
73  		assertEquals("expected object length " + Files.size(f), Files.size(f),
74  				repository.getSize(id));
75  	}
76  
77  	@Test
78  	public void testParallelUploads() throws Exception {
79  		int count = 10;
80  		List<Path> paths = new ArrayList<>(count);
81  
82  		for (int i = 0; i < count; i++) {
83  			Path f = Paths.get(getTempDirectory().toString(),
84  					"largeRandomFile_" + i);
85  			createPseudoRandomContentFile(f, 1 * MiB);
86  			paths.add(f);
87  		}
88  
89  		final CyclicBarrier barrier = new CyclicBarrier(count);
90  
91  		ExecutorService e = Executors.newFixedThreadPool(count);
92  		try {
93  			for (Path p : paths) {
94  				Future<Object> result = e.submit(() -> {
95  					barrier.await();
96  					putContent(p);
97  					return null;
98  				});
99  				assertNotNull(result);
100 			}
101 		} finally {
102 			e.shutdown();
103 			e.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
104 		}
105 	}
106 }