You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.nio.file.Files;
  17. import java.nio.file.Path;
  18. import java.nio.file.Paths;
  19. import java.text.MessageFormat;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.concurrent.CyclicBarrier;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.Executors;
  25. import java.util.concurrent.Future;
  26. import java.util.concurrent.TimeUnit;
  27. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  28. import org.eclipse.jgit.lfs.lib.LongObjectId;
  29. import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
  30. import org.junit.Test;
  31. public class UploadTest extends LfsServerTest {
  32. @Test
  33. public void testUpload() throws Exception {
  34. String TEXT = "test";
  35. AnyLongObjectId id = putContent(TEXT);
  36. assertTrue("expect object " + id.name() + " to exist",
  37. repository.getSize(id) >= 0);
  38. assertEquals("expected object length " + TEXT.length(), TEXT.length(),
  39. repository.getSize(id));
  40. }
  41. @Test
  42. public void testCorruptUpload() throws Exception {
  43. String TEXT = "test";
  44. AnyLongObjectId id = LongObjectIdTestUtils.hash("wrongHash");
  45. try {
  46. putContent(id, TEXT);
  47. fail("expected RuntimeException(\"Status 400\")");
  48. } catch (RuntimeException e) {
  49. assertEquals("Status: 400. Bad Request", e.getMessage());
  50. }
  51. assertFalse("expect object " + id.name() + " not to exist",
  52. repository.getSize(id) >= 0);
  53. }
  54. @SuppressWarnings("boxing")
  55. @Test
  56. public void testLargeFileUpload() throws Exception {
  57. Path f = Paths.get(getTempDirectory().toString(), "largeRandomFile");
  58. createPseudoRandomContentFile(f, 5 * MiB);
  59. long start = System.nanoTime();
  60. LongObjectId id = putContent(f);
  61. System.out.println(
  62. MessageFormat.format("uploaded 10 MiB random data in {0}ms",
  63. (System.nanoTime() - start) / 1e6));
  64. assertTrue("expect object " + id.name() + " to exist",
  65. repository.getSize(id) >= 0);
  66. assertEquals("expected object length " + Files.size(f), Files.size(f),
  67. repository.getSize(id));
  68. }
  69. @Test
  70. public void testParallelUploads() throws Exception {
  71. int count = 10;
  72. List<Path> paths = new ArrayList<>(count);
  73. for (int i = 0; i < count; i++) {
  74. Path f = Paths.get(getTempDirectory().toString(),
  75. "largeRandomFile_" + i);
  76. createPseudoRandomContentFile(f, 1 * MiB);
  77. paths.add(f);
  78. }
  79. final CyclicBarrier barrier = new CyclicBarrier(count);
  80. ExecutorService e = Executors.newFixedThreadPool(count);
  81. try {
  82. for (Path p : paths) {
  83. Future<Object> result = e.submit(() -> {
  84. barrier.await();
  85. putContent(p);
  86. return null;
  87. });
  88. assertNotNull(result);
  89. }
  90. } finally {
  91. e.shutdown();
  92. e.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  93. }
  94. }
  95. }