Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test connection closed #498

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
Expand All @@ -64,30 +65,26 @@
class TeragrepBloomFilterTest {

private final String pattern = "[a-zA-Z]*$";
private LazyConnection lazyConnection;
private FilterTypes filterTypes;
private final BloomFilter emptyFilter = BloomFilter.create(100, 0.01);
private SortedMap<Long, Double> sizeMap;
private final String tableName = "bloomfilter_test";
private final String username = "sa";
private final String password = "";
private final String connectionUrl = "jdbc:h2:mem:test;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE";
private final Connection conn = Assertions
.assertDoesNotThrow(() -> DriverManager.getConnection(connectionUrl, username, password));

@BeforeAll
void setEnv() {
Properties properties = new Properties();
String username = "sa";
properties.put("dpl.pth_10.bloom.db.username", username);
String password = "";
properties.put("dpl.pth_10.bloom.db.password", password);
String connectionUrl = "jdbc:h2:mem:test;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE";
properties.put("dpl.pth_06.bloom.db.url", connectionUrl);
properties
.put(
"dpl.pth_06.bloom.db.fields",
"[" + "{expected: 10000, fpp: 0.01}," + "{expected: 20000, fpp: 0.03},"
+ "{expected: 30000, fpp: 0.05}" + "]"
);
Config config = ConfigFactory.parseProperties(properties);
lazyConnection = new LazyConnection(config);
Connection conn = lazyConnection.get();
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); // h2 clear database
});
Expand Down Expand Up @@ -128,10 +125,10 @@ void setEnv() {

@AfterAll
public void tearDown() {
Connection conn = lazyConnection.get();
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); // h2 clear database
});
Assertions.assertDoesNotThrow(conn::close);
}

// -- Tests --
Expand All @@ -147,7 +144,7 @@ void testSavingToDatabase() {
TeragrepBloomFilter filter = new TeragrepBloomFilter(
partition,
rawFilter,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
Expand All @@ -156,7 +153,7 @@ void testSavingToDatabase() {
Map.Entry<Long, Double> entry = sizeMap.entrySet().iterator().next();
String sql = "SELECT `filter` FROM `" + tableName + "`";
Assertions.assertDoesNotThrow(() -> {
ResultSet rs = lazyConnection.get().prepareStatement(sql).executeQuery();
ResultSet rs = conn.prepareStatement(sql).executeQuery();
int cols = rs.getMetaData().getColumnCount();
BloomFilter resultFilter = emptyFilter;
int loops = 0;
Expand Down Expand Up @@ -187,7 +184,7 @@ void testSavingToDatabaseWithOverwrite() {
TeragrepBloomFilter filter = new TeragrepBloomFilter(
partition,
rawFilter,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
Expand All @@ -196,7 +193,7 @@ void testSavingToDatabaseWithOverwrite() {
String sql = "SELECT `filter` FROM `" + tableName + "`";
Assertions.assertDoesNotThrow(() -> {
BloomFilter resultFilter = emptyFilter;
ResultSet rs = lazyConnection.get().prepareStatement(sql).executeQuery();
ResultSet rs = conn.prepareStatement(sql).executeQuery();
while (rs.next()) {
byte[] bytes = rs.getBytes(1);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
Expand All @@ -220,15 +217,15 @@ void testSavingToDatabaseWithOverwrite() {
TeragrepBloomFilter secondFilter = new TeragrepBloomFilter(
secondPartition,
rawFilter2,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
);
secondFilter.saveFilter(true);
String secondSql = "SELECT `filter` FROM `" + tableName + "`";
Assertions.assertDoesNotThrow(() -> {
ResultSet secondRs = lazyConnection.get().prepareStatement(secondSql).executeQuery();
ResultSet secondRs = conn.prepareStatement(secondSql).executeQuery();
BloomFilter secondResultFilter = emptyFilter;
while (secondRs.next()) {
byte[] bytes = secondRs.getBytes(1);
Expand All @@ -242,7 +239,7 @@ void testSavingToDatabaseWithOverwrite() {
Assertions.assertFalse(secondResultFilter.mightContain("one"));
Assertions.assertTrue(secondResultFilter.mightContain("neo"));
Assertions.assertTrue(secondResultFilter.expectedFpp() <= 0.01D);
secondRs.close();
Assertions.assertDoesNotThrow(secondRs::close);
});
}

Expand All @@ -261,7 +258,7 @@ void testCorrectFilterSizeSelection() {
TeragrepBloomFilter filter = new TeragrepBloomFilter(
partition,
rawFilter,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
Expand All @@ -276,7 +273,7 @@ void testCorrectFilterSizeSelection() {
Double fpp = sizeMap.get(size);
String sql = "SELECT `filter` FROM `" + tableName + "`";
Assertions.assertDoesNotThrow(() -> {
ResultSet rs = lazyConnection.get().prepareStatement(sql).executeQuery();
ResultSet rs = conn.prepareStatement(sql).executeQuery();
int cols = rs.getMetaData().getColumnCount();
BloomFilter resultFilter = emptyFilter;
int loops = 0;
Expand All @@ -292,15 +289,15 @@ void testCorrectFilterSizeSelection() {
Assertions.assertTrue(resultFilter.mightContain("one"));
Assertions.assertFalse(resultFilter.mightContain("neo"));
Assertions.assertTrue(resultFilter.expectedFpp() <= fpp);
rs.close();
Assertions.assertDoesNotThrow(rs::close);
});
}

@Test
public void testPatternSavedToDatabase() {
String sql = "SELECT `pattern` FROM `filtertype` GROUP BY `pattern`";
Assertions.assertDoesNotThrow(() -> {
ResultSet rs = lazyConnection.get().prepareStatement(sql).executeQuery();
ResultSet rs = conn.prepareStatement(sql).executeQuery();
String pattern = "";
while (rs.next()) {
pattern = rs.getString(1);
Expand All @@ -320,15 +317,15 @@ public void testEquals() {
TeragrepBloomFilter filter1 = new TeragrepBloomFilter(
partition,
rawFilter,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
);
TeragrepBloomFilter filter2 = new TeragrepBloomFilter(
partition,
rawFilter,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
Expand All @@ -350,15 +347,15 @@ public void testNotEqualsTokens() {
TeragrepBloomFilter filter1 = new TeragrepBloomFilter(
row1.getString(0),
rawFilter1,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
);
TeragrepBloomFilter filter2 = new TeragrepBloomFilter(
row2.getString(0),
rawFilter2,
lazyConnection.get(),
conn,
filterTypes,
tableName,
pattern
Expand Down