• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static java.util.concurrent.TimeUnit.SECONDS;
20 
21 import com.google.common.annotations.GwtIncompatible;
22 import java.util.ArrayList;
23 import java.util.Random;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import junit.framework.TestCase;
29 
30 /**
31  * Basher test for {@link AtomicLongMap}.
32  *
33  * @author mike nonemacher
34  */
35 @GwtIncompatible // threads
36 public class AtomicLongMapBasherTest extends TestCase {
37   private final Random random = new Random(301);
38 
testModify_basher()39   public void testModify_basher() throws Exception {
40     int nTasks = 3000;
41     int nThreads = 100;
42     final int getsPerTask = 1000;
43     final int deltaRange = 10000;
44     final String key = "key";
45 
46     final AtomicLongMap<String> map = AtomicLongMap.create();
47 
48     ExecutorService threadPool = Executors.newFixedThreadPool(nThreads);
49     ArrayList<Future<Long>> futures = new ArrayList<>();
50     for (int i = 0; i < nTasks; i++) {
51       futures.add(
52           threadPool.submit(
53               new Callable<Long>() {
54                 @Override
55                 public Long call() {
56                   long 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                   return threadSum;
110                 }
111               }));
112     }
113     threadPool.shutdown();
114     assertTrue(threadPool.awaitTermination(300, SECONDS));
115     long sum = 0;
116     for (Future<Long> f : futures) {
117       sum += f.get();
118     }
119     assertEquals(sum, map.get(key));
120   }
121 }
122