Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
pbernet committed May 22, 2024
1 parent 05823f5 commit c5fcb6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import scala.concurrent.Future
* From uploadSourceQueue do a HTTP file upload via [[Uploader]]
* Finally move the file to `rootDir/processed`
*
* Run with test class: [[DirectoryListenerSpec]]
* Run with test class: [[DirectoryWatcherSpec]]
*
* Remarks:
* - [[FileAlterationListenerAdaptor]] allows to recursively listen to file changes at runtime
* - Currently Alpakka DirectoryChangesSource can not do this, see:
* https://discuss.lightbend.com/t/using-directorychangessource-recursively/7630
* - Alternative Impl: https://github.com/gmethvin/directory-watcher
*/
class DirectoryListener(uploadDir: Path, processedDir: Path) {
class DirectoryWatcher(uploadDir: Path, processedDir: Path) {
val logger: Logger = LoggerFactory.getLogger(this.getClass)
implicit val system: ActorSystem = ActorSystem()

Expand Down Expand Up @@ -120,11 +120,11 @@ class DirectoryListener(uploadDir: Path, processedDir: Path) {
}

def stop(): Future[Terminated] = {
logger.info("About to shutdown DirectoryListener...")
logger.info("About to shutdown DirectoryWatcher...")
uploader.stop()
}
}

object DirectoryListener extends App {
def apply(uploadDir: Path, processedDir: Path) = new DirectoryListener(uploadDir, processedDir)
object DirectoryWatcher extends App {
def apply(uploadDir: Path, processedDir: Path) = new DirectoryWatcher(uploadDir, processedDir)
}
2 changes: 1 addition & 1 deletion src/main/scala/alpakka/file/uploader/Uploader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.util.{Failure, Success}

/**
* Upload file, eg from file system
* Is used by [[DirectoryListener]]
* Is used by [[DirectoryWatcher]]
*
* Also starts a mock server to handle the uploaded files
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package alpakka.file

import alpakka.file.uploader.DirectoryListener
import alpakka.file.uploader.DirectoryWatcher
import org.apache.commons.io.FileUtils
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec
Expand All @@ -13,35 +13,35 @@ import scala.util.Random

/**
* Designed as IT test on purpose to demonstrate
* the realistic usage of [[DirectoryListener]], hence we:
* the realistic usage of [[DirectoryWatcher]], hence we:
* - copy files to the file system before each test
* - clean up after each test
* - have a shared listener instance
*/
final class DirectoryListenerSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEachTestData {
final class DirectoryWatcherSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEachTestData {
val logger: Logger = LoggerFactory.getLogger(this.getClass)

var listener: DirectoryListener = _
var listener: DirectoryWatcher = _
var tmpRootDir: Path = _
var parentDir: Path = _
var processedDir: Path = _

"DirectoryListener" should {
"DirectoryWatcher" should {
"detect_files_on_startup" in {
listener = DirectoryListener(parentDir, processedDir)
waitForCondition(2.seconds)(listener.countFilesProcessed() == 2) shouldBe true
listener = DirectoryWatcher(parentDir, processedDir)
waitForCondition(3.seconds)(listener.countFilesProcessed() == 2) shouldBe true
}

"detect_added_files_at_runtime_in_parent" in {
copyTestFileToDir(parentDir)
listener = DirectoryListener(parentDir, processedDir)
waitForCondition(2.seconds)(listener.countFilesProcessed() == 2 + 1) shouldBe true
listener = DirectoryWatcher(parentDir, processedDir)
waitForCondition(3.seconds)(listener.countFilesProcessed() == 2 + 1) shouldBe true
}

"detect_added_files_at_runtime_in_subdir" in {
copyTestFileToDir(parentDir.resolve("subdir"))
listener = DirectoryListener(parentDir, processedDir)
waitForCondition(2.seconds)(listener.countFilesProcessed() == 2 + 1) shouldBe true
listener = DirectoryWatcher(parentDir, processedDir)
waitForCondition(3.seconds)(listener.countFilesProcessed() == 2 + 1) shouldBe true
}

"detect_added_nested_subdir_at_runtime_with_files_in_subdir" in {
Expand All @@ -55,16 +55,16 @@ final class DirectoryListenerSpec extends AsyncWordSpec with Matchers with Befor
val targetDir = Files.createDirectories(parentDir.resolve("subdir").resolve("nestedDirWithFiles"))
FileUtils.copyDirectory(tmpDir.toFile, targetDir.toFile)

listener = DirectoryListener(parentDir, processedDir)
waitForCondition(2.seconds)(listener.countFilesProcessed() == 2 + 2) shouldBe true
listener = DirectoryWatcher(parentDir, processedDir)
waitForCondition(3.seconds)(listener.countFilesProcessed() == 2 + 2) shouldBe true
}

"handle invalid parent directory path" in {
val invalidParentDir = Paths.get("/path/to/non-existent/directory")
val processedDir = Files.createTempDirectory("processed")

the[IllegalArgumentException] thrownBy {
listener = DirectoryListener(invalidParentDir, processedDir)
listener = DirectoryWatcher(invalidParentDir, processedDir)
} should have message s"Invalid upload directory path: $invalidParentDir"
}
}
Expand Down

0 comments on commit c5fcb6e

Please sign in to comment.