Skip to content

Commit

Permalink
Merge pull request #27 from gradle/lptr/improve-test-with-timeout
Browse files Browse the repository at this point in the history
Test startup watcher timeout via synthetic test
  • Loading branch information
lptr authored Nov 19, 2024
2 parents 5cdb99c + 74030f6 commit 3896c08
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: '0.14.0-dev.1911+3bf89f55c'
version: '0.14.0-dev.2254+73f2671c7'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ utilizes [Zig](https://ziglang.org/) to cross-compile the C/C++ code.
### Prerequisites

- [JDK 17](https://adoptopenjdk.net/)
- [Zig nightly](https://ziglang.org/download/) (tested on `0.14.0-dev.1224+16d74809d`)

The project currently requires macOS to link to the SDK. It also depends on JDK to be specifically for Darwin. Both of
these limitations should be removable with some legwork in the future.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sourceSets {
}

zig {
zigVersion = "0.14.0-dev.1911+3bf89f55c"
zigVersion = "0.14.0-dev.2254+73f2671c7"
outputDir = layout.buildDirectory.dir("zig")
targets {
create("x86_64-linux-gnu")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import java.util.concurrent.BlockingQueue
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern

import static java.util.concurrent.TimeUnit.SECONDS
import static org.gradle.fileevents.FileWatchEvent.ChangeType.CREATED
import static org.gradle.fileevents.FileWatchEvent.ChangeType.MODIFIED
import static org.gradle.fileevents.FileWatchEvent.ChangeType.REMOVED
Expand Down Expand Up @@ -749,15 +748,6 @@ class BasicFileEventFunctionsTest extends AbstractFileEventFunctionsTest {
expectLogMessage(ERROR, "Couldn't queue event: TERMINATE")
}

def "can handle watcher start timing out"() {
when:
service.newWatcher(eventQueue).start(0, SECONDS)

then:
def ex = thrown AbstractFileEventFunctions.FileWatcherTimeoutException
ex.message == "Starting the watcher timed out"
}

@IgnoreIf(value = { Platform.current().windows }, reason = "Windows 2019 Server does not handle this")
def "can detect events in directory removed then re-added"() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,51 @@ import org.gradle.fileevents.testfixtures.TestFileEventFunctions
import spock.lang.Specification

import java.util.concurrent.LinkedBlockingDeque
import java.util.concurrent.TimeUnit

import static java.util.concurrent.TimeUnit.MILLISECONDS
import static java.util.concurrent.TimeUnit.SECONDS

class SyntheticFileEventFunctionsTest extends Specification {
def service = new TestFileEventFunctions()
def eventQueue = new LinkedBlockingDeque()
def watcher = service
.newWatcher(eventQueue)
.start()

def "normal termination produces termination event"() {
def service = new TestFileEventFunctions()
def watcher = service
.newWatcher(eventQueue)
.start()

when:
watcher.shutdown()
watcher.awaitTermination(1, TimeUnit.SECONDS)
watcher.awaitTermination(1, SECONDS)

then:
eventQueue*.toString() == ["TERMINATE"]
}

def "failure in run loop produces failure event followed by termination events"() {
def service = new TestFileEventFunctions()
def watcher = service
.newWatcher(eventQueue)
.start()

when:
watcher.injectFailureIntoRunLoop()
watcher.awaitTermination(1, TimeUnit.SECONDS)
watcher.awaitTermination(1, SECONDS)

then:
eventQueue*.toString() == ["FAILURE Error", "TERMINATE"]
}

def "can handle watcher start timing out"() {
def service = new TestFileEventFunctions({
Thread.sleep(200)
})

when:
service.newWatcher(eventQueue).start(100, MILLISECONDS)

then:
def ex = thrown AbstractFileEventFunctions.FileWatcherTimeoutException
ex.message == "Starting the watcher timed out"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ import java.util.concurrent.LinkedBlockingQueue

class TestFileEventFunctions extends AbstractFileEventFunctions<TestFileWatcher> {

private final Closure initRunLoop

TestFileEventFunctions(Closure initRunLoop = {}) {
this.initRunLoop = initRunLoop
}

@Override
WatcherBuilder newWatcher(BlockingQueue<FileWatchEvent> eventQueue) {
new WatcherBuilder(eventQueue)
}

static class TestFileWatcher extends AbstractFileEventFunctions.AbstractFileWatcher {
class TestFileWatcher extends AbstractFileEventFunctions.AbstractFileWatcher {
enum Command {
FAIL, TERMINATE
}
Expand All @@ -41,6 +47,7 @@ class TestFileEventFunctions extends AbstractFileEventFunctions<TestFileWatcher>

@Override
protected void initializeRunLoop() {
initRunLoop.call()
}

@Override
Expand All @@ -66,7 +73,7 @@ class TestFileEventFunctions extends AbstractFileEventFunctions<TestFileWatcher>

@Override
protected boolean awaitTermination(long timeoutInMillis) {
return true;
return true
}

@Override
Expand All @@ -80,7 +87,7 @@ class TestFileEventFunctions extends AbstractFileEventFunctions<TestFileWatcher>
}
}

static class WatcherBuilder extends AbstractFileEventFunctions.AbstractWatcherBuilder<TestFileWatcher> {
class WatcherBuilder extends AbstractFileEventFunctions.AbstractWatcherBuilder<TestFileWatcher> {
WatcherBuilder(BlockingQueue<FileWatchEvent> eventQueue) {
super(eventQueue)
}
Expand Down

0 comments on commit 3896c08

Please sign in to comment.