Skip to content

Commit 563a51f

Browse files
author
James Hagborg
committed
Unit test PeriodicScheduler
1 parent e7d9c04 commit 563a51f

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

src/main/java/org/usfirst/frc/team69/util/PeriodicScheduler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public synchronized void addEvent(Runnable event) {
5050
events.add(event);
5151
}
5252

53+
/**
54+
* Remove all scheduled tasks. This is mainly useful for testing.
55+
*/
56+
public synchronized void clear() {
57+
events.clear();
58+
}
59+
5360
/**
5461
* Run all of the events which have been added to the scheduler. If you
5562
* are using {@link HYPERRobot}, you do not need to call this manually.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.usfirst.frc.team69.util.oi.test;
2+
3+
import org.usfirst.frc.team69.util.pref.PreferencesListener;
4+
5+
public class MockPreferencesListener implements PreferencesListener {
6+
7+
private int m_count;
8+
9+
public int getCount() {
10+
return m_count;
11+
}
12+
13+
@Override
14+
public void onPreferencesUpdated() {
15+
m_count++;
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.usfirst.frc.team69.util.oi.test;
2+
3+
public class MockRunnable implements Runnable {
4+
5+
private int m_count = 0;
6+
7+
public int getCount() {
8+
return m_count;
9+
}
10+
11+
@Override
12+
public void run() {
13+
m_count++;
14+
}
15+
16+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.usfirst.frc.team69.util.oi.test;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
import org.usfirst.frc.team69.util.PeriodicScheduler;
10+
11+
import edu.wpi.first.wpilibj.UnitTestUtility;
12+
13+
public class PeroidicSchedulerTest {
14+
15+
private PeriodicScheduler m_sched;
16+
17+
@BeforeClass
18+
public static void setUpBeforeClass() throws Exception {
19+
UnitTestUtility.setupMockBase();
20+
}
21+
22+
@Before
23+
public void setUp() {
24+
m_sched = PeriodicScheduler.getInstance();
25+
}
26+
27+
@After
28+
public void tearDown() {
29+
m_sched.clear();
30+
}
31+
32+
@Test
33+
public void testClearOnEmpty() {
34+
// just check that nothing crashes
35+
m_sched.clear();
36+
m_sched.run();
37+
}
38+
39+
@Test
40+
public void testClearWithElement() {
41+
MockRunnable event = new MockRunnable();
42+
m_sched.addEvent(event);
43+
m_sched.clear();
44+
m_sched.run();
45+
assertEquals(0, event.getCount());
46+
}
47+
48+
@Test
49+
public void testRunOnEmpty() {
50+
// just check nothing crashes
51+
m_sched.run();
52+
}
53+
54+
@Test
55+
public void testRunEvent() {
56+
MockRunnable event = new MockRunnable();
57+
m_sched.addEvent(event);
58+
m_sched.run();
59+
assertEquals(1, event.getCount());
60+
}
61+
62+
@Test
63+
public void testAddFromEvent() {
64+
MockRunnable event = new MockRunnable();
65+
66+
Runnable adder = new Runnable() {
67+
private boolean hasRun = false;
68+
@Override public void run() {
69+
if (!hasRun) {
70+
m_sched.addEvent(event);
71+
hasRun = true;
72+
}
73+
}
74+
};
75+
76+
m_sched.addEvent(adder);
77+
m_sched.run();
78+
assertEquals(1, event.getCount());
79+
m_sched.run();
80+
assertEquals(2, event.getCount());
81+
}
82+
83+
}

0 commit comments

Comments
 (0)