3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.db.migrations;
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;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class PlatformDatabaseMigrationConcurrentAccessTest {
41 private ExecutorService pool = Executors.newFixedThreadPool(2);
43 * Latch is used to make sure both testing threads try and call {@link PlatformDatabaseMigration#startIt()} at the
46 private CountDownLatch latch = new CountDownLatch(2);
49 * Implementation of execute runs Runnable synchronously
51 private PlatformDatabaseMigrationExecutorService executorService = new PlatformDatabaseMigrationExecutorServiceAdaptor() {
53 public void execute(Runnable command) {
58 * thread-safe counter of calls to the trigger method of {@link #rubyDatabaseMigration}
60 private AtomicInteger triggerCount = new AtomicInteger();
62 * Implementation of RubyDatabaseMigration which trigger method increments a thread-safe counter and add a delay of 200ms
64 private RubyDatabaseMigration rubyDatabaseMigration = new RubyDatabaseMigration() {
66 public void trigger() {
67 triggerCount.incrementAndGet();
69 Thread.currentThread().sleep(1000);
70 } catch (InterruptedException e) {
71 Throwables.propagate(e);
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);
81 public void tearDown() {
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);
90 pool.submit(new CallStartit());
91 pool.submit(new CallStartit());
93 pool.awaitTermination(2, TimeUnit.SECONDS);
95 assertThat(triggerCount.get()).isEqualTo(1);
98 private class CallStartit implements Runnable {
104 } catch (InterruptedException e) {
105 // propagate interruption
106 Thread.currentThread().interrupt();