Browse Source

Fix memory leaks in server medium tests

tags/5.0-RC1
Simon Brandhof 9 years ago
parent
commit
c2fad0c080

+ 0
- 67
server/sonar-server/src/test/java/org/sonar/server/activity/SearchMediumTest.java View File

@@ -1,67 +0,0 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.activity;

import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.db.DbClient;
import org.sonar.server.search.IndexClient;
import org.sonar.server.tester.ServerTester;

import java.lang.management.ManagementFactory;

public abstract class SearchMediumTest {


private static Logger LOGGER = LoggerFactory.getLogger(SearchMediumTest.class);

@ClassRule
public static ServerTester tester = new ServerTester();

protected DbClient db;
protected IndexClient index;
protected DbSession dbSession;

@Before
public void before() {
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
index = tester.get(IndexClient.class);
dbSession = tester.get(DbClient.class).openSession(false);

ManagementFactory.getMemoryMXBean();
LOGGER.info("* Environment ({})", ManagementFactory.getOperatingSystemMXBean().getName());
LOGGER.info("* heap:\t{}", ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
LOGGER.info("* #cpu:\t{}", ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors());
LOGGER.info("* load:\t{}", ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage());
}

@After
public void after() {
if (dbSession != null) {
dbSession.close();
}
}

}

+ 39
- 6
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java View File

@@ -21,22 +21,29 @@ package org.sonar.server.qualityprofile;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.activity.SearchMediumTest;
import org.sonar.server.db.DbClient;
import org.sonar.server.platform.Platform;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.rule.RuleTesting;
import org.sonar.server.rule.db.RuleDao;
import org.sonar.server.search.FacetValue;
import org.sonar.server.search.IndexClient;
import org.sonar.server.tester.ServerTester;

import java.util.Collection;
import java.util.Date;
@@ -46,7 +53,32 @@ import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;

public class ActiveRuleBackendMediumTest extends SearchMediumTest {
public class ActiveRuleBackendMediumTest {

@ClassRule
public static ServerTester tester = new ServerTester();

DbClient db;
RuleDao dao;
IndexClient index;
DbSession dbSession;

@Before
public void before() {
dao = tester.get(RuleDao.class);
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
index = tester.get(IndexClient.class);
dbSession = tester.get(DbClient.class).openSession(false);

}

@After
public void after() {
if (dbSession != null) {
dbSession.close();
}
}

@Test
public void synchronize_index() throws Exception {
@@ -175,7 +207,8 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest {
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
// Removed rule can still be activated for instance when removing the checkstyle plugin, active rules related on checkstyle are not removed
// Removed rule can still be activated for instance when removing the checkstyle plugin, active rules related on checkstyle are not
// removed
// because if the plugin is re-install, quality profiles using these rule are not changed.
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, removedRule).setSeverity(Severity.MAJOR));
dbSession.commit();
@@ -310,7 +343,7 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest {
ActiveRuleDto.createFor(profileDto2, ruleDto2)
.setInheritance(ActiveRule.Inheritance.INHERITED.name())
.setSeverity(Severity.BLOCKER)
);
);
dbSession.commit();
dbSession.clearCache();

@@ -335,7 +368,7 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest {
db.ruleDao().insert(dbSession, ruleDto1, ruleDto2);

List<String> profileKeys = newArrayList();
for (int i = 0; i<30; i++) {
for (int i = 0; i < 30; i++) {
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor("xoo", "profile-" + i), "profile-" + i);
profileKeys.add(profileDto.getKey());
db.qualityProfileDao().insert(dbSession, profileDto);
@@ -345,7 +378,7 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest {
.setSeverity(Severity.BLOCKER),
ActiveRuleDto.createFor(profileDto, ruleDto2)
.setSeverity(Severity.MAJOR)
);
);
}
dbSession.commit();
dbSession.clearCache();

+ 67
- 28
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java View File

@@ -25,16 +25,20 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import org.apache.commons.lang.time.DateUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
import org.sonar.server.activity.SearchMediumTest;
import org.sonar.server.db.DbClient;
import org.sonar.server.debt.DebtTesting;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileTesting;
@@ -44,19 +48,53 @@ import org.sonar.server.rule.db.RuleDao;
import org.sonar.server.search.FacetValue;
import org.sonar.server.search.QueryContext;
import org.sonar.server.search.Result;
import org.sonar.server.tester.ServerTester;

import javax.annotation.Nullable;

import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;

public class RuleIndexMediumTest extends SearchMediumTest {
public class RuleIndexMediumTest {

RuleDao dao = tester.get(RuleDao.class);
RuleIndex index = tester.get(RuleIndex.class);
@ClassRule
public static ServerTester tester = new ServerTester();

protected DbClient db;

RuleDao dao;
RuleIndex index;
// IndexClient index;
DbSession dbSession;

@Before
public void before() {
dao = tester.get(RuleDao.class);
index = tester.get(RuleIndex.class);
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
// index = tester.get(IndexClient.class);
dbSession = tester.get(DbClient.class).openSession(false);

}

@After
public void after() {
if (dbSession != null) {
dbSession.close();
}
}

@Test
public void getByKey() throws InterruptedException {
@@ -435,7 +473,8 @@ public class RuleIndexMediumTest extends SearchMediumTest {
.setSubCharacteristicId(char21.getId()))
.setDefaultSubCharacteristicId(char11.getId());

// Rule with both default sub characteristic and overridden sub characteristic and with same values -> should be find by char11 and char1
// Rule with both default sub characteristic and overridden sub characteristic and with same values -> should be find by char11 and
// char1
dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S004"))
.setSubCharacteristicId(char11.getId()))
.setDefaultSubCharacteristicId(char11.getId());
@@ -588,7 +627,7 @@ public class RuleIndexMediumTest extends SearchMediumTest {

// 4. get all active rules on profile
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey()),
.setQProfileKey(qualityProfileDto2.getKey()),
new QueryContext());
assertThat(result.getHits()).hasSize(1);
assertThat(result.getHits().get(0).name()).isEqualTo(rule1.getName());
@@ -625,7 +664,7 @@ public class RuleIndexMediumTest extends SearchMediumTest {
ActiveRuleDto.createFor(qualityProfileDto2, rule3)
.setSeverity("BLOCKER")
.setInheritance(ActiveRule.Inheritance.INHERITED.name())
);
);

dbSession.commit();

@@ -647,52 +686,52 @@ public class RuleIndexMediumTest extends SearchMediumTest {

// 3. get Inherited Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(0);

// 4. get Inherited Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(2);

// 5. get Overridden Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(0);

// 6. get Overridden Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(1);

// 7. get Inherited AND Overridden Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(0);

// 8. get Inherited AND Overridden Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
);
assertThat(result.getHits()).hasSize(3);
}


+ 4
- 2
server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java View File

@@ -58,7 +58,7 @@ public class ServerTester extends ExternalResource {
private final Integer clusterPort;

private SearchServer searchServer;
private final Platform platform;
private Platform platform;
private final File homeDir;
private final List components = Lists.newArrayList(WsTester.class);
private final Properties initialProps = new Properties();
@@ -154,6 +154,7 @@ public class ServerTester extends ExternalResource {
} catch (Exception e) {
e.printStackTrace();
}
platform = null;
try {
if (searchServer != null) {
searchServer.stop();
@@ -161,6 +162,7 @@ public class ServerTester extends ExternalResource {
} catch (Exception e) {
e.printStackTrace();
}
searchServer = null;
FileUtils.deleteQuietly(homeDir);
}

@@ -234,7 +236,7 @@ public class ServerTester extends ExternalResource {
}

private void checkNotStarted() {
if (platform.isStarted()) {
if (platform != null && platform.isStarted()) {
throw new IllegalStateException("Already started");
}
}

+ 1
- 0
sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java View File

@@ -142,6 +142,7 @@ public class TestDatabase extends ExternalResource {
protected void after() {
db.stop();
db = null;
myBatis = null;
}

public Database database() {

Loading…
Cancel
Save