view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgFileUtilTest.java @ 1002:138c9ab0b1f0 Jaipur-2018.1.x

TW-55764: remove option
author pavel.sher
date Fri, 29 Jun 2018 11:48:54 +0200
parents 7bf4d943d5bb
children 10dc26b32c35
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial;


import jetbrains.buildServer.util.TestFor;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;

@Test
public class HgFileUtilTest {

  public void createTempFile_should_return_unique_dir() throws IOException {
    File tmpDir1 = HgFileUtil.createTempDir();
    File tmpDir2 = HgFileUtil.createTempDir();
    assertFalse(tmpDir1.getCanonicalPath().equals(tmpDir2.getCanonicalPath()));
  }


  @TestFor(issues = "TW-30589")
  public void createTempFile_should_always_return_unique_dir() throws Exception {
    final CountDownLatch allThreadsInitialized = new CountDownLatch(1);
    final Set<File> tmpFiles = new HashSet<File>();
    Runnable createTmpFile = new Runnable() {
      public void run() {
        try {
          allThreadsInitialized.await();
          tmpFiles.add(HgFileUtil.createTempDir());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    };

    List<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < 50; i++) {
      Thread t = new Thread(createTmpFile);
      t.start();
      threads.add(t);
    }

    allThreadsInitialized.countDown();

    for (Thread t : threads) {
      t.join();
    }

    assertEquals("Race condition in createTempDir", threads.size(), tmpFiles.size());
  }
}