Skip to content

Commit 9d0fb02

Browse files
committed
Timeout in RuntimeExecutor is now set from Application configuration
Instead of just hardcoding a default wait time to get the process return code, we now use the Application#timeout configuration. Related to jcgay/gradle-notifier#7
1 parent 95a2a45 commit 9d0fb02

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

send-notification/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@
118118
<version>1.58</version>
119119
<scope>test</scope>
120120
</dependency>
121+
<dependency>
122+
<groupId>org.objenesis</groupId>
123+
<artifactId>objenesis</artifactId>
124+
<version>2.6</version>
125+
<scope>test</scope>
126+
</dependency>
121127
</dependencies>
122128

123129
<build>

send-notification/src/main/java/fr/jcgay/notification/NotifierProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public DiscoverableNotifier byName(ChosenNotifiers notifier, Properties properti
8585
return new GrowlNotifier(application, GrowlConfiguration.create(properties), ERROR);
8686
}
8787
if (NOTIFICATION_CENTER.equals(notifier)) {
88-
return new TerminalNotifier(application, TerminalNotifierConfiguration.create(properties), new RuntimeExecutor());
88+
return new TerminalNotifier(application, TerminalNotifierConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
8989
}
9090
if (NOTIFY_SEND.equals(notifier)) {
91-
return new NotifySendNotifier(application, NotifySendConfiguration.create(properties), new RuntimeExecutor());
91+
return new NotifySendNotifier(application, NotifySendConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
9292
}
9393
if (PUSHBULLET.equals(notifier)) {
9494
return new PushbulletNotifier(application, PushbulletConfiguration.create(properties));
@@ -100,19 +100,19 @@ public DiscoverableNotifier byName(ChosenNotifiers notifier, Properties properti
100100
return new SystemTrayNotifier(application);
101101
}
102102
if (NOTIFU.equals(notifier)) {
103-
return new NotifuNotifier(application, NotifuConfiguration.create(properties), new RuntimeExecutor());
103+
return new NotifuNotifier(application, NotifuConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
104104
}
105105
if (KDIALOG.equals(notifier)) {
106-
return new KdialogNotifier(application, KdialogConfiguration.create(properties), new RuntimeExecutor());
106+
return new KdialogNotifier(application, KdialogConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
107107
}
108108
if (ANYBAR.equals(notifier)) {
109109
return AnyBarNotifier.create(application, AnyBarConfiguration.create(properties));
110110
}
111111
if (SIMPLE_NOTIFICATION_CENTER.equals(notifier)) {
112-
return new SimpleNotificationCenterNotifier(TerminalNotifierConfiguration.create(properties), new RuntimeExecutor());
112+
return new SimpleNotificationCenterNotifier(TerminalNotifierConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
113113
}
114114
if (TOASTER.equals(notifier)) {
115-
return new ToasterNotifier(ToasterConfiguration.create(properties), new RuntimeExecutor());
115+
return new ToasterNotifier(ToasterConfiguration.create(properties), new RuntimeExecutor(application.timeout()));
116116
}
117117
if (NOTIFY.equals(notifier)) {
118118
return new NotifyNotifier(application, NotifyConfiguration.create(properties));

send-notification/src/main/java/fr/jcgay/notification/notifier/executor/RuntimeExecutor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
public class RuntimeExecutor implements Executor {
2424

2525
private static final Logger LOGGER = getLogger(RuntimeExecutor.class);
26+
private static final int DEFAULT_WAIT_PROCESS_TIMEOUT = 200;
2627

2728
private final ExecutorService executor = Executors.newSingleThreadExecutor();
29+
private final long timeout;
30+
31+
public RuntimeExecutor(long timeout) {
32+
this.timeout = timeout == -1 ? DEFAULT_WAIT_PROCESS_TIMEOUT : timeout;
33+
}
2834

2935
@Override
3036
public void exec(final String[] command) {
3137

3238
if (LOGGER.isDebugEnabled()) {
33-
LOGGER.debug("Will execute command line: {}", logCommand(command));
39+
LOGGER.debug("Will execute command line: {} (waiting at most: {} milliseconds)", logCommand(command), timeout);
3440
}
3541

3642
Future<Integer> task = executor.submit(new Callable<Integer>() {
@@ -62,7 +68,7 @@ public Integer call() {
6268
});
6369

6470
try {
65-
task.get(200, MILLISECONDS);
71+
task.get(timeout, MILLISECONDS);
6672
} catch (InterruptedException e) {
6773
Thread.currentThread().interrupt();
6874
throw Throwables.propagate(e);

send-notification/src/test/groovy/fr/jcgay/notification/NotifierProviderSpec.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class NotifierProviderSpec extends Specification {
3838
then:
3939
assertThat(result).containsExactly(
4040
new GrowlNotifier(application, GrowlConfiguration.byDefault(), DEBUG),
41-
new TerminalNotifier(application, TerminalNotifierConfiguration.byDefault(), new RuntimeExecutor()),
41+
new TerminalNotifier(application, TerminalNotifierConfiguration.byDefault(), new RuntimeExecutor(application.timeout())),
4242
new SystemTrayNotifier(application)
4343
)
4444
}
@@ -54,7 +54,7 @@ class NotifierProviderSpec extends Specification {
5454
assertThat(result).containsExactly(
5555
new SnarlNotifier(application, SnarlConfiguration.byDefault()),
5656
new GrowlNotifier(application, GrowlConfiguration.byDefault(), DEBUG),
57-
new ToasterNotifier(ToasterConfiguration.byDefault(), new RuntimeExecutor()),
57+
new ToasterNotifier(ToasterConfiguration.byDefault(), new RuntimeExecutor(application.timeout())),
5858
new BurntToastNotifier(application, BurntToastNotifierConfiguration.byDefault()),
5959
new SystemTrayNotifier(application)
6060
)
@@ -69,8 +69,8 @@ class NotifierProviderSpec extends Specification {
6969

7070
then:
7171
assertThat(result).containsExactly(
72-
new KdialogNotifier(application, KdialogConfiguration.byDefault(), new RuntimeExecutor()),
73-
new NotifySendNotifier(application, NotifySendConfiguration.byDefault(), new RuntimeExecutor()),
72+
new KdialogNotifier(application, KdialogConfiguration.byDefault(), new RuntimeExecutor(application.timeout())),
73+
new NotifySendNotifier(application, NotifySendConfiguration.byDefault(), new RuntimeExecutor(application.timeout())),
7474
new SystemTrayNotifier(application)
7575
)
7676
}

0 commit comments

Comments
 (0)