-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFileWatchServiceSpec.scala
More file actions
139 lines (112 loc) · 3.94 KB
/
Copy pathFileWatchServiceSpec.scala
File metadata and controls
139 lines (112 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.dev.filewatch
import better.files._
import org.specs2.mutable.Specification
import java.util.function.Supplier
import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
class JavaFileWatchServiceSpec extends FileWatchServiceSpec {
// the mac impl consistently fails because it takes more than 5s so skip this one on mac
args(skipAll = OS.getCurrent == OS.Mac)
override def watchService: FileWatchService =
FileWatchService.jdk7(FileWatchServiceSpecLoggerProxy, false)
}
class JavaFileWatchServiceHashCheckDisabledSpec extends FileWatchServiceSpec {
// the mac impl consistently fails because it takes more than 5s so skip this one on mac
args(skipAll = OS.getCurrent == OS.Mac)
override def watchService: FileWatchService =
FileWatchService.jdk7(FileWatchServiceSpecLoggerProxy, true)
}
class MacFileWatchServiceSpec extends FileWatchServiceSpec {
// this only works on mac
args(skipAll = OS.getCurrent != OS.Mac)
override def watchService: FileWatchService =
FileWatchService.mac(FileWatchServiceSpecLoggerProxy, false)
}
class MacFileWatchServiceHashCheckDisabledSpec extends FileWatchServiceSpec {
// this only works on mac
args(skipAll = OS.getCurrent != OS.Mac)
override def watchService: FileWatchService =
FileWatchService.mac(FileWatchServiceSpecLoggerProxy, true)
}
class PollingFileWatchServiceSpec extends FileWatchServiceSpec {
override def watchService: FileWatchService = FileWatchService.polling(200)
}
object FileWatchServiceSpecLoggerProxy extends LoggerProxy {
override def verbose(message: Supplier[String]): Unit = ()
override def debug(message: Supplier[String]): Unit = ()
override def info(message: Supplier[String]): Unit = ()
override def warn(message: Supplier[String]): Unit = ()
override def error(message: Supplier[String]): Unit = ()
override def trace(t: Supplier[Throwable]): Unit = ()
override def success(message: Supplier[String]): Unit = ()
}
abstract class FileWatchServiceSpec extends Specification {
sequential
def watchService: FileWatchService
private def withTempDir[T](block: File => T): T = {
val baseDir = File.newTemporaryDirectory("file-watch-service")
try {
block(baseDir)
} finally {
baseDir.delete()
}
}
@volatile var changed: Boolean = false
@volatile var startTime: Long = 0
@volatile var endTime: Long = 0
private def reset(): Unit = {
Thread.sleep(200)
changed = false
startTime = System.nanoTime
}
private def reportChange(): Unit = {
endTime = System.nanoTime
changed = true
}
private def assertChanged() = {
val deadline = 5.seconds.fromNow
while (!changed) {
if (deadline.isOverdue()) {
failure("Changed did not become true within 5 seconds")
}
Thread.sleep(200)
}
printf("%s: %7.1f ms\n", getClass.getSimpleName, (endTime - startTime).nanoseconds.toUnit(MILLISECONDS))
ok
}
private def watchFiles[T](files: File*)(block: => T): T = {
val watcher = watchService.watch(files.map(_.toJava).asJava, maybePath => reportChange())
reset()
try {
block
} finally {
watcher.stop()
}
}
"The file watch service" should {
"detect new files" in withTempDir { dir =>
watchFiles(dir) {
(dir / "test").write("new file")
assertChanged()
}
}
"detect changes on files" in withTempDir { dir =>
val testFile = dir / "test"
testFile.write("new file")
watchFiles(dir) {
testFile.write("changed")
assertChanged()
}
}
"detect changes on files in new subdirectories" in withTempDir { dir =>
val subDir = dir.createChild("subdir", asDirectory = true)
watchFiles(dir) {
(subDir / "test").write("new file")
assertChanged()
}
}
}
}