<commit_before>/*
 *    Copyright 2015 <NAME> (<EMAIL>)
 *
 *  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 eu.activelogic.instrumentor.test.thread.vm;

import static org.junit.Assert.*;

import org.junit.Test;

import eu.activelogic.instrumentor.thread.vm.ThreadCallInterceptor;
import eu.activelogic.instrumentor.thread.vm.ThreadInterceptor;

/**
 * Some simple tests
 * 
 * @author <NAME>
 */
public class ThreadCallInterceptorIntegrationTest {

	private static final String THREAD_NAME = "TEST-THREAD";

	static class MyInterceptor implements ThreadCallInterceptor {

		public Thread createdThread;
		public Thread startedThread;
		public Runnable createdRunnable;

		public void createThread(Thread t, Runnable r) {
			createdThread = t;
			createdRunnable = r;
		}

		public void callStart(Thread t) {
			startedThread = t;
		}
	};

	@Test
	public void newThreadCreationShouldInvokeTheInterceptor() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = new Runnable() {
			public void run() {

			}
		};

		Thread t = new Thread(r);

		assertTrue(t == m.createdThread);
		assertTrue(r == m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorNoRunnable() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Thread t = new Thread();

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorNullRunnable() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = null;

		Thread t = new Thread(r);

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorSubclass() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Thread t = new Thread() {
			@Override
			public void run() {
				super.run();
			}
		};

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void threadStartShouldInvokeTheInterceptor() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = new Runnable() {
			public void run() {
				try {
					Thread.sleep(10l);
				} catch (InterruptedException e) {
				}
			}
		};

		Thread t = new Thread(r);
		t.start();

		assertTrue(t == m.createdThread);
		assertTrue(r == m.createdRunnable);
		assertTrue(t == m.startedThread);

	}

	@Test(expected = RuntimeException.class)
	public void threadStartExceptionShouldPropagete() {
		ThreadInterceptor
				.registerThreadInterceptor(new ThreadCallInterceptor() {

					@Override
					public void createThread(Thread t, Runnable r) {

					}

					@Override
					public void callStart(Thread t) {
						if (t.getName().equals(THREAD_NAME)) {
							throw new RuntimeException();
						}
					}
				});

		Thread t = new Thread(THREAD_NAME);
		t.start();
	}

	@Test(expected = RuntimeException.class)
	public void threadCreateExceptionShouldPropagete() {
		ThreadInterceptor
				.registerThreadInterceptor(new ThreadCallInterceptor() {

					@Override
					public void createThread(Thread t, Runnable r) {
						if (t.getName().equals(THREAD_NAME)) {
							throw new RuntimeException();
						}
					}

					@Override
					public void callStart(Thread t) {
					}
				});

		new Thread(THREAD_NAME);
	}

}<commit_msg>Test for concurrent modification of the interceptor registry<commit_after>/*
 *    Copyright 2015 <NAME> (<EMAIL>)
 *
 *  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 eu.activelogic.instrumentor.test.thread.vm;

import static org.junit.Assert.*;

import org.junit.Test;

import eu.activelogic.instrumentor.thread.vm.ThreadCallInterceptor;
import eu.activelogic.instrumentor.thread.vm.ThreadInterceptor;

/**
 * Some simple tests
 * 
 * @author <NAME>
 */
public class ThreadCallInterceptorIntegrationTest {

	private static final String THREAD_NAME = "TEST-THREAD";

	static class MyInterceptor implements ThreadCallInterceptor {

		public Thread createdThread;
		public Thread startedThread;
		public Runnable createdRunnable;

		public void createThread(Thread t, Runnable r) {
			createdThread = t;
			createdRunnable = r;
		}

		public void callStart(Thread t) {
			startedThread = t;
		}
	};

	@Test
	public void newThreadCreationShouldInvokeTheInterceptor() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = new Runnable() {
			public void run() {

			}
		};

		Thread t = new Thread(r);

		assertTrue(t == m.createdThread);
		assertTrue(r == m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorNoRunnable() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Thread t = new Thread();

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorNullRunnable() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = null;

		Thread t = new Thread(r);

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void newThreadCreationShouldInvokeTheInterceptorSubclass() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Thread t = new Thread() {
			@Override
			public void run() {
				super.run();
			}
		};

		assertTrue(t == m.createdThread);
		assertNull(m.createdRunnable);
		assertNull(m.startedThread);

	}

	@Test
	public void threadStartShouldInvokeTheInterceptor() {
		MyInterceptor m = new MyInterceptor();
		ThreadInterceptor.registerThreadInterceptor(m);

		Runnable r = new Runnable() {
			public void run() {
				try {
					Thread.sleep(10l);
				} catch (InterruptedException e) {
				}
			}
		};

		Thread t = new Thread(r);
		t.start();

		assertTrue(t == m.createdThread);
		assertTrue(r == m.createdRunnable);
		assertTrue(t == m.startedThread);

	}

	@Test(expected = RuntimeException.class)
	public void threadStartExceptionShouldPropagete() {
		ThreadInterceptor
				.registerThreadInterceptor(new ThreadCallInterceptor() {

					@Override
					public void createThread(Thread t, Runnable r) {

					}

					@Override
					public void callStart(Thread t) {
						if (t.getName().equals(THREAD_NAME)) {
							throw new RuntimeException();
						}
					}
				});

		Thread t = new Thread(THREAD_NAME);
		t.start();
	}

	@Test(expected = RuntimeException.class)
	public void threadCreateExceptionShouldPropagete() {
		ThreadInterceptor
				.registerThreadInterceptor(new ThreadCallInterceptor() {

					@Override
					public void createThread(Thread t, Runnable r) {
						if (t.getName().equals(THREAD_NAME)) {
							throw new RuntimeException();
						}
					}

					@Override
					public void callStart(Thread t) {
					}
				});

		new Thread(THREAD_NAME);
	}

	@Test
	public void threadCallInterceptorRegistrationShouldNotCauseConcurrentModificationException() {

		// Register an interceptor that sleeps for 500ms on THREAD_NAME
		ThreadInterceptor
				.registerThreadInterceptor(new ThreadCallInterceptor() {

					@Override
					public void createThread(Thread t, Runnable r) {
						if (t.getName().equals(THREAD_NAME)) {
							try {
								Thread.sleep(550l);
							} catch (InterruptedException e) {
							}
						}
					}

					@Override
					public void callStart(Thread t) {
					}
				});

		// An anonymous thread is created that adds another interceptor, while
		// the interceptor above is in the event processing loop.
		new Thread(() -> {
			try {
				Thread.sleep(100l);
			} catch (InterruptedException e) {
			}
			ThreadInterceptor.registerThreadInterceptor(new MyInterceptor());
		}).start();
		// Cause the first interceptor to run (hopefully within ~50ms)
		new Thread(THREAD_NAME);

	}

}
{'fraction_non_alphanumeric': 0.09966417506229011, 'fraction_numerical': 0.0033582493770989057, 'mean_word_length': 3.763673890608875, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '4337973', 'n_tokens_mistral': 3279, 'n_tokens_neox': 2706, 'n_words': 704}