Skip to content

Commit

Permalink
Add toString() for SequentialExecutor's worker runnable
Browse files Browse the repository at this point in the history
RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=320760882
  • Loading branch information
clm authored and netdpb committed Jul 13, 2020
1 parent b5210ca commit ea4e950
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ public void run() {
}

public void testToString() {
Executor delegate =
final Runnable[] currentTask = new Runnable[1];
final Executor delegate =
new Executor() {
@Override
public void execute(Runnable task) {}
public void execute(Runnable task) {
currentTask[0] = task;
task.run();
currentTask[0] = null;
}

@Override
public String toString() {
Expand All @@ -365,5 +370,19 @@ public String toString() {
Executor sequential2 = newSequentialExecutor(delegate);
assertThat(sequential1.toString()).contains("theDelegate");
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
final String[] whileRunningToString = new String[1];
sequential1.execute(
new Runnable() {
@Override
public void run() {
whileRunningToString[0] = "" + currentTask[0];
}

@Override
public String toString() {
return "my runnable's toString";
}
});
assertThat(whileRunningToString[0]).contains("my runnable's toString");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public void execute(final Runnable task) {
public void run() {
task.run();
}

@Override
public String toString() {
return task.toString();
}
};
queue.add(submittedTask);
workerRunningState = QUEUING;
Expand Down Expand Up @@ -165,6 +170,8 @@ public void run() {

/** Worker that runs tasks from {@link #queue} until it is empty. */
private final class QueueWorker implements Runnable {
Runnable task;

@Override
public void run() {
try {
Expand Down Expand Up @@ -196,7 +203,6 @@ private void workOnQueue() {
boolean hasSetRunning = false;
try {
while (true) {
Runnable task;
synchronized (queue) {
// Choose whether this thread will run or not after acquiring the lock on the first
// iteration
Expand Down Expand Up @@ -227,6 +233,8 @@ private void workOnQueue() {
task.run();
} catch (RuntimeException e) {
log.log(Level.SEVERE, "Exception while executing runnable " + task, e);
} finally {
task = null;
}
}
} finally {
Expand All @@ -238,6 +246,16 @@ private void workOnQueue() {
}
}
}

@SuppressWarnings("GuardedBy")
@Override
public String toString() {
Runnable currentlyRunning = task;
if (currentlyRunning != null) {
return "SequentialExecutorWorker{running=" + currentlyRunning + "}";
}
return "SequentialExecutorWorker{state=" + workerRunningState + "}";
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ public void run() {
}

public void testToString() {
Executor delegate =
final Runnable[] currentTask = new Runnable[1];
final Executor delegate =
new Executor() {
@Override
public void execute(Runnable task) {}
public void execute(Runnable task) {
currentTask[0] = task;
task.run();
currentTask[0] = null;
}

@Override
public String toString() {
Expand All @@ -365,5 +370,19 @@ public String toString() {
Executor sequential2 = newSequentialExecutor(delegate);
assertThat(sequential1.toString()).contains("theDelegate");
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
final String[] whileRunningToString = new String[1];
sequential1.execute(
new Runnable() {
@Override
public void run() {
whileRunningToString[0] = "" + currentTask[0];
}

@Override
public String toString() {
return "my runnable's toString";
}
});
assertThat(whileRunningToString[0]).contains("my runnable's toString");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public void execute(final Runnable task) {
public void run() {
task.run();
}

@Override
public String toString() {
return task.toString();
}
};
queue.add(submittedTask);
workerRunningState = QUEUING;
Expand Down Expand Up @@ -165,6 +170,8 @@ public void run() {

/** Worker that runs tasks from {@link #queue} until it is empty. */
private final class QueueWorker implements Runnable {
Runnable task;

@Override
public void run() {
try {
Expand Down Expand Up @@ -196,7 +203,6 @@ private void workOnQueue() {
boolean hasSetRunning = false;
try {
while (true) {
Runnable task;
synchronized (queue) {
// Choose whether this thread will run or not after acquiring the lock on the first
// iteration
Expand Down Expand Up @@ -227,6 +233,8 @@ private void workOnQueue() {
task.run();
} catch (RuntimeException e) {
log.log(Level.SEVERE, "Exception while executing runnable " + task, e);
} finally {
task = null;
}
}
} finally {
Expand All @@ -238,6 +246,16 @@ private void workOnQueue() {
}
}
}

@SuppressWarnings("GuardedBy")
@Override
public String toString() {
Runnable currentlyRunning = task;
if (currentlyRunning != null) {
return "SequentialExecutorWorker{running=" + currentlyRunning + "}";
}
return "SequentialExecutorWorker{state=" + workerRunningState + "}";
}
}

@Override
Expand Down

0 comments on commit ea4e950

Please sign in to comment.