<commit_before>/*
Copyright 2011 Selenium committers
Copyright 2011 Software Freedom Conservancy

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 org.openqa.grid.internal;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.openqa.grid.common.RegistrationRequest.APP;
import static org.openqa.grid.common.RegistrationRequest.MAX_INSTANCES;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
import org.openqa.grid.web.servlet.handler.RequestHandler;

public class GridShutdownTest {


  // TODO freynaud test failing. Not part of the suite. Check if 
  // it was ok before.
  @Test(timeout = 5000)
  public void shutdown() throws InterruptedException {

    final Map<String, Object> ff = new HashMap<String, Object>();
    ff.put(APP, "FF");
    ff.put(MAX_INSTANCES, 1);

    final Registry registry = Registry.newInstance();

    RemoteProxy p1 =
        RemoteProxyFactory.getNewBasicRemoteProxy(ff, "http://machine1:4444", registry);
    registry.add(p1);
    registry.setThrowOnCapabilityNotPresent(true);

    MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
    newSessionRequest.process();

    final int before = getCurrentThreadCount();
    final CountDownLatch latch = new CountDownLatch(numRequests());
    List<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < numRequests(); i++) {
      final Thread thread = new Thread(new Runnable() { // Thread safety reviewed
            public void run() {
              latch.countDown();
              RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
              newSessionRequest.process();
            }
          }, "TestThread" + i);
      threads.add(thread);
      thread.start();
    }
    Thread.sleep(500);
    latch.await();
    assertEquals(before + 5, getCurrentThreadCount());
    registry.stop();
    for (Thread thread : threads) {
        thread.join();
    }
    assertTrue(getCurrentThreadCount() <= before);
  }

  private int getCurrentThreadCount() {
    return Thread.currentThread().getThreadGroup().activeCount();
  }

  private int numRequests() {
    return 5;
  }

}
<commit_msg>FrancoisReynaud: removing hardcoded number<commit_after>/*
Copyright 2011 Selenium committers
Copyright 2011 Software Freedom Conservancy

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 org.openqa.grid.internal;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.openqa.grid.common.RegistrationRequest.APP;
import static org.openqa.grid.common.RegistrationRequest.MAX_INSTANCES;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
import org.openqa.grid.web.servlet.handler.RequestHandler;

public class GridShutdownTest {

  @Test(timeout = 5000)
  public void shutdown() throws InterruptedException {

    final Map<String, Object> ff = new HashMap<String, Object>();
    ff.put(APP, "FF");
    ff.put(MAX_INSTANCES, 1);

    final Registry registry = Registry.newInstance();

    RemoteProxy p1 =
        RemoteProxyFactory.getNewBasicRemoteProxy(ff, "http://machine1:4444", registry);
    registry.add(p1);
    registry.setThrowOnCapabilityNotPresent(true);

    MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
    newSessionRequest.process();

    final int before = getCurrentThreadCount();
    final CountDownLatch latch = new CountDownLatch(numRequests());
    List<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < numRequests(); i++) {
      final Thread thread = new Thread(new Runnable() { // Thread safety reviewed
            public void run() {
              latch.countDown();
              RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
              newSessionRequest.process();
            }
          }, "TestThread" + i);
      threads.add(thread);
      thread.start();
    }
    Thread.sleep(500);
    latch.await();
    assertEquals(before + numRequests(), getCurrentThreadCount());
    registry.stop();
    for (Thread thread : threads) {
        thread.join();
    }
    assertTrue(getCurrentThreadCount() <= before);
  }

  private int getCurrentThreadCount() {
    return Thread.currentThread().getThreadGroup().activeCount();
  }

  private int numRequests() {
    return 5;
  }

}

{'fraction_non_alphanumeric': 0.09984506799793424, 'fraction_numerical': 0.01015665346875538, 'mean_word_length': 3.8578595317725752, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3139128', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1571, 'n_words': 462}