mirror of
https://github.com/corda/corda.git
synced 2025-01-01 02:36:44 +00:00
replaces uses of java.Date with java.time;
changed nullable timestamp variables to non-nullable
This commit is contained in:
parent
7d71fa338a
commit
80640cca91
@ -6,9 +6,9 @@ import net.corda.node.utilities.AffinityExecutor
|
|||||||
import net.corda.nodeapi.internal.persistence.CordaPersistence
|
import net.corda.nodeapi.internal.persistence.CordaPersistence
|
||||||
import net.corda.nodeapi.internal.persistence.NODE_DATABASE_PREFIX
|
import net.corda.nodeapi.internal.persistence.NODE_DATABASE_PREFIX
|
||||||
import org.hibernate.Session
|
import org.hibernate.Session
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.time.Duration
|
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.ScheduledExecutorService
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
@ -36,7 +36,6 @@ class RunOnceService(private val database: CordaPersistence, private val machine
|
|||||||
|
|
||||||
private val log = loggerFor<RunOnceService>()
|
private val log = loggerFor<RunOnceService>()
|
||||||
private val running = AtomicBoolean(false)
|
private val running = AtomicBoolean(false)
|
||||||
private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (waitInterval <= updateInterval) {
|
if (waitInterval <= updateInterval) {
|
||||||
@ -47,7 +46,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = TABLE)
|
@Table(name = TABLE)
|
||||||
class MutualExclusion(machineNameInit: String, pidInit: String) {
|
class MutualExclusion(machineNameInit: String, pidInit: String, timeStampInit: LocalDateTime) {
|
||||||
@Column(name = ID, insertable = false, updatable = false)
|
@Column(name = ID, insertable = false, updatable = false)
|
||||||
@Id
|
@Id
|
||||||
val id: Char = 'X'
|
val id: Char = 'X'
|
||||||
@ -59,8 +58,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine
|
|||||||
val pid = pidInit
|
val pid = pidInit
|
||||||
|
|
||||||
@Column(name = TIMESTAMP)
|
@Column(name = TIMESTAMP)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
val timestamp = timeStampInit
|
||||||
val timestamp: Date? = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
@ -130,7 +128,7 @@ class RunOnceService(private val database: CordaPersistence, private val machine
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTimestamp(session: Session, mutualExclusion: MutualExclusion): Boolean {
|
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 " +
|
val query = session.createNativeQuery("UPDATE $TABLE SET $MACHINE_NAME = :machineName, $TIMESTAMP = CURRENT_TIMESTAMP, $PID = :pid " +
|
||||||
"WHERE $ID = 'X' AND " +
|
"WHERE $ID = 'X' AND " +
|
||||||
|
|
||||||
@ -165,4 +163,4 @@ class RunOnceService(private val database: CordaPersistence, private val machine
|
|||||||
|
|
||||||
return returnValue == 1
|
return returnValue == 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import org.junit.Before
|
|||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.contrib.java.lang.system.ExpectedSystemExit
|
import org.junit.contrib.java.lang.system.ExpectedSystemExit
|
||||||
import java.util.*
|
import java.time.LocalDateTime
|
||||||
import java.util.concurrent.ScheduledExecutorService
|
import java.util.concurrent.ScheduledExecutorService
|
||||||
import java.util.concurrent.ScheduledFuture
|
import java.util.concurrent.ScheduledFuture
|
||||||
import javax.persistence.Query
|
import javax.persistence.Query
|
||||||
@ -64,7 +64,9 @@ class RunOnceServiceTest {
|
|||||||
fun `row updated when change of master node`() {
|
fun `row updated when change of master node`() {
|
||||||
runOnceServiceMachine1.start()
|
runOnceServiceMachine1.start()
|
||||||
|
|
||||||
var firstTimestamp: Date? = null
|
var secondTimestamp = LocalDateTime.now()
|
||||||
|
var firstTimestamp = LocalDateTime.now()
|
||||||
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine1RowCheck(query)
|
val result = machine1RowCheck(query)
|
||||||
@ -76,14 +78,13 @@ class RunOnceServiceTest {
|
|||||||
|
|
||||||
runOnceServiceMachine2.start()
|
runOnceServiceMachine2.start()
|
||||||
|
|
||||||
var secondTimestamp: Date? = null
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine2RowCheck(query)
|
val result = machine2RowCheck(query)
|
||||||
secondTimestamp = result.timestamp
|
secondTimestamp = result.timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant()))
|
assertTrue(secondTimestamp.isAfter(firstTimestamp))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -105,7 +106,9 @@ class RunOnceServiceTest {
|
|||||||
fun `row updated when last run was same machine`() {
|
fun `row updated when last run was same machine`() {
|
||||||
runOnceServiceMachine1.start()
|
runOnceServiceMachine1.start()
|
||||||
|
|
||||||
var firstTimestamp: Date? = null
|
var secondTimestamp = LocalDateTime.now()
|
||||||
|
var firstTimestamp = LocalDateTime.now()
|
||||||
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine1RowCheck(query)
|
val result = machine1RowCheck(query)
|
||||||
@ -117,14 +120,13 @@ class RunOnceServiceTest {
|
|||||||
|
|
||||||
runOnceServiceMachine1.start()
|
runOnceServiceMachine1.start()
|
||||||
|
|
||||||
var secondTimestamp: Date? = null
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine1RowCheck(query)
|
val result = machine1RowCheck(query)
|
||||||
secondTimestamp = result.timestamp
|
secondTimestamp = result.timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant()))
|
assertTrue(secondTimestamp.isAfter(firstTimestamp))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -132,7 +134,9 @@ class RunOnceServiceTest {
|
|||||||
whenever(mockUpdateExecutor.scheduleAtFixedRate(any(), any(), any(), any())).thenAnswer { invocation ->
|
whenever(mockUpdateExecutor.scheduleAtFixedRate(any(), any(), any(), any())).thenAnswer { invocation ->
|
||||||
val runnable = invocation.arguments[0] as Runnable
|
val runnable = invocation.arguments[0] as Runnable
|
||||||
|
|
||||||
var firstTimestamp: Date? = null
|
var secondTimestamp = LocalDateTime.now()
|
||||||
|
var firstTimestamp = LocalDateTime.now()
|
||||||
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine1RowCheck(query)
|
val result = machine1RowCheck(query)
|
||||||
@ -141,14 +145,13 @@ class RunOnceServiceTest {
|
|||||||
|
|
||||||
runnable.run()
|
runnable.run()
|
||||||
|
|
||||||
var secondTimestamp: Date? = null
|
|
||||||
database.transaction {
|
database.transaction {
|
||||||
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
val query = session.createNativeQuery(selectQuery, RunOnceService.MutualExclusion::class.java)
|
||||||
val result = machine1RowCheck(query)
|
val result = machine1RowCheck(query)
|
||||||
secondTimestamp = result.timestamp
|
secondTimestamp = result.timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(secondTimestamp!!.toInstant().isAfter(firstTimestamp!!.toInstant()))
|
assertTrue(secondTimestamp.isAfter(firstTimestamp))
|
||||||
|
|
||||||
mock<ScheduledFuture<*>>()
|
mock<ScheduledFuture<*>>()
|
||||||
}
|
}
|
||||||
@ -228,7 +231,7 @@ class RunOnceServiceTest {
|
|||||||
assertEquals('X', result.id)
|
assertEquals('X', result.id)
|
||||||
assertEquals("machine1", result.machineName)
|
assertEquals("machine1", result.machineName)
|
||||||
assertEquals("123", result.pid)
|
assertEquals("123", result.pid)
|
||||||
assertTrue(result.timestamp is Date)
|
assertTrue(result.timestamp is LocalDateTime)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,7 +241,7 @@ class RunOnceServiceTest {
|
|||||||
assertEquals('X', result.id)
|
assertEquals('X', result.id)
|
||||||
assertEquals("machine2", result.machineName)
|
assertEquals("machine2", result.machineName)
|
||||||
assertEquals("789", result.pid)
|
assertEquals("789", result.pid)
|
||||||
assertTrue(result.timestamp is Date)
|
assertTrue(result.timestamp is LocalDateTime)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user