Skip to content

Commit 86f1553

Browse files
authored
Timer Abstraction (#29)
* timer abstraction for unit testing * timer abstraction for unit testing * added timer test * fix from PR * added to minor version
1 parent 5b4368b commit 86f1553

File tree

8 files changed

+137
-7
lines changed

8 files changed

+137
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
}
3333

3434
group = 'org.team5499'
35-
version = '2.3.0'
35+
version = '2.4.0'
3636

3737
task sourcesJar(type: Jar) {
3838
from sourceSets.main.allJava

src/main/kotlin/org/team5499/monkeyLib/auto/Action.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.team5499.monkeyLib.auto
22

3-
import edu.wpi.first.wpilibj.Timer
3+
import org.team5499.monkeyLib.util.time.ITimer
4+
import org.team5499.monkeyLib.util.time.WPITimer
45

5-
public open class Action(timeoutSeconds: Double) {
6-
private val mTimer: Timer
6+
public open class Action(timeoutSeconds: Double, timer: ITimer = WPITimer()) {
7+
8+
private val mTimer: ITimer
79
private val mTimeoutSeconds: Double
810

911
init {
10-
mTimer = Timer()
12+
mTimer = timer
1113
mTimeoutSeconds = timeoutSeconds
1214
}
1315

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
package org.team5499.monkeyLib.auto
22

3-
public class NothingAction(timeout: Double) : Action(timeout)
3+
import org.team5499.monkeyLib.util.time.ITimer
4+
import org.team5499.monkeyLib.util.time.WPITimer
5+
6+
public class NothingAction(timeout: Double, timer: ITimer = WPITimer()) : Action(timeout, timer)

src/main/kotlin/org/team5499/monkeyLib/auto/ParallelAction.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.team5499.monkeyLib.auto
22

3-
class ParallelAction(vararg actions: Action) : Action(0.0) {
3+
import org.team5499.monkeyLib.util.time.ITimer
4+
import org.team5499.monkeyLib.util.time.WPITimer
5+
6+
class ParallelAction(vararg actions: Action, timer: ITimer = WPITimer()) : Action(0.0, timer) {
47

58
private val mActions: Array<out Action>
69

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.team5499.monkeyLib.util.time
2+
3+
/**
4+
* Timer abstraction layer to allow unit testing
5+
*/
6+
public interface ITimer {
7+
8+
/**
9+
* Function to get time since timer started
10+
* @return number of ellapsed seconds
11+
*/
12+
public fun get(): Double
13+
14+
/**
15+
* Starts the timer
16+
*/
17+
public fun start(): Unit
18+
19+
/**
20+
* stops the timer
21+
*/
22+
public fun stop(): Unit
23+
24+
/**
25+
* resets the timer to 0.
26+
*/
27+
public fun reset(): Unit
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.team5499.monkeyLib.util.time
2+
3+
public class SystemTimer : ITimer {
4+
5+
private var mStartTime: Long
6+
private var mTotalPauseTime: Long
7+
private var mStartPauseTime: Long
8+
private var mPaused: Boolean
9+
10+
init {
11+
mStartTime = 0L
12+
mTotalPauseTime = 0L
13+
mStartPauseTime = 0L
14+
15+
mPaused = false
16+
}
17+
18+
public override fun get(): Double {
19+
@Suppress("MagicNumber")
20+
return (mStartTime - System.currentTimeMillis() - mTotalPauseTime) / 1000.0
21+
}
22+
23+
public override fun start() {
24+
if (mPaused) {
25+
mTotalPauseTime += System.currentTimeMillis() - mStartPauseTime
26+
mPaused = false
27+
}
28+
mStartTime = System.currentTimeMillis()
29+
}
30+
31+
public override fun stop() {
32+
if (!mPaused) {
33+
mStartPauseTime = System.currentTimeMillis()
34+
mPaused = true
35+
}
36+
}
37+
38+
public override fun reset() {
39+
mStartTime = 0L
40+
mTotalPauseTime = 0L
41+
mTotalPauseTime = 0L
42+
mPaused = false
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.team5499.monkeyLib.util.time
2+
3+
import edu.wpi.first.wpilibj.Timer
4+
5+
public class WPITimer(timer: Timer = Timer()) : ITimer {
6+
7+
private val mTimer: Timer
8+
9+
init {
10+
mTimer = timer
11+
}
12+
13+
public override fun get() = mTimer.get()
14+
15+
public override fun start() = mTimer.start()
16+
17+
public override fun stop() = mTimer.stop()
18+
19+
public override fun reset() = mTimer.reset()
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package tests.utils
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
import org.team5499.monkeyLib.util.time.ITimer
7+
import org.team5499.monkeyLib.util.time.SystemTimer
8+
9+
public class TimerTest {
10+
11+
private val kEpsilon = 1E6
12+
13+
@Test
14+
fun timerSystemTest() {
15+
val timer: ITimer = SystemTimer()
16+
timer.start()
17+
assertEquals(timer.get(), 0.0, kEpsilon)
18+
Thread.sleep(2000L)
19+
assertEquals(timer.get(), 2.0, kEpsilon)
20+
timer.stop()
21+
Thread.sleep(1000L)
22+
timer.start()
23+
Thread.sleep(1000L)
24+
assertEquals(timer.get(), 3.0, kEpsilon)
25+
timer.stop()
26+
timer.reset()
27+
timer.start()
28+
assertEquals(timer.get(), 0.0, kEpsilon)
29+
}
30+
}

0 commit comments

Comments
 (0)