1 /* 2 * Copyright 2022 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.code_intelligence.jazzer.runtime; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 import java.util.concurrent.ExecutorService; 22 import java.util.concurrent.Executors; 23 import java.util.concurrent.TimeUnit; 24 import java.util.function.Function; 25 import org.junit.Test; 26 27 public class TraceCmpHooksTest { 28 private static final ExecutorService ES = Executors.newFixedThreadPool(5); 29 30 @Test cmpHookShouldHandleConcurrentModifications()31 public void cmpHookShouldHandleConcurrentModifications() throws InterruptedException { 32 String arg = "test"; 33 Map<String, Object> map = new HashMap<>(); 34 map.put(arg, arg); 35 36 // Add elements to map asynchronously 37 Function<Integer, Runnable> put = (final Integer num) -> () -> { 38 map.put(String.valueOf(num), num); 39 }; 40 for (int i = 0; i < 1_000_000; i++) { 41 ES.submit(put.apply(i)); 42 } 43 44 // Call hook 45 for (int i = 0; i < 1_000; i++) { 46 TraceCmpHooks.mapGet(null, map, new Object[] {arg}, 1, null); 47 } 48 49 ES.shutdown(); 50 // noinspection ResultOfMethodCallIgnored 51 ES.awaitTermination(5, TimeUnit.SECONDS); 52 } 53 } 54