Skip to content

Commit 53723f2

Browse files
committed
Use Spock utilities to test asynchronous code
1 parent f339cb2 commit 53723f2

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

src/test/groovy/net/kautler/command/api/CommandHandlerTest.groovy

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.junit.Rule
3131
import org.powermock.reflect.Whitebox
3232
import spock.lang.Specification
3333
import spock.lang.Subject
34+
import spock.util.concurrent.BlockingVariable
3435
import spock.util.mop.Use
3536

3637
import javax.annotation.PostConstruct
@@ -40,12 +41,10 @@ import javax.enterprise.inject.Instance
4041
import javax.enterprise.util.TypeLiteral
4142
import javax.inject.Inject
4243
import java.util.Map.Entry
43-
import java.util.concurrent.CompletableFuture
4444
import java.util.concurrent.ExecutorService
4545

4646
import static java.lang.Thread.currentThread
4747
import static java.util.concurrent.TimeUnit.DAYS
48-
import static java.util.concurrent.TimeUnit.SECONDS
4948
import static org.apache.logging.log4j.Level.DEBUG
5049
import static org.apache.logging.log4j.Level.ERROR
5150
import static org.apache.logging.log4j.Level.INFO
@@ -966,15 +965,15 @@ class CommandHandlerTest extends Specification {
966965

967966
def 'asynchronous command execution should happen asynchronously'() {
968967
given:
969-
def threadFuture = new CompletableFuture()
968+
def executingThread = new BlockingVariable<Thread>(5)
970969

971970
when:
972971
commandHandler.executeAsync(_) {
973-
threadFuture.complete(currentThread())
972+
executingThread.set(currentThread())
974973
}
975974

976975
then:
977-
threadFuture.get(5, SECONDS) != currentThread()
976+
executingThread.get() != currentThread()
978977
}
979978

980979
@Use([ContextualInstanceCategory, Whitebox])

src/test/groovy/net/kautler/command/handler/CommandHandlerJavacordTest.groovy

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.jboss.weld.junit4.WeldInitiator
3838
import org.junit.Rule
3939
import spock.lang.Specification
4040
import spock.lang.Subject
41+
import spock.util.concurrent.BlockingVariable
4142
import spock.util.mop.Use
4243

4344
import javax.annotation.PostConstruct
@@ -48,14 +49,11 @@ import javax.enterprise.inject.Instance
4849
import javax.enterprise.util.TypeLiteral
4950
import javax.inject.Inject
5051
import java.lang.reflect.Type
51-
import java.util.concurrent.CompletableFuture
52-
import java.util.concurrent.CountDownLatch
5352
import java.util.concurrent.ExecutorService
5453

5554
import static java.lang.Thread.currentThread
5655
import static java.util.Arrays.asList
5756
import static java.util.concurrent.TimeUnit.DAYS
58-
import static java.util.concurrent.TimeUnit.SECONDS
5957
import static org.apache.logging.log4j.Level.ERROR
6058
import static org.apache.logging.log4j.Level.INFO
6159
import static org.apache.logging.log4j.test.appender.ListAppender.getListAppender
@@ -328,19 +326,19 @@ class CommandHandlerJavacordTest extends Specification {
328326
def 'command not allowed event should be fired on restricted command'() {
329327
given:
330328
message.content >> '!test'
331-
def countDownLatch = new CountDownLatch(1)
329+
def commandNotAllowedEventFired = new BlockingVariable<Boolean>(5)
332330

333331
when:
334332
commandHandlerJavacord.ci().handleMessage(messageCreateEvent)
335-
countDownLatch.await(5, SECONDS)
333+
commandNotAllowedEventFired.get()
336334

337335
then:
338336
with(testEventReceiverDelegate) {
339337
1 * handleCommandNotAllowedEvent {
340338
it.message == this.message
341339
it.prefix == '!'
342340
it.usedAlias == this.command.aliases.first()
343-
} >> { countDownLatch.countDown() }
341+
} >> { commandNotAllowedEventFired.set(true) }
344342
0 * _
345343
}
346344
}
@@ -349,19 +347,19 @@ class CommandHandlerJavacordTest extends Specification {
349347
def 'message with correct prefix but wrong trigger should fire command not found event'() {
350348
given:
351349
message.content >> '!nocommand'
352-
def countDownLatch = new CountDownLatch(1)
350+
def commandNotFoundEventReceived = new BlockingVariable<Boolean>(5)
353351

354352
when:
355353
commandHandlerJavacord.ci().handleMessage(messageCreateEvent)
356-
countDownLatch.await(5, SECONDS)
354+
commandNotFoundEventReceived.get()
357355

358356
then:
359357
with(testEventReceiverDelegate) {
360358
1 * handleCommandNotFoundEvent {
361359
it.message == this.message
362360
it.prefix == '!'
363361
it.usedAlias == 'nocommand'
364-
} >> { countDownLatch.countDown() }
362+
} >> { commandNotFoundEventReceived.set(true) }
365363
0 * _
366364
}
367365
}
@@ -384,15 +382,15 @@ class CommandHandlerJavacordTest extends Specification {
384382
given:
385383
def discordApi = new DiscordApiImpl(null, null, null, null, null, false)
386384
message.api >> discordApi
387-
def threadFuture = new CompletableFuture()
385+
def executingThread = new BlockingVariable<Thread>(5)
388386

389387
when:
390388
commandHandlerJavacord.executeAsync(message) {
391-
threadFuture.complete(currentThread())
389+
executingThread.set(currentThread())
392390
}
393391

394392
then:
395-
threadFuture.get(5, SECONDS) != currentThread()
393+
executingThread.get() != currentThread()
396394

397395
cleanup:
398396
discordApi?.disconnect()

src/test/groovy/net/kautler/command/handler/CommandHandlerJdaTest.groovy

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.jboss.weld.junit4.WeldInitiator
3636
import org.junit.Rule
3737
import spock.lang.Specification
3838
import spock.lang.Subject
39+
import spock.util.concurrent.BlockingVariable
3940
import spock.util.mop.Use
4041

4142
import javax.annotation.PostConstruct
@@ -46,10 +47,8 @@ import javax.enterprise.inject.Instance
4647
import javax.enterprise.util.TypeLiteral
4748
import javax.inject.Inject
4849
import java.lang.reflect.Type
49-
import java.util.concurrent.CountDownLatch
5050

5151
import static java.util.Arrays.asList
52-
import static java.util.concurrent.TimeUnit.SECONDS
5352
import static org.apache.logging.log4j.Level.INFO
5453
import static org.apache.logging.log4j.test.appender.ListAppender.getListAppender
5554

@@ -357,19 +356,19 @@ class CommandHandlerJdaTest extends Specification {
357356
def 'command not allowed event should be fired on restricted command'() {
358357
given:
359358
message.contentRaw >> '!test'
360-
def countDownLatch = new CountDownLatch(1)
359+
def commandNotAllowedEventFired = new BlockingVariable<Boolean>(5)
361360

362361
when:
363362
commandHandlerJda.ci().onEvent(messageReceivedEvent)
364-
countDownLatch.await(5, SECONDS)
363+
commandNotAllowedEventFired.get()
365364

366365
then:
367366
with(testEventReceiverDelegate) {
368367
1 * handleCommandNotAllowedEvent {
369368
it.message == this.message
370369
it.prefix == '!'
371370
it.usedAlias == this.command.aliases.first()
372-
} >> { countDownLatch.countDown() }
371+
} >> { commandNotAllowedEventFired.set(true) }
373372
0 * _
374373
}
375374
}
@@ -378,19 +377,19 @@ class CommandHandlerJdaTest extends Specification {
378377
def 'message with correct prefix but wrong trigger should fire command not found event'() {
379378
given:
380379
message.contentRaw >> '!nocommand'
381-
def countDownLatch = new CountDownLatch(1)
380+
def commandNotFoundEventReceived = new BlockingVariable<Boolean>(5)
382381

383382
when:
384383
commandHandlerJda.ci().onEvent(messageReceivedEvent)
385-
countDownLatch.await(5, SECONDS)
384+
commandNotFoundEventReceived.get()
386385

387386
then:
388387
with(testEventReceiverDelegate) {
389388
1 * handleCommandNotFoundEvent {
390389
it.message == this.message
391390
it.prefix == '!'
392391
it.usedAlias == 'nocommand'
393-
} >> { countDownLatch.countDown() }
392+
} >> { commandNotFoundEventReceived.set(true) }
394393
0 * _
395394
}
396395
}

0 commit comments

Comments
 (0)