Skip to content

Commit

Permalink
sandbox: fail on already existing port-file. (#7929)
Browse files Browse the repository at this point in the history
Fixes #7806. This aligns the port file behaviour of the sandbox with the
HTTP JSON API.

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
Robin Krom authored Nov 17, 2020
1 parent 9bf4ef9 commit 5bfff4e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
7 changes: 4 additions & 3 deletions compatibility/bazel_tools/daml_ledger/Sandbox.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import System.Environment (getEnvironment)
import System.Exit (exitFailure)
import System.FilePath ((</>))
import System.IO.Error (isDoesNotExistError)
import System.IO.Extra (Handle, IOMode (..), hClose, newTempFile, openBinaryFile, stderr)
import System.IO.Extra (Handle, IOMode (..), hClose, newTempDir, openBinaryFile, stderr)
import System.Info.Extra (isWindows)
import System.Process
import Test.Tasty (TestTree, withResource)
Expand Down Expand Up @@ -113,9 +113,10 @@ createSandbox portFile sandboxOutput conf = do
withSandbox :: IO SandboxConfig -> (IO Int -> TestTree) -> TestTree
withSandbox getConf f =
withResource (openBinaryFile nullDevice ReadWriteMode) hClose $ \getDevNull ->
withResource newTempFile snd $ \getPortFile ->
withResource newTempDir snd $ \getTempDir ->
let createSandbox' = do
(portFile, _) <- getPortFile
(tempDir, _) <- getTempDir
let portFile = tempDir </> "sandbox-portfile"
devNull <- getDevNull
conf <- getConf
createSandbox portFile devNull conf
Expand Down
4 changes: 3 additions & 1 deletion compiler/damlc/tests/src/DA/Test/Repl/FuncTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ main = do
-- pulling apart functions like `withGrpcClient`. Therefore we just
-- allocate the resources before handing over to tasty and accept that
-- it will spin up sandbox and the repl client.
withTempFile $ \portFile ->
withTempDir $ \tmpDir ->
let portFile = tmpDir </> "sandbox-portfile"
in
withBinaryFile nullDevice WriteMode $ \devNull ->
bracket (createSandbox portFile devNull defaultSandboxConf { dars = testDars }) destroySandbox $ \SandboxResource{sandboxPort} ->
ReplClient.withReplClient (ReplClient.Options replJar (Just ("localhost", show sandboxPort)) Nothing Nothing Nothing Nothing ReplClient.ReplWallClock CreatePipe) $ \replHandle mbServiceOut processHandle ->
Expand Down
3 changes: 2 additions & 1 deletion daml-assistant/daml-helper/src/DA/Daml/Helper/Start.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ navigatorURL :: NavigatorPort -> String
navigatorURL (NavigatorPort p) = "http://localhost:" <> show p

withSandbox :: SandboxClassic -> SandboxPortSpec -> [String] -> (Process () () () -> SandboxPort -> IO a) -> IO a
withSandbox (SandboxClassic classic) portSpec extraArgs a = withTempFile $ \portFile -> do
withSandbox (SandboxClassic classic) portSpec extraArgs a = withTempDir $ \tempDir -> do
let portFile = tempDir </> "sandbox-portfile"
let sandbox = if classic then "sandbox-classic" else "sandbox"
let args = [ sandbox
, "--port", show (fromSandboxPortSpec portSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ trait MultiParticipantFixture
with AkkaBeforeAndAfterAll {
self: Suite =>
private def darFile = Paths.get(rlocation("daml-script/test/script-test.dar"))
private val participant1Portfile = Files.createTempFile("participant1", "port")
private val participant2Portfile = Files.createTempFile("participant2", "port")
private val tmpDir = Files.createTempDirectory("testMultiParticipantFixture")
private val participant1Portfile = tmpDir.resolve("participant1-portfile")
private val participant2Portfile = tmpDir.resolve("participant2-portfile")

override protected def afterAll(): Unit = {
Files.delete(participant1Portfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.daml.platform.apiserver

import java.io.File
import java.nio.file.Files
import java.time.{Clock, Instant}

import akka.actor.ActorSystem
Expand All @@ -26,14 +25,14 @@ import com.daml.platform.configuration.{
PartyConfiguration,
ServerRole
}
import com.daml.ports.{PortFiles}
import com.daml.platform.index.JdbcIndex
import com.daml.platform.packages.InMemoryPackageStore
import com.daml.platform.services.time.TimeProviderType
import com.daml.platform.store.dao.events.LfValueTranslation
import com.daml.ports.Port
import io.grpc.{BindableService, ServerInterceptor}

import scala.collection.JavaConverters._
import scala.collection.immutable

// Main entry point to start an index server that also hosts the ledger API.
Expand Down Expand Up @@ -150,6 +149,6 @@ final class StandaloneApiServer(

private def writePortFile(port: Port): Unit =
config.portFile.foreach { path =>
Files.write(path, Seq(port.toString).asJava)
PortFiles.write(path, port)
}
}
5 changes: 3 additions & 2 deletions libs-haskell/test-utils/DA/Test/Sandbox.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ createSandbox portFile sandboxOutput conf = do

withSandbox :: SandboxConfig -> (IO Int -> TestTree) -> TestTree
withSandbox conf f =
withResource newTempFile snd $ \getPortFile ->
withResource newTempDir snd $ \getTmpDir ->
let createSandbox' = do
(portFile, _) <- getPortFile
(tempDir, _) <- getTmpDir
let portFile = tempDir </> "sandbox-portfile"
createSandbox portFile stdout conf
in withResource createSandbox' destroySandbox (f . fmap sandboxPort)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ object PortFiles {
}

private def writeUnsafe(path: Path, port: Port): Unit = {
import java.nio.file.StandardOpenOption.CREATE_NEW
val lines: java.lang.Iterable[String] = List(port.value.toString).asJava
val created = Files.write(path, lines, CREATE_NEW)
val tmpFile = Files.createTempFile("portfile", "")
Files.write(tmpFile, lines)
val created = Files.move(tmpFile, path)
created.toFile.deleteOnExit()
}
}

0 comments on commit 5bfff4e

Please sign in to comment.