Skip to content

Commit

Permalink
Port typescript tests (#21)
Browse files Browse the repository at this point in the history
* Add all simple tests

Signed-off-by: Sanjula Ganepola <[email protected]>

* Add pool test

Signed-off-by: Sanjula Ganepola <[email protected]>

---------

Signed-off-by: Sanjula Ganepola <[email protected]>
  • Loading branch information
SanjulaGanepola authored Aug 15, 2024
1 parent 84eab31 commit 09d8e6b
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 92 deletions.
15 changes: 7 additions & 8 deletions src/main/java/io/github/mapapire/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PoolOptions {

PoolOptions(DaemonServer creds, int maxSize, int startingSize) {
this.creds = creds;
this.opts = new JDBCOptions();
this.maxSize = maxSize;
this.startingSize = startingSize;
}
Expand Down Expand Up @@ -235,24 +236,22 @@ public CompletableFuture<SqlJob> popJob() {
}
}

// TODO: Fix optional parm
public <T> Query<T> query(String sql) {
return this.query(sql, null);
QueryOptions options = new QueryOptions();
return this.query(sql, options);
}

// TODO: Fix optional parm
public <T> Query<T> query(String sql, QueryOptions opts) {
SqlJob job = this.getJob();
return job.query(sql, opts);
}

// TODO: Fix optional parm
public <T> CompletableFuture<QueryResult<T>> execute(String sql) {
return this.execute(sql, null);
public <T> CompletableFuture<QueryResult<T>> execute(String sql) throws Exception {
QueryOptions options = new QueryOptions();
return this.execute(sql, options);
}

// TODO: Fix optional parm
public <T> CompletableFuture<QueryResult<T>> execute(String sql, QueryOptions opts) {
public <T> CompletableFuture<QueryResult<T>> execute(String sql, QueryOptions opts) throws Exception {
SqlJob job = this.getJob();
return job.execute(sql, opts);
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/io/github/mapapire/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ public void cleanup() {
.collect(Collectors.toList());
}

public <T> CompletableFuture<QueryResult<T>> execute() {
public <T> CompletableFuture<QueryResult<T>> execute() throws Exception {
return this.execute(100);
}

public <T> CompletableFuture<QueryResult<T>> execute(int rowsToFetch) {
public <T> CompletableFuture<QueryResult<T>> execute(int rowsToFetch) throws Exception {
switch (this.state) {
case RUN_MORE_DATA_AVAILABLE:
throw new Error("Statement has already been run");
throw new Exception("Statement has already been run");
case RUN_DONE:
throw new Error("Statement has already been fully run");
throw new Exception("Statement has already been fully run");
}

ObjectNode queryObject = objectMapper.createObjectNode();
Expand Down Expand Up @@ -158,23 +158,23 @@ public <T> CompletableFuture<QueryResult<T>> execute(int rowsToFetch) {
errorList.add("Failed to run query (unknown error)");
}

throw new Error(String.join(", ", errorList));
throw new Exception(String.join(", ", errorList));
}

this.correlationId = queryResult.getId();
return CompletableFuture.completedFuture(queryResult);
}

public CompletableFuture<QueryResult<T>> fetchMore() {
public CompletableFuture<QueryResult<T>> fetchMore() throws Exception {
return this.fetchMore(this.rowsToFetch);
}

public CompletableFuture<QueryResult<T>> fetchMore(int rowsToFetch) {
public CompletableFuture<QueryResult<T>> fetchMore(int rowsToFetch) throws Exception {
switch (this.state) {
case NOT_YET_RUN:
throw new Error("Statement has not yet been run");
throw new Exception("Statement has not yet been run");
case RUN_DONE:
throw new Error("Statement has already been fully run");
throw new Exception("Statement has already been fully run");
}

ObjectNode queryObject = objectMapper.createObjectNode();
Expand Down Expand Up @@ -202,9 +202,9 @@ public CompletableFuture<QueryResult<T>> fetchMore(int rowsToFetch) {

String error = queryResult.getError();
if (error != null) {
throw new Error(error.toString());
throw new Exception(error.toString());
} else {
throw new Error("Failed to run query (unknown error)");
throw new Exception("Failed to run query (unknown error)");
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/main/java/io/github/mapapire/SqlJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public int getRunningCount() {
}

// TODO:
public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) {
public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) throws Exception {
this.status = JobStatus.Connecting;
try {
this.socket = this.getChannel(db2Server).get();
Expand Down Expand Up @@ -267,9 +267,9 @@ public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) {
this.dispose();
this.status = JobStatus.NotStarted;
if (connectResult.getError() != null) {
throw new Error(connectResult.getError());
throw new Exception(connectResult.getError());
} else {
throw new Error("Failed to connect to server.");
throw new Exception("Failed to connect to server.");
}
}

Expand All @@ -288,20 +288,20 @@ public <T> Query<T> query(String sql, QueryOptions opts) {
return new Query(this, sql, opts);
}

public <T> CompletableFuture<QueryResult<T>> execute(String sql) {
public <T> CompletableFuture<QueryResult<T>> execute(String sql) throws Exception {
QueryOptions options = new QueryOptions();
return this.execute(sql, options);
}

public <T> CompletableFuture<QueryResult<T>> execute(String sql, QueryOptions opts) {
public <T> CompletableFuture<QueryResult<T>> execute(String sql, QueryOptions opts) throws Exception {
Query<T> query = query(sql, opts);
CompletableFuture<QueryResult<T>> future = query.execute();
try {
QueryResult<T> result = future.get();
query.close().get();

if (result.getError() != null) {
throw new Error(result.getError());
throw new Exception(result.getError());
}

return CompletableFuture.completedFuture(result);
Expand All @@ -311,7 +311,7 @@ public <T> CompletableFuture<QueryResult<T>> execute(String sql, QueryOptions op
}
}

public CompletableFuture<VersionCheckResult> getVersion() {
public CompletableFuture<VersionCheckResult> getVersion() throws Exception {
ObjectNode verObj = objectMapper.createObjectNode();
verObj.put("id", SqlJob.getNewUniqueId());
verObj.put("type", "getversion");
Expand All @@ -334,20 +334,20 @@ public CompletableFuture<VersionCheckResult> getVersion() {

if (!version.getSuccess()) {
if (version.getError() != null) {
throw new Error(version.getError());
throw new Exception(version.getError());
} else {
throw new Error("Failed to get version from backend");
throw new Exception("Failed to get version from backend");
}
}

return CompletableFuture.completedFuture(version);
}

public CompletableFuture<ExplainResults<?>> explain(String statement) {
public CompletableFuture<ExplainResults<?>> explain(String statement) throws Exception {
return this.explain(statement, ExplainType.Run);
}

public CompletableFuture<ExplainResults<?>> explain(String statement, ExplainType type) {
public CompletableFuture<ExplainResults<?>> explain(String statement, ExplainType type) throws Exception {
ObjectNode explainRequest = objectMapper.createObjectNode();
explainRequest.put("id", SqlJob.getNewUniqueId());
explainRequest.put("type", "dove");
Expand All @@ -372,9 +372,9 @@ public CompletableFuture<ExplainResults<?>> explain(String statement, ExplainTyp

if (!explainResult.getSuccess()) {
if (explainResult.getError() != null) {
throw new Error(explainResult.getError());
throw new Exception(explainResult.getError());
} else {
throw new Error("Failed to explain.");
throw new Exception("Failed to explain.");
}
}

Expand All @@ -385,7 +385,7 @@ public String getTraceFilePath() {
return this.traceFile;
}

public CompletableFuture<GetTraceDataResult> getTraceData() {
public CompletableFuture<GetTraceDataResult> getTraceData() throws Exception {
ObjectNode tracedataReqObj = objectMapper.createObjectNode();
tracedataReqObj.put("id", SqlJob.getNewUniqueId());
tracedataReqObj.put("type", "gettracedata");
Expand All @@ -408,16 +408,16 @@ public CompletableFuture<GetTraceDataResult> getTraceData() {

if (!rpy.getSuccess()) {
if (rpy.getError() != null) {
throw new Error(rpy.getError());
throw new Exception(rpy.getError());
} else {
throw new Error("Failed to get trace data from backend");
throw new Exception("Failed to get trace data from backend");
}
}

return CompletableFuture.completedFuture(rpy);
}

public CompletableFuture<SetConfigResult> setTraceConfig(ServerTraceDest dest, ServerTraceLevel level) {
public CompletableFuture<SetConfigResult> setTraceConfig(ServerTraceDest dest, ServerTraceLevel level) throws Exception {
ObjectNode reqObj = objectMapper.createObjectNode();
reqObj.put("id", SqlJob.getNewUniqueId());
reqObj.put("type", "setconfig");
Expand All @@ -444,9 +444,9 @@ public CompletableFuture<SetConfigResult> setTraceConfig(ServerTraceDest dest, S

if (!rpy.getSuccess()) {
if (rpy.getError() != null) {
throw new Error(rpy.getError());
throw new Exception(rpy.getError());
} else {
throw new Error("Failed to set trace options on backend");
throw new Exception("Failed to set trace options on backend");
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ public boolean underCommitControl() {
// });
// }

public CompletableFuture<QueryResult<JobLogEntry>> endTransaction(TransactionEndType type) {
public CompletableFuture<QueryResult<JobLogEntry>> endTransaction(TransactionEndType type) throws Exception {
String query;

switch (type) {
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/io/github/mapapire/MapepireTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.mapapire;

import io.github.mapapire.types.DaemonServer;

import java.io.InputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.jupiter.api.BeforeAll;

import com.fasterxml.jackson.databind.ObjectMapper;

class MapepireTest {
private static final String CONFIG_FILE = "config.properties";
public static DaemonServer creds;
public static final ObjectMapper objectMapper = new ObjectMapper();

@BeforeAll
public static void setup() throws Exception {
Properties properties = new Properties();
try (InputStream input = MapepireTest.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
if (input == null) {
throw new IOException("Unable to find " + CONFIG_FILE);
}
properties.load(input);
}

List<String> keys = Arrays.asList("IBMI_HOST", "IBMI_USER", "IBMI_PASSWORD", "IBMI_PORT");
Map<String, String> secrets = new HashMap<>();
for (String key : keys) {
String value = properties.getProperty(key);
if (value.equals("")) {
throw new Exception(key + " not set in config.properties");
}
secrets.put(key, value);
}

String host = secrets.get(keys.get(0));
String user = secrets.get(keys.get(1));
String password = secrets.get(keys.get(2));
int port = Integer.parseInt(secrets.get(keys.get(3)));

creds = new DaemonServer(host, port, user, password, true, "");

// TODO: Get certificate
}
}
56 changes: 56 additions & 0 deletions src/test/java/io/github/mapapire/PoolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.mapapire;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.junit.jupiter.api.Test;

import io.github.mapapire.types.QueryResult;

class PoolTest extends MapepireTest {
@Test
@SuppressWarnings("unchecked")
void simplePoolUsingPoolExecute() throws Exception {
PoolOptions options = new PoolOptions(MapepireTest.creds, 5, 3);
Pool pool = new Pool(options);
pool.init().get();

List<CompletableFuture<QueryResult<Object>>> futuresA = new ArrayList<>();
for (int i = 0; i < 3; i++) {
futuresA.add(pool.execute("values (job_name)"));
}
CompletableFuture<Void> allOfA = CompletableFuture.allOf(futuresA.toArray(new CompletableFuture[0]));
allOfA.join();

List<String> jobNamesA = new ArrayList<>();
for (CompletableFuture<QueryResult<Object>> future : futuresA) {
jobNamesA.add(((HashMap<String, String>) future.get().getData().get(0)).get("00001"));
}

assertEquals(3, jobNamesA.size());
assertEquals(3, pool.getActiveJobCount());

List<CompletableFuture<QueryResult<Object>>> futuresB = new ArrayList<>();
for (int i = 0; i < 15; i++) {
futuresB.add(pool.execute("values (job_name)"));
}
CompletableFuture<Void> allOfB = CompletableFuture.allOf(futuresB.toArray(new CompletableFuture[0]));
allOfB.join();

List<String> jobNamesB = new ArrayList<>();
for (CompletableFuture<QueryResult<Object>> future : futuresB) {
jobNamesB.add(((HashMap<String, String>) future.get().getData().get(0)).get("00001"));
}

assertEquals(15, jobNamesB.size());
assertTrue(pool.getActiveJobCount() >= 3);
assertTrue(pool.getActiveJobCount() <= 5);

pool.end();
}
}
Loading

0 comments on commit 09d8e6b

Please sign in to comment.