# Structure of this test:
# =======================

# This suite runs a few of the tests detailed on `README.md`

## WARNING: some assertions on this test are implicit.
##  - e.g. asserting dev.mode doesn't exit the JVM is implicitly asserted by
##    running `Mode.Dev tests` first and asserting the whole test moves on to
##    `Mode.Test tests`


### --------------
### Mode.Dev tests
### --------------

## Start dev mode
> run

## Mode.Dev doesn't create a PID_FILE
-$ exists target/universal/stage/RUNNING_PID

# The app started
> verifyResourceContains / 200

# Force a reload (copy a new file + produce a request)
$ copy-file changes/HomeController.scala src/main/scala/controllers/HomeController.scala
# Need to wait a little while, because incremental compilation timestamps.
$ sleep 1000
> verifyResourceContains / 200
$ exists target/proofs/application-actorsystem-name.txt
$ delete target/proofs/application-actorsystem-name.txt
-$ exists target/proofs/application-actorsystem-name.txt

## Stopping Mode.Dev runs Coordinated Shutdown in both Server and Application Actor Systems
> playStop
$ exists target/proofs/application-actorsystem-name.txt

# TODO: assert the `play-dev-mode` CS was executed
# Asserting the `play-dev-mode` CS was executed is tricky because it's not available to the user
# # $ exists target/proofs/play-dev-mode.txt

## Dev.Mode doesn't exit the JVM
#  - This step is implicitly verified if the test moves forward because the JVM is still alive.



### ---------------
### Mode.Test tests
### ---------------

## Cleanup previous execution leftovers.
$ delete target/universal/stage/RUNNING_PID
-$ exists target/universal/stage/RUNNING_PID
$ delete target/proofs/application-actorsystem-name.txt
-$ exists target/proofs/application-actorsystem-name.txt


## Run user integration tests
> test

# Mode.Test doesn't create a PID_FILE but runs CoordinatedShutdown
-$ exists target/universal/stage/RUNNING_PID
$ exists target/proofs/application-actorsystem-name.txt

## Test.Mode doesn't exit the JVM
#  - This step is implicitly verified if the test moves forward because the JVM is still alive.




### ---------------
### Mode.Prod tests
### ---------------

## 0. Cleanup previous execution leftovers.
$ delete target/universal/stage/RUNNING_PID
-$ exists target/universal/stage/RUNNING_PID
$ delete target/proofs/application-actorsystem-name.txt
-$ exists target/proofs/application-actorsystem-name.txt


## 1. Start prod mode
> runProd --no-exit-sbt

# The app started (wait few seconds for the app to start)
$ sleep 1000
> verifyResourceContains / 200

# Mode.Prod creates a PID_FILE
$ exists target/universal/stage/RUNNING_PID

## 2. Mode.Prod exits the JVM
# SIGTERM-ing Mode.Prod runs Coordinated Shutdown for a single Actor System
# Mode.Prod exits runs the Coordinated Shutdown when there's a SIGTERM.
# SIGTERM is sent using Play's `stopProd` which extracts the `pid` from
# `RUNNING_PID` and uses `kill` to send the SIGTERM signal.

> assertProcessIsStopped
> awaitPidfileDeletion
$ exists target/proofs/application-actorsystem-name.txt
-$ exists target/universal/stage/RUNNING_PID


# SIGTERM-ing Mode.Prod must first finish in-flight requests

> runProd --no-exit-sbt
# The app started (wait few seconds for the app to start)
$ sleep 1000
> verifyResourceContains / 200

# Send a request to the slow action and record the result
> makeRequestAndRecordResponseBody /slow slow-request.txt
$ sleep 500

# Stops the process by sending a SIGTERM
> assertProcessIsStopped

# In-flight request should be recorded as expected
$ exists target/slow-request.txt
> checkRecordedRequestContains slow-request.txt DONE
