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