]> source.dussan.org Git - sonarqube.git/blob
f524b6ac58cfcfd942f5ad90854bfd6439919538
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.db.migrations;
21
22 import com.google.common.base.Throwables;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import org.junit.After;
29 import org.junit.Test;
30 import org.sonar.server.platform.Platform;
31 import org.sonar.server.ruby.RubyBridge;
32 import org.sonar.server.ruby.RubyDatabaseMigration;
33 import org.sonar.server.ruby.RubyRailsRoutes;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 public class PlatformDatabaseMigrationConcurrentAccessTest {
40
41   private ExecutorService pool = Executors.newFixedThreadPool(2);
42   /**
43    * Latch is used to make sure both testing threads try and call {@link PlatformDatabaseMigration#startIt()} at the
44    * same time
45    */
46   private CountDownLatch latch = new CountDownLatch(2);
47
48   /**
49    * Implementation of execute runs Runnable synchronously
50    */
51   private PlatformDatabaseMigrationExecutorService executorService = new PlatformDatabaseMigrationExecutorServiceAdaptor() {
52     @Override
53     public void execute(Runnable command) {
54       command.run();
55     }
56   };
57   /**
58    * thread-safe counter of calls to the trigger method of {@link #rubyDatabaseMigration}
59    */
60   private AtomicInteger triggerCount = new AtomicInteger();
61   /**
62    * Implementation of RubyDatabaseMigration which trigger method increments a thread-safe counter and add a delay of 200ms
63    */
64   private RubyDatabaseMigration rubyDatabaseMigration = new RubyDatabaseMigration() {
65     @Override
66     public void trigger() {
67       triggerCount.incrementAndGet();
68       try {
69         Thread.currentThread().sleep(1000);
70       } catch (InterruptedException e) {
71         Throwables.propagate(e);
72       }
73     }
74   };
75   private RubyBridge rubyBridge = mock(RubyBridge.class);
76   private Platform platform = mock(Platform.class);
77   private RubyRailsRoutes railsRoutes = mock(RubyRailsRoutes.class);
78   private PlatformDatabaseMigration underTest = new PlatformDatabaseMigration(rubyBridge, executorService, platform);
79
80   @After
81   public void tearDown() {
82     pool.shutdownNow();
83   }
84
85   @Test
86   public void two_concurrent_calls_to_startit_call_trigger_only_once() throws Exception {
87     when(rubyBridge.databaseMigration()).thenReturn(rubyDatabaseMigration);
88     when(rubyBridge.railsRoutes()).thenReturn(railsRoutes);
89
90     pool.submit(new CallStartit());
91     pool.submit(new CallStartit());
92
93     pool.awaitTermination(2, TimeUnit.SECONDS);
94
95     assertThat(triggerCount.get()).isEqualTo(1);
96   }
97
98   private class CallStartit implements Runnable {
99     @Override
100     public void run() {
101       latch.countDown();
102       try {
103         latch.await();
104       } catch (InterruptedException e) {
105         // propagate interruption
106         Thread.currentThread().interrupt();
107       }
108       underTest.startIt();
109     }
110   }
111 }