Skip to content

Commit

Permalink
Merge pull request #4 from code77se/unit-tests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
code77se committed Mar 10, 2016
2 parents f96696b + 93743cb commit 1190cbf
Show file tree
Hide file tree
Showing 17 changed files with 1,487 additions and 23 deletions.
12 changes: 7 additions & 5 deletions jq/src/main/java/se/code77/jq/JQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ public V call() throws Exception {
throw (Exception) cause;
} else {
// Promises can only rejected with
// Exceptions, not
// any Throwable. Simply wrap it.
throw new Exception("Future threw a Throwable",
cause);
// Exceptions, not any Throwable.
// Simply throw the ExecutionException.
throw e;
}
}

Expand Down Expand Up @@ -588,10 +587,13 @@ public final void checkStates(final List<Promise<V>> promises) {
mFulfilledCount = 0;
mRejectedCount = 0;

for (Promise<V> p : promises) {
mStates.add(p.inspect());
}

for (int i = 0; i < promises.size(); i++) {
final int pos = i;
final Promise<V> p = promises.get(pos);
mStates.add(p.inspect());

p.then(new Promise.OnFulfilledCallback<V, Void>() {
@Override
Expand Down
18 changes: 18 additions & 0 deletions jq/src/main/java/se/code77/jq/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ public static final class StateSnapshot<V> {
this.reason = reason;
}

@Override
public int hashCode() {
return state.hashCode();
}

@Override
public boolean equals(Object o) {
if (o instanceof StateSnapshot) {
StateSnapshot that = (StateSnapshot)o;

return state.equals(that.state) &&
(value == null ? that.value == null : value.equals(that.value)) &&
(reason == null ? that.reason == null : reason.equals(that.reason));
}

return false;
}

@Override
public String toString() {
switch (state) {
Expand Down
22 changes: 19 additions & 3 deletions jq/src/main/java/se/code77/jq/PromiseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ synchronized void _resolve(V value) {
ensurePending();

mState = new StateSnapshot<>(State.FULFILLED, value, null);
debug("fulfilled with value '" + value + "'");
info("fulfilled with value '" + value + "'");
notify();
handleCompletion();
}
Expand All @@ -255,7 +255,7 @@ synchronized void _reject(Exception reason) {
ensurePending();

mState = new StateSnapshot<>(State.REJECTED, null, reason);
debug("rejected with reason '" + reason + "'");
info("rejected with reason '" + reason + "'");
notify();
handleCompletion();
}
Expand Down Expand Up @@ -316,23 +316,30 @@ public void run() {
try {
Future<?> nextValue;

debug("[" + mState + "]: Handling link " + link);

if (isRejected()) {
if (link.onRejectedCallback != null) {
nextValue = link.onRejectedCallback.onRejected(mState.reason);
verbose("Link.onRejected returned " + nextValue);
} else {
verbose("Link has no onRejected, forward to next promise");
link.nextPromise._reject(mState.reason);
return;
}
} else {
if (link.onFulfilledCallback != null) {
nextValue = link.onFulfilledCallback.onFulfilled(mState.value);
verbose("Link.onFulfilled returned " + nextValue);
} else {
verbose("Link has no onFulfilled, forward to next promise");
link.nextPromise._resolve(mState.value);
return;
}
}

if (nextValue != null) {
verbose("Link returned future, next promise will inherit");
JQ.wrap(nextValue).then(new OnFulfilledCallback() {
@Override
public Future onFulfilled(Object value) throws Exception {
Expand All @@ -347,14 +354,15 @@ public Future onRejected(Exception reason) throws Exception {
}
}).done();
} else {
verbose("Link returned null, next promise will resolve directly");
link.nextPromise._resolve(null);
}
} catch (UnhandledRejectionException e) {
throw e;
} catch (Exception reason) {
StringWriter sw = new StringWriter();
reason.printStackTrace(new PrintWriter(sw));
debug("Promise rejected from callback: " + sw.toString());
info("Promise rejected from callback: " + sw.toString());

link.nextPromise._reject(reason);
}
Expand All @@ -374,10 +382,18 @@ private String getLogPrefix() {
return "Promise@" + Integer.toHexString(hashCode()) + ": ";
}

private void verbose(String s) {
getLogger().verbose(getLogPrefix() + s);
}

private void debug(String s) {
getLogger().debug(getLogPrefix() + s);
}

private void info(String s) {
getLogger().info(getLogPrefix() + s);
}

private void warn(String s) {
getLogger().warn(getLogPrefix() + s);
}
Expand Down
7 changes: 7 additions & 0 deletions jq/src/main/java/se/code77/jq/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public interface Dispatcher {
* A logger creates log prints.
*/
public interface Logger {
/**
* Print a debug log
* @param s log text
*/
void verbose(String s);

/**
* Print a debug log
* @param s log text
Expand All @@ -64,6 +70,7 @@ public interface Logger {
}

public enum LogLevel {
VERBOSE,
DEBUG,
INFO,
WARN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ private boolean hasLevel(LogLevel level) {
return mLogLevel.ordinal() <= level.ordinal();
}

@Override
public void verbose(String s) {
if (hasLevel(LogLevel.VERBOSE)) {
Log.v(LOG_TAG, s);
}
}

@Override
public void debug(String s) {
if (hasLevel(LogLevel.DEBUG)) {
Expand Down
15 changes: 0 additions & 15 deletions jq/src/test/java/se/code77/jq/ExampleUnitTest.java

This file was deleted.

Loading

0 comments on commit 1190cbf

Please sign in to comment.