• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.serialization;
6 
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.withSettings;
9 
10 import java.nio.charset.CharacterCodingException;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Random;
14 import java.util.concurrent.*;
15 
16 import org.junit.Test;
17 import org.mockitousage.IMethods;
18 import org.mockitoutil.SimpleSerializationUtil;
19 
20 public class ParallelSerializationTest {
21 
22     @Test
single_mock_being_serialized_in_different_classloaders_by_multiple_threads()23     public void single_mock_being_serialized_in_different_classloaders_by_multiple_threads()
24             throws ExecutionException, InterruptedException {
25         // given
26         int iterations = 2;
27         int threadingFactor = 200;
28         final ExecutorService executorService = Executors.newFixedThreadPool(threadingFactor);
29         final IMethods iMethods_that_store_invocations =
30                 mock(IMethods.class, withSettings().serializable());
31 
32         // when
33         for (int i = 0; i <= iterations; i++) {
34             List<Future<?>> futures = new ArrayList<Future<?>>(threadingFactor);
35             final CyclicBarrier barrier_that_will_wait_until_threads_are_ready =
36                     new CyclicBarrier(threadingFactor);
37 
38             // prepare all threads by submitting a callable
39             //  - that will serialize the mock a 'threadingFactor' times
40             //  - that will use the mock a 'threadingFactor' times
41             for (int j = 0; j < threadingFactor; j++) {
42                 // submit a callable that will serialize the mock 'iMethods'
43                 futures.add(
44                         executorService.submit(
45                                 new Callable<Object>() {
46                                     public Object call() throws Exception {
47                                         barrier_that_will_wait_until_threads_are_ready.await();
48 
49                                         randomCallOn(iMethods_that_store_invocations);
50 
51                                         return SimpleSerializationUtil.serializeMock(
52                                                         iMethods_that_store_invocations)
53                                                 .toByteArray();
54                                     }
55                                 }));
56 
57                 // submit a callable that will only use the mock 'iMethods'
58                 executorService.submit(
59                         new Callable<Object>() {
60                             public Object call() throws Exception {
61                                 barrier_that_will_wait_until_threads_are_ready.await();
62                                 return iMethods_that_store_invocations.longObjectReturningMethod();
63                             }
64                         });
65             }
66 
67             // ensure we are getting the futures
68             for (Future<?> future : futures) {
69                 future.get();
70             }
71         }
72     }
73 
randomCallOn(IMethods iMethods)74     private void randomCallOn(IMethods iMethods) throws CharacterCodingException {
75         int random = new Random().nextInt(10);
76         switch (random) {
77             case 0:
78                 iMethods.arrayReturningMethod();
79                 break;
80             case 1:
81                 iMethods.longObjectReturningMethod();
82                 break;
83             case 2:
84                 iMethods.linkedListReturningMethod();
85                 break;
86             case 3:
87                 iMethods.iMethodsReturningMethod();
88                 break;
89             case 4:
90                 iMethods.canThrowException();
91                 break;
92             case 5:
93                 iMethods.differentMethod();
94                 break;
95             case 6:
96                 iMethods.voidMethod();
97                 break;
98             case 7:
99                 iMethods.varargsString(1, "");
100                 break;
101             case 8:
102                 iMethods.forMap(null);
103                 break;
104             case 9:
105                 iMethods.throwsNothing(false);
106                 break;
107             default:
108         }
109     }
110 }
111