-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DPP-468 StorageBackend tests (#10529)
* Add StorageBackend tests changelog_begin changelog_end * Fix Oracle tests * Do not use empty byte arrays * Format * Fix after rebase * Substitute type params with type bounded ingest method * Remove empty line * Assert on configuration contents * Fix Oracle build * Add tests for ingestion initialization * fmt * Add test for leftover data after reset * Add resetAll * Use resetAll between tests Co-authored-by: Marton Nagy <[email protected]>
- Loading branch information
1 parent
4e08b47
commit 8501832
Showing
18 changed files
with
1,064 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 0 additions & 73 deletions
73
...ntegration-api/src/test/lib/scala/platform/store/backend/StorageBackendPostgresSpec.scala
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
...nt-integration-api/src/test/lib/scala/platform/store/backend/StorageBackendProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.platform.store.backend | ||
|
||
import java.sql.Connection | ||
|
||
import com.daml.platform.store.backend.h2.H2StorageBackend | ||
import com.daml.platform.store.backend.oracle.OracleStorageBackend | ||
import com.daml.platform.store.backend.postgresql.PostgresStorageBackend | ||
import com.daml.testing.oracle.OracleAroundAll | ||
import com.daml.testing.postgresql.PostgresAroundAll | ||
import org.scalatest.Suite | ||
|
||
/** Creates a database and a [[StorageBackend]]. | ||
* Used by [[StorageBackendSpec]] to run all StorageBackend tests on different databases. | ||
*/ | ||
private[backend] trait StorageBackendProvider { | ||
protected def jdbcUrl: String | ||
protected def backend: StorageBackend[_] | ||
protected final def ingest(dbDtos: Vector[DbDto], connection: Connection): Unit = { | ||
def typeBoundIngest[T](backend: StorageBackend[T]): Unit = | ||
backend.insertBatch(connection, backend.batch(dbDtos)) | ||
typeBoundIngest(backend) | ||
} | ||
} | ||
|
||
private[backend] trait StorageBackendProviderPostgres | ||
extends StorageBackendProvider | ||
with PostgresAroundAll { this: Suite => | ||
override protected def jdbcUrl: String = postgresDatabase.url | ||
override protected val backend: StorageBackend[_] = PostgresStorageBackend | ||
} | ||
|
||
private[backend] trait StorageBackendProviderH2 extends StorageBackendProvider { this: Suite => | ||
override protected def jdbcUrl: String = "jdbc:h2:mem:storage_backend_provider;db_close_delay=-1" | ||
override protected val backend: StorageBackend[_] = H2StorageBackend | ||
} | ||
|
||
private[backend] trait StorageBackendProviderOracle | ||
extends StorageBackendProvider | ||
with OracleAroundAll { this: Suite => | ||
override protected def jdbcUrl: String = | ||
s"jdbc:oracle:thin:$oracleUser/$oraclePwd@localhost:$oraclePort/ORCLPDB1" | ||
override protected val backend: StorageBackend[_] = OracleStorageBackend | ||
} |
95 changes: 95 additions & 0 deletions
95
...cipant-integration-api/src/test/lib/scala/platform/store/backend/StorageBackendSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.platform.store.backend | ||
|
||
import java.sql.Connection | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import com.codahale.metrics.MetricRegistry | ||
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll | ||
import com.daml.ledger.resources.{Resource, ResourceContext} | ||
import com.daml.logging.{ContextualizedLogger, LoggingContext} | ||
import com.daml.metrics.Metrics | ||
import com.daml.platform.configuration.ServerRole | ||
import com.daml.platform.store.appendonlydao.DbDispatcher | ||
import com.daml.platform.store.FlywayMigrations | ||
import org.scalatest.{AsyncTestSuite, BeforeAndAfterEach} | ||
|
||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
|
||
private[backend] trait StorageBackendSpec | ||
extends AkkaBeforeAndAfterAll | ||
with BeforeAndAfterEach | ||
with StorageBackendProvider { this: AsyncTestSuite => | ||
|
||
protected val logger: ContextualizedLogger = ContextualizedLogger.get(getClass) | ||
implicit protected val loggingContext: LoggingContext = LoggingContext.ForTesting | ||
|
||
private val connectionPoolSize: Int = 16 | ||
private val metrics = new Metrics(new MetricRegistry) | ||
|
||
// Initialized in beforeAll() | ||
private var dbDispatcherResource: Resource[DbDispatcher] = _ | ||
private var dbDispatcher: DbDispatcher = _ | ||
|
||
protected def executeSql[T](sql: Connection => T): Future[T] = { | ||
dbDispatcher.executeSql(metrics.test.db)(sql) | ||
} | ||
|
||
override protected def beforeAll(): Unit = { | ||
super.beforeAll() | ||
|
||
implicit val resourceContext: ResourceContext = ResourceContext(system.dispatcher) | ||
implicit val loggingContext: LoggingContext = LoggingContext.ForTesting | ||
dbDispatcherResource = for { | ||
_ <- Resource.fromFuture( | ||
new FlywayMigrations(jdbcUrl).migrate(enableAppendOnlySchema = true) | ||
) | ||
dispatcher <- DbDispatcher | ||
.owner( | ||
dataSource = backend.createDataSource(jdbcUrl), | ||
serverRole = ServerRole.Testing(this.getClass), | ||
connectionPoolSize = connectionPoolSize, | ||
connectionTimeout = FiniteDuration(250, "millis"), | ||
metrics = metrics, | ||
) | ||
.acquire() | ||
} yield dispatcher | ||
|
||
dbDispatcher = Await.result(dbDispatcherResource.asFuture, 30.seconds) | ||
logger.info( | ||
s"Finished setting up database $jdbcUrl for tests. You can now connect to this database to debug failed tests. Note that tables are truncated between each test." | ||
) | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
Await.result(dbDispatcherResource.release(), 30.seconds) | ||
super.afterAll() | ||
} | ||
|
||
private val runningTests = new AtomicInteger(0) | ||
|
||
// Each test should start with an empty database to allow testing low-level behavior | ||
// However, creating a fresh database for each test would be too expensive. | ||
// Instead, we truncate all tables using the reset() call before each test. | ||
override protected def beforeEach(): Unit = { | ||
super.beforeEach() | ||
|
||
assert( | ||
runningTests.incrementAndGet() == 1, | ||
"StorageBackendSpec tests must not run in parallel, as they all run against the same database.", | ||
) | ||
Await.result(executeSql(backend.resetAll), 10.seconds) | ||
} | ||
|
||
override protected def afterEach(): Unit = { | ||
assert( | ||
runningTests.decrementAndGet() == 0, | ||
"StorageBackendSpec tests must not run in parallel, as they all run against the same database.", | ||
) | ||
|
||
super.afterEach() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ipant-integration-api/src/test/lib/scala/platform/store/backend/StorageBackendSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.platform.store.backend | ||
|
||
import org.scalatest.flatspec.AsyncFlatSpec | ||
|
||
trait StorageBackendSuite | ||
extends StorageBackendSpec | ||
with StorageBackendTestsInitialization | ||
with StorageBackendTestsInitializeIngestion | ||
with StorageBackendTestsIngestion | ||
with StorageBackendTestsReset | ||
with StorageBackendTestsPruning { | ||
this: AsyncFlatSpec => | ||
} |
Oops, something went wrong.