replaces uses of java.Date with java.time;

changed nullable timestamp variables to non-nullable
This commit is contained in:
bpaunescu 2018-01-02 15:22:03 +00:00
parent 7d71fa338a
commit 80640cca91
2 changed files with 22 additions and 21 deletions

View File

@ -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<RunOnceService>()
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
}
}
}

View File

@ -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<ScheduledFuture<*>>()
}
@ -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
}
}
}