1 /* 2 * Copyright (C) 2011 The Guava Authors 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.google.common.util.concurrent; 18 19 import com.google.common.annotations.GwtIncompatible; 20 import java.util.Random; 21 import java.util.concurrent.ExecutorService; 22 import java.util.concurrent.Executors; 23 import java.util.concurrent.Future; 24 import java.util.concurrent.TimeUnit; 25 import java.util.concurrent.atomic.AtomicLong; 26 import junit.framework.TestCase; 27 28 /** 29 * Basher test for {@link AtomicLongMap}. 30 * 31 * @author mike nonemacher 32 */ 33 @GwtIncompatible // threads 34 35 public class AtomicLongMapBasherTest extends TestCase { 36 private final Random random = new Random(301); 37 testModify_basher()38 public void testModify_basher() throws InterruptedException { 39 int nTasks = 3000; 40 int nThreads = 100; 41 final int getsPerTask = 1000; 42 final int deltaRange = 10000; 43 final String key = "key"; 44 45 final AtomicLong sum = new AtomicLong(); 46 final AtomicLongMap<String> map = AtomicLongMap.create(); 47 48 ExecutorService threadPool = Executors.newFixedThreadPool(nThreads); 49 for (int i = 0; i < nTasks; i++) { 50 @SuppressWarnings("unused") // go/futurereturn-lsc 51 Future<?> possiblyIgnoredError = 52 threadPool.submit( 53 new Runnable() { 54 @Override 55 public void run() { 56 int threadSum = 0; 57 for (int j = 0; j < getsPerTask; j++) { 58 long delta = random.nextInt(deltaRange); 59 int behavior = random.nextInt(10); 60 switch (behavior) { 61 case 0: 62 map.incrementAndGet(key); 63 threadSum++; 64 break; 65 case 1: 66 map.decrementAndGet(key); 67 threadSum--; 68 break; 69 case 2: 70 map.addAndGet(key, delta); 71 threadSum += delta; 72 break; 73 case 3: 74 map.getAndIncrement(key); 75 threadSum++; 76 break; 77 case 4: 78 map.getAndDecrement(key); 79 threadSum--; 80 break; 81 case 5: 82 map.getAndAdd(key, delta); 83 threadSum += delta; 84 break; 85 case 6: 86 long oldValue = map.put(key, delta); 87 threadSum += delta - oldValue; 88 break; 89 case 7: 90 oldValue = map.get(key); 91 if (map.replace(key, oldValue, delta)) { 92 threadSum += delta - oldValue; 93 } 94 break; 95 case 8: 96 oldValue = map.remove(key); 97 threadSum -= oldValue; 98 break; 99 case 9: 100 oldValue = map.get(key); 101 if (map.remove(key, oldValue)) { 102 threadSum -= oldValue; 103 } 104 break; 105 default: 106 throw new AssertionError(); 107 } 108 } 109 sum.addAndGet(threadSum); 110 } 111 }); 112 } 113 114 threadPool.shutdown(); 115 assertTrue(threadPool.awaitTermination(300, TimeUnit.SECONDS)); 116 117 assertEquals(sum.get(), map.get(key)); 118 } 119 } 120