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.

ObjectDirectoryTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2012, Roberto Tyley <roberto.tyley@gmail.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.internal.storage.file;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.util.Arrays;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.concurrent.Callable;
  49. import java.util.concurrent.ExecutorService;
  50. import java.util.concurrent.Executors;
  51. import java.util.concurrent.Future;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.junit.TestRepository;
  54. import org.eclipse.jgit.lib.ConfigConstants;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.storage.file.FileBasedConfig;
  58. import org.junit.Test;
  59. import org.junit.runner.RunWith;
  60. import org.junit.runners.Parameterized;
  61. import org.junit.runners.Parameterized.Parameter;
  62. import org.junit.runners.Parameterized.Parameters;
  63. @RunWith(Parameterized.class)
  64. public class ObjectDirectoryTest extends RepositoryTestCase {
  65. @Parameter
  66. public Boolean trustFolderStats;
  67. @Parameters(name= "core.trustfolderstat={0}")
  68. public static Iterable<? extends Object> data() {
  69. return Arrays.asList(Boolean.TRUE, Boolean.FALSE);
  70. }
  71. @Test
  72. public void testConcurrentInsertionOfBlobsToTheSameNewFanOutDirectory()
  73. throws Exception {
  74. ExecutorService e = Executors.newCachedThreadPool();
  75. for (int i=0; i < 100; ++i) {
  76. ObjectDirectory dir = createBareRepository().getObjectDatabase();
  77. for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) {
  78. f.get();
  79. }
  80. }
  81. }
  82. @Test
  83. public void testShouldNotSearchPacksAgainTheSecondTime() throws Exception {
  84. FileRepository bareRepository = newTestRepositoryWithOnePackfile();
  85. ObjectDirectory dir = bareRepository.getObjectDatabase();
  86. // Make sure that timestamps are modified and read so that a full
  87. // file snapshot check is performed
  88. Thread.sleep(3000L);
  89. assertTrue(dir.searchPacksAgain(dir.packList.get()));
  90. assertFalse(dir.searchPacksAgain(dir.packList.get()));
  91. }
  92. private FileRepository newTestRepositoryWithOnePackfile() throws Exception {
  93. FileRepository repository = createBareRepository();
  94. TestRepository<FileRepository> testRepository = new TestRepository<FileRepository>(repository);
  95. testRepository.commit();
  96. testRepository.packAndPrune();
  97. FileBasedConfig repoConfig = repository.getConfig();
  98. repoConfig.setBoolean(ConfigConstants.CONFIG_CORE_SECTION,null,
  99. ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT,
  100. trustFolderStats.booleanValue());
  101. repoConfig.save();
  102. return repository;
  103. }
  104. private Collection<Callable<ObjectId>> blobInsertersForTheSameFanOutDir(
  105. final ObjectDirectory dir) {
  106. Callable<ObjectId> callable = new Callable<ObjectId>() {
  107. public ObjectId call() throws Exception {
  108. return dir.newInserter().insert(Constants.OBJ_BLOB, new byte[0]);
  109. }
  110. };
  111. return Collections.nCopies(4, callable);
  112. }
  113. }