Skip to content

Commit

Permalink
Fixed findbugs issues and refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
renas committed Jun 12, 2017
1 parent b41a9d9 commit d624f3d
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 144 deletions.
42 changes: 31 additions & 11 deletions Flank/src/main/java/com/walmart/otto/Flank.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class Flank {
private ToolManager toolManager;
private Configurator configurator;

public void start(String[] args) throws FileNotFoundException, RuntimeException, IOException {
public void start(String[] args)
throws RuntimeException, IOException, InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();

if (!FileUtils.doFileExist(args[0])) {
Expand Down Expand Up @@ -76,6 +78,10 @@ public static void main(String[] args) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
System.exit(-1);
}
Expand Down Expand Up @@ -114,7 +120,8 @@ private void printEstimates() {
}
}

private void downloadTestTimeFile(GsutilTool gsutilTool, int shardDuration) throws IOException {
private void downloadTestTimeFile(GsutilTool gsutilTool, int shardDuration)
throws IOException, InterruptedException {
if (shardDuration == -1) {
return;
}
Expand All @@ -131,16 +138,19 @@ private void downloadTestTimeFile(GsutilTool gsutilTool, int shardDuration) thro
}
}

private void uploadTestTimeFile(GsutilTool gsutilTool, int shardDuration) throws IOException {
private void uploadTestTimeFile(GsutilTool gsutilTool, int shardDuration)
throws IOException, InterruptedException {
if (shardDuration == -1) {
return;
}
gsutilTool.uploadTestTimeFile();
}

private String getProjectName(ToolManager toolManager) throws IOException {
System.setOut(emptyStream);
private String getProjectName(ToolManager toolManager) throws IOException, InterruptedException {
System.setOut(getEmptyStream());

String text = toolManager.get(GcloudTool.class).getProjectName() + "-flank";

System.setOut(originalStream);
return text;
}
Expand Down Expand Up @@ -172,7 +182,7 @@ private void printShards(Configurator configurator, int numberOfShards) {
}

private List<String> getTestCaseNames(String[] args) {
System.setOut(emptyStream);
System.setOut(getEmptyStream());
List<String> filteredTests;

if (args.length < 3) {
Expand All @@ -186,9 +196,19 @@ private List<String> getTestCaseNames(String[] args) {
}

static PrintStream originalStream = System.out;
static PrintStream emptyStream =
new PrintStream(
new OutputStream() {
public void write(int b) {}
});

public PrintStream getEmptyStream() {
PrintStream emptyStream = null;
try {
emptyStream =
new PrintStream(
new OutputStream() {
public void write(int b) {}
},
false,
"UTF-8");
} catch (UnsupportedEncodingException ignored) {
}
return emptyStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ private void setProperty(String property, String value) throws IllegalArgumentEx
case "fetch-xml-files":
configurator.setFetchXMLFiles(Boolean.parseBoolean(value));
break;

default:
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static HashMap<String, BigDecimal> getTotalPrice(List<Integer> times) {

public static Double getTotalBillableTime(List<Integer> times) {

HashMap<String, Double> mapOfPrices = getPricePerMinForDevice();
Double time = 0.00;

for (Integer executionTime : times) {
Expand Down
16 changes: 9 additions & 7 deletions Flank/src/main/java/com/walmart/otto/shards/ShardCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.walmart.otto.Constants;
import com.walmart.otto.configurator.Configurator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -47,7 +45,7 @@ private String addToShard(Stack<String> stack, int numTestsInShards) {
StringBuilder stringBuilder = new StringBuilder();

if (stack.isEmpty()) {
return new String();
return "";
}

for (int i = 0; i < numTestsInShards; i++) {
Expand Down Expand Up @@ -109,7 +107,9 @@ public StringBuilder createConfigurableShard(Map shardMap, List<String> testCase
while (it.hasNext()) {
Map.Entry testCase = (Map.Entry) it.next();

int testCaseTime = Integer.valueOf((String) testCase.getValue());
int testCaseTime =
Integer.parseInt(
(String) testCase.getValue()); //Integer.valueOf((String) testCase.getValue());

if (shardLengthSec - testCaseTime > 0) {
if (testCases.contains(testCase.getKey())) {
Expand All @@ -134,7 +134,9 @@ public Map<String, String> createShardMap() {
Map<String, String> shardMap = new HashMap<>();
try {
try (BufferedReader br =
new BufferedReader(new FileReader(new File(Constants.TEST_TIME_FILE)))) {
new BufferedReader(
new InputStreamReader(
new FileInputStream(Constants.TEST_TIME_FILE), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
String[] testCase = line.split(Pattern.quote(" "));
Expand Down
49 changes: 23 additions & 26 deletions Flank/src/main/java/com/walmart/otto/shards/ShardExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import com.walmart.otto.utils.XMLUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

public class ShardExecutor {
ExecutorService executorService;
Expand All @@ -27,7 +26,8 @@ public ShardExecutor(Configurator configurator, ToolManager toolManager) {
shardCreator = new ShardCreator(configurator);
}

public void execute(List<String> testCases, String bucket) {
public void execute(List<String> testCases, String bucket)
throws InterruptedException, ExecutionException, IOException {

executeShards(testCases, bucket);

Expand All @@ -36,7 +36,9 @@ public void execute(List<String> testCases, String bucket) {
}
}

public void executeShards(List<String> testCases, String bucket) {
public void executeShards(List<String> testCases, String bucket)
throws InterruptedException, ExecutionException {
List<Future> futures = new ArrayList<>();
List<String> shards = shardCreator.getConfigurableShards(testCases);

if (shards.isEmpty()) {
Expand All @@ -47,32 +49,31 @@ public void executeShards(List<String> testCases, String bucket) {

if (configurator.getShardIndex() != -1) {
printTests(shards.get(configurator.getShardIndex()), configurator.getShardIndex());
executeShard(shards.get(configurator.getShardIndex()), bucket);
futures.add(executeShard(shards.get(configurator.getShardIndex()), bucket));
} else {
System.out.println(
shards.size() + " shards will be executed on: " + configurator.getDeviceIds() + "\n");

for (int i = 0; i < shards.size(); i++) {
printTests(shards.get(i), i);
executeShard(shards.get(i), bucket);
futures.add(executeShard(shards.get(i), bucket));
}
}

executorService.shutdown();

try {
executorService.awaitTermination(240, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
for (Future future : futures) {
future.get();
}
}

private void executeShard(String testCase, String bucket) {
Runnable testCaseRunner = getRunnable(testCase, bucket);
private Future executeShard(String testCase, String bucket) throws RuntimeException {
Callable testCaseCallable = getCallable(testCase, bucket);

executorService.submit(testCaseRunner);
return executorService.submit(testCaseCallable);
}

private void fetchResults(GsutilTool gsutilTool) {
private void fetchResults(GsutilTool gsutilTool) throws IOException, InterruptedException {
Map<String, String> resultsMap = gsutilTool.fetchResults();

//Add device name to test case names
Expand All @@ -90,20 +91,16 @@ private void printTests(String testsString, int index) {
System.out.println("Executing shard " + index + ": " + tests + "\n");
}

private Runnable getRunnable(String testCase, String bucket) {
Runnable gCloudRunner =
new Runnable() {
private Callable<Void> getCallable(String testCase, String bucket) throws RuntimeException {
Callable<Void> gCloudCallable =
new Callable<Void>() {
@Override
public void run() {
public Void call() throws Exception {
final GcloudTool gcloudTool = toolManager.get(GcloudTool.class);
try {
gcloudTool.runGcloud(testCase, bucket);
} catch (RuntimeException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
gcloudTool.runGcloud(testCase, bucket);
return null;
}
};
return gCloudRunner;
return gCloudCallable;
}
}
10 changes: 7 additions & 3 deletions Flank/src/main/java/com/walmart/otto/tools/GcloudTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.walmart.otto.reporter.TimeReporter;
import com.walmart.otto.utils.FileUtils;
import com.walmart.otto.utils.FilterUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -19,7 +20,8 @@ public GcloudTool(ToolManager.Config config) {
super(ToolManager.GCLOUD_TOOL, config);
}

public void runGcloud(String testCase, String bucket) throws RuntimeException {
public void runGcloud(String testCase, String bucket)
throws RuntimeException, IOException, InterruptedException {
// don't quote arguments or ProcessBuilder will error in strange ways.
String[] runGcloud =
new String[] {
Expand Down Expand Up @@ -64,7 +66,8 @@ public void runGcloud(String testCase, String bucket) throws RuntimeException {
executeGcloud(cmdArray, testCase);
}

public void executeGcloud(String[] commands, String test) throws RuntimeException {
public void executeGcloud(String[] commands, String test)
throws RuntimeException, IOException, InterruptedException {
List<String> inputStreamList = new ArrayList<>();
List<String> errorStreamList = new ArrayList<>();

Expand Down Expand Up @@ -97,11 +100,12 @@ public void executeGcloud(String[] commands, String test) throws RuntimeExceptio
printTests = true;
}

public String getProjectName() {
public String getProjectName() throws IOException, InterruptedException {
String[] projectDetails =
new String[] {getConfigurator().getGcloud(), "config", "get-value", "project"};
List<String> inputStreamList = new ArrayList<>();
String projectName = "";

executeCommand(projectDetails, inputStreamList, new ArrayList<>());

for (String projectProperties : inputStreamList) {
Expand Down

0 comments on commit d624f3d

Please sign in to comment.