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