Skip to content

Commit 25bae3e

Browse files
committed
Fixing timeout bug :(
1 parent e6d90c4 commit 25bae3e

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/main/java/com/algorithmia/algo/Algorithm.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public final class Algorithm {
2323
private final HttpClient client;
2424
private final Map<String, String> options;
2525
private final AlgorithmOutputType outputType;
26+
private static final long DEFAULT_TIMEOUT = 300L;
2627
final static Gson gson = new Gson();
2728

2829
public Algorithm(HttpClient client, AlgorithmRef algoRef) {
@@ -54,12 +55,21 @@ public Algorithm setOption(String key, String value) {
5455
}
5556

5657
public Algorithm setTimeout(Long timeout, TimeUnit unit) {
57-
Long time = unit.convert(timeout, TimeUnit.SECONDS);
58+
Long time = TimeUnit.SECONDS.convert(timeout, unit);
5859
Map<String, String> optionsClone = new HashMap<String, String>(options);
5960
optionsClone.put(AlgorithmOptions.TIMEOUT.toString(), time.toString());
6061
return new Algorithm(client, algoRef, optionsClone);
6162
}
6263

64+
public Long getTimeout() {
65+
String key = AlgorithmOptions.TIMEOUT.toString();
66+
if (options.containsKey(key)) {
67+
return Long.parseLong(options.get(key));
68+
}
69+
return DEFAULT_TIMEOUT;
70+
71+
}
72+
6373
public Algorithm setStdout(boolean showStdout) {
6474
Map<String, String> optionsClone = new HashMap<String, String>(options);
6575
optionsClone.put(AlgorithmOptions.STDOUT.toString(), Boolean.toString(showStdout));

src/test/java/AlgorithmTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.util.HashMap;
1313
import java.util.Map;
14+
import java.util.concurrent.*;
1415

1516
public class AlgorithmTest {
1617

@@ -87,4 +88,25 @@ public void algorithmSetOptions() throws Exception {
8788

8889
}
8990

91+
@Test
92+
public void algorithmCheckTimeout() throws Exception {
93+
Algorithm algo = Algorithmia.client(key).algo("docs/JavaAddOne");
94+
95+
// Check default timeout - just for fun. This doesn't have to be specified at all time
96+
// but I wanted to make sure this method never throws an exception when the key in the options
97+
// is null.
98+
Assert.assertEquals((long)300, (long)algo.getTimeout());
99+
100+
// Check Minute conversion
101+
algo = algo.setTimeout(20L, TimeUnit.MINUTES);
102+
Assert.assertEquals((long)20 * 60, (long)algo.getTimeout());
103+
104+
// And seconds just in case
105+
algo = algo.setTimeout(30L, TimeUnit.SECONDS);
106+
Assert.assertEquals((long)30, (long)algo.getTimeout());
107+
108+
// And milliseconds
109+
algo = algo.setTimeout(5000L, TimeUnit.MILLISECONDS);
110+
Assert.assertEquals((long)5, (long)algo.getTimeout());
111+
}
90112
}

0 commit comments

Comments
 (0)