From 80640cca91743e124078e679aa74bcb80a831376 Mon Sep 17 00:00:00 2001 From: bpaunescu Date: Tue, 2 Jan 2018 15:22:03 +0000 Subject: [PATCH] replaces uses of java.Date with java.time; changed nullable timestamp variables to non-nullable --- .../services/persistence/RunOnceService.kt | 14 ++++----- .../persistence/RunOnceServiceTest.kt | 29 ++++++++++--------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/services/persistence/RunOnceService.kt b/node/src/main/kotlin/net/corda/node/services/persistence/RunOnceService.kt index a3528f8f9c..c55ae29f59 100644 --- a/node/src/main/kotlin/net/corda/node/services/persistence/RunOnceService.kt +++ b/node/src/main/kotlin/net/corda/node/services/persistence/RunOnceService.kt @@ -6,9 +6,9 @@ import net.corda.node.utilities.AffinityExecutor import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.NODE_DATABASE_PREFIX import org.hibernate.Session -import java.text.SimpleDateFormat import java.time.Duration -import java.util.Date +import java.time.LocalDateTime +import java.time.temporal.ChronoField import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean @@ -36,7 +36,6 @@ class RunOnceService(private val database: CordaPersistence, private val machine private val log = loggerFor() private val running = AtomicBoolean(false) - private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") init { if (waitInterval <= updateInterval) { @@ -47,7 +46,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine @Entity @Table(name = TABLE) - class MutualExclusion(machineNameInit: String, pidInit: String) { + class MutualExclusion(machineNameInit: String, pidInit: String, timeStampInit: LocalDateTime) { @Column(name = ID, insertable = false, updatable = false) @Id val id: Char = 'X' @@ -59,8 +58,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine val pid = pidInit @Column(name = TIMESTAMP) - @Temporal(TemporalType.TIMESTAMP) - val timestamp: Date? = null + val timestamp = timeStampInit } fun start() { @@ -130,7 +128,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine } private fun updateTimestamp(session: Session, mutualExclusion: MutualExclusion): Boolean { - val minWaitTime = simpleDateFormat.format(Date(mutualExclusion.timestamp!!.time + waitInterval)) + val minWaitTime = mutualExclusion.timestamp.plus(waitInterval, ChronoField.MILLI_OF_SECOND.baseUnit) val query = session.createNativeQuery("UPDATE $TABLE SET $MACHINE_NAME = :machineName, $TIMESTAMP = CURRENT_TIMESTAMP, $PID = :pid " + "WHERE $ID = 'X' AND " + @@ -165,4 +163,4 @@ class RunOnceService(private val database: CordaPersistence, private val machine return returnValue == 1 } -} \ No newline at end of file +} diff --git a/node/src/test/kotlin/net/corda/node/services/persistence/RunOnceServiceTest.kt b/node/src/test/kotlin/net/corda/node/services/persistence/RunOnceServiceTest.kt index 05568dc4dd..0af20f8e8c 100644 --- a/node/src/test/kotlin/net/corda/node/services/persistence/RunOnceServiceTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/persistence/RunOnceServiceTest.kt @@ -14,7 +14,7 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.contrib.java.lang.system.ExpectedSystemExit -import java.util.* +import java.time.LocalDateTime import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledFuture import javax.persistence.Query @@ -64,7 +64,9 @@ class RunOnceServiceTest { fun `row updated when change of master node`() { runOnceServiceMachine1.start() - var firstTimestamp: Date? = null + var secondTimestamp = LocalDateTime.now() + var firstTimestamp = LocalDateTime.now() + database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine1RowCheck(query) @@ -76,14 +78,13 @@ class RunOnceServiceTest { runOnceServiceMachine2.start() - var secondTimestamp: Date? = null database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine2RowCheck(query) secondTimestamp = result.timestamp } - assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant())) + assertTrue(secondTimestamp.isAfter(firstTimestamp)) } @Test @@ -105,7 +106,9 @@ class RunOnceServiceTest { fun `row updated when last run was same machine`() { runOnceServiceMachine1.start() - var firstTimestamp: Date? = null + var secondTimestamp = LocalDateTime.now() + var firstTimestamp = LocalDateTime.now() + database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine1RowCheck(query) @@ -117,14 +120,13 @@ class RunOnceServiceTest { runOnceServiceMachine1.start() - var secondTimestamp: Date? = null database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine1RowCheck(query) secondTimestamp = result.timestamp } - assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant())) + assertTrue(secondTimestamp.isAfter(firstTimestamp)) } @Test @@ -132,7 +134,9 @@ class RunOnceServiceTest { whenever(mockUpdateExecutor.scheduleAtFixedRate(any(), any(), any(), any())).thenAnswer { invocation -> val runnable = invocation.arguments[0] as Runnable - var firstTimestamp: Date? = null + var secondTimestamp = LocalDateTime.now() + var firstTimestamp = LocalDateTime.now() + database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine1RowCheck(query) @@ -141,14 +145,13 @@ class RunOnceServiceTest { runnable.run() - var secondTimestamp: Date? = null database.transaction { val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java) val result = machine1RowCheck(query) secondTimestamp = result.timestamp } - assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant())) + assertTrue(secondTimestamp.isAfter(firstTimestamp)) mock>() } @@ -228,7 +231,7 @@ class RunOnceServiceTest { assertEquals('X', result.id) assertEquals("machine1", result.machineName) assertEquals("123", result.pid) - assertTrue(result.timestamp is Date) + assertTrue(result.timestamp is LocalDateTime) return result } @@ -238,7 +241,7 @@ class RunOnceServiceTest { assertEquals('X', result.id) assertEquals("machine2", result.machineName) assertEquals("789", result.pid) - assertTrue(result.timestamp is Date) + assertTrue(result.timestamp is LocalDateTime) return result } -} \ No newline at end of file +}