-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExpectNoConnectionLeak annotation
- Loading branch information
1 parent
cf17771
commit c0bd884
Showing
14 changed files
with
1,054 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/src/main/java/org/quickperf/repository/BooleanMeasureRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2019-2020 the original author or authors. | ||
*/ | ||
|
||
package org.quickperf.repository; | ||
|
||
import org.quickperf.WorkingFolder; | ||
import org.quickperf.measure.BooleanMeasure; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
public class BooleanMeasureRepository { | ||
|
||
public static final BooleanMeasureRepository INSTANCE = new BooleanMeasureRepository(); | ||
|
||
private BooleanMeasureRepository() { } | ||
|
||
private final ObjectInputStreamBuilder objectInputStreamBuilder = ObjectInputStreamBuilder.INSTANCE; | ||
|
||
private final ObjectOutputStreamBuilder objectOutputStreamBuilder = ObjectOutputStreamBuilder.INSTANCE; | ||
|
||
public void save(BooleanMeasure booleanMeasure, WorkingFolder workingFolder, String fileName) { | ||
String workingFolderPath = workingFolder.getPath(); | ||
try(ObjectOutputStream objectOutputStream = objectOutputStreamBuilder | ||
.build(workingFolderPath, fileName) | ||
) { | ||
objectOutputStream.writeBoolean(booleanMeasure.getValue()); | ||
objectOutputStream.flush(); | ||
} catch (IOException ioe) { | ||
throw new IllegalStateException("Unable to save " + fileName, ioe); | ||
} | ||
} | ||
|
||
public BooleanMeasure find(WorkingFolder workingFolder, String fileName) { | ||
boolean booleanValue = retrieveBooleanValueFromFile(workingFolder, fileName); | ||
return BooleanMeasure.of(booleanValue); | ||
} | ||
|
||
private boolean retrieveBooleanValueFromFile(WorkingFolder workingFolder, String fileName) { | ||
String workingFolderPath = workingFolder.getPath(); | ||
try(ObjectInputStream objectInputStream = objectInputStreamBuilder | ||
.buildObjectInputStream(workingFolderPath, fileName) | ||
) { | ||
return objectInputStream.readBoolean(); | ||
} catch (IOException ioe) { | ||
throw new IllegalStateException("Unable to deserialize.", ioe); | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
sql/sql-annotations/src/main/java/org/quickperf/sql/annotation/ExpectNoConnectionLeak.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2019-2020 the original author or authors. | ||
*/ | ||
|
||
package org.quickperf.sql.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* <p><code>ExpectNoConnectionLeak</code> annotation allows detecting database connection leaks. | ||
* | ||
* <p>This annotation verifies that each connection got from the data source is well closed during the test method execution. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface ExpectNoConnectionLeak { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...-annotations/src/main/java/org/quickperf/sql/config/library/QuickPerfProxyDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2019-2020 the original author or authors. | ||
*/ | ||
|
||
package org.quickperf.sql.config.library; | ||
|
||
import net.ttddyy.dsproxy.support.ProxyDataSource; | ||
import org.quickperf.sql.connection.QuickPerfDatabaseConnection; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class QuickPerfProxyDataSource extends ProxyDataSource { | ||
|
||
@Override | ||
public Connection getConnection() throws SQLException { | ||
Connection connection = super.getConnection(); | ||
QuickPerfDatabaseConnection connectionProxy = QuickPerfDatabaseConnection.buildFrom(connection); | ||
connectionProxy.getFromTheDatasource(); | ||
return connectionProxy; | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String username, String password) throws SQLException { | ||
Connection connection = super.getConnection(username, password); | ||
QuickPerfDatabaseConnection connectionProxy = QuickPerfDatabaseConnection.buildFrom(connection); | ||
connectionProxy.getFromTheDatasource(); | ||
return connectionProxy; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
sql/sql-annotations/src/main/java/org/quickperf/sql/connection/ConnectionLeakListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2019-2020 the original author or authors. | ||
*/ | ||
|
||
package org.quickperf.sql.connection; | ||
|
||
import org.quickperf.TestExecutionContext; | ||
import org.quickperf.WorkingFolder; | ||
import org.quickperf.measure.BooleanMeasure; | ||
import org.quickperf.repository.BooleanMeasureRepository; | ||
|
||
import java.sql.Connection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ConnectionLeakListener extends ConnectionsListener<BooleanMeasure> { | ||
|
||
private final List<Connection> connections = new ArrayList<>(); | ||
|
||
private BooleanMeasure connectionLeak; | ||
|
||
private static final String CONNECTION_LEAK_FILE_NAME = "connection-leak.ser"; | ||
|
||
@Override | ||
public void getFromTheDataSource(Connection connection) { | ||
connections.add(connection); | ||
} | ||
|
||
@Override | ||
public void close(Connection connection) { | ||
connections.remove(connection); | ||
} | ||
|
||
@Override | ||
public void startRecording(TestExecutionContext testExecutionContext) { | ||
connections.clear(); | ||
ConnectionListenerRegistry.INSTANCE.register(this); | ||
} | ||
|
||
@Override | ||
public void stopRecording(TestExecutionContext testExecutionContext) { | ||
ConnectionListenerRegistry.unregister(this); | ||
connectionLeak = BooleanMeasure.of(!connections.isEmpty()); | ||
connections.clear(); | ||
if (testExecutionContext.testExecutionUsesTwoJVMs()) { | ||
WorkingFolder workingFolder = testExecutionContext.getWorkingFolder(); | ||
BooleanMeasureRepository.INSTANCE.save(connectionLeak, workingFolder, CONNECTION_LEAK_FILE_NAME); | ||
} | ||
} | ||
|
||
@Override | ||
public BooleanMeasure findRecord(TestExecutionContext testExecutionContext) { | ||
if (testExecutionContext.testExecutionUsesTwoJVMs()) { | ||
WorkingFolder workingFolder = testExecutionContext.getWorkingFolder(); | ||
return BooleanMeasureRepository.INSTANCE.find(workingFolder, CONNECTION_LEAK_FILE_NAME); | ||
} | ||
return connectionLeak; | ||
} | ||
|
||
@Override | ||
public void cleanResources() { } | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
sql/sql-annotations/src/main/java/org/quickperf/sql/connection/ConnectionLeakVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2019-2020 the original author or authors. | ||
*/ | ||
|
||
package org.quickperf.sql.connection; | ||
|
||
import org.quickperf.issue.PerfIssue; | ||
import org.quickperf.issue.VerifiablePerformanceIssue; | ||
import org.quickperf.measure.BooleanMeasure; | ||
import org.quickperf.sql.annotation.ExpectNoConnectionLeak; | ||
|
||
public class ConnectionLeakVerifier implements VerifiablePerformanceIssue<ExpectNoConnectionLeak, BooleanMeasure> { | ||
|
||
public static final ConnectionLeakVerifier INSTANCE = new ConnectionLeakVerifier(); | ||
|
||
private ConnectionLeakVerifier() { } | ||
|
||
@Override | ||
public PerfIssue verifyPerfIssue(ExpectNoConnectionLeak annotation, BooleanMeasure connectionLeak) { | ||
if(connectionLeak.isTrue()) { | ||
return new PerfIssue("Database connection leak"); | ||
} | ||
return PerfIssue.NONE; | ||
} | ||
|
||
} |
Oops, something went wrong.