forked from robotpy/robotpy-commands-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_schedulingrecursion.py
165 lines (126 loc) · 4.5 KB
/
test_schedulingrecursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from typing import TYPE_CHECKING
import commands2
from util import * # type: ignore
if TYPE_CHECKING:
from .util import *
import pytest
@pytest.mark.parametrize(
"interruptionBehavior",
[
commands2.InterruptionBehavior.kCancelIncoming,
commands2.InterruptionBehavior.kCancelSelf,
],
)
def test_cancelFromInitialize(
interruptionBehavior: commands2.InterruptionBehavior,
scheduler: commands2.CommandScheduler,
):
hasOtherRun = OOBoolean()
requirement = commands2.Subsystem()
selfCancels = commands2.Command()
selfCancels.addRequirements(requirement)
selfCancels.getInterruptionBehavior = lambda: interruptionBehavior
selfCancels.initialize = lambda: scheduler.cancel(selfCancels)
other = commands2.RunCommand(lambda: hasOtherRun.set(True), requirement)
scheduler.schedule(selfCancels)
scheduler.run()
scheduler.schedule(other)
assert not scheduler.isScheduled(selfCancels)
assert scheduler.isScheduled(other)
scheduler.run()
assert hasOtherRun == True
@pytest.mark.parametrize(
"interruptionBehavior",
[
commands2.InterruptionBehavior.kCancelIncoming,
commands2.InterruptionBehavior.kCancelSelf,
],
)
def test_defaultCommandGetsRescheduledAfterSelfCanceling(
interruptionBehavior: commands2.InterruptionBehavior,
scheduler: commands2.CommandScheduler,
):
hasOtherRun = OOBoolean()
requirement = commands2.Subsystem()
selfCancels = commands2.Command()
selfCancels.addRequirements(requirement)
selfCancels.getInterruptionBehavior = lambda: interruptionBehavior
selfCancels.initialize = lambda: scheduler.cancel(selfCancels)
other = commands2.RunCommand(lambda: hasOtherRun.set(True), requirement)
scheduler.setDefaultCommand(requirement, other)
scheduler.schedule(selfCancels)
scheduler.run()
scheduler.run()
assert not scheduler.isScheduled(selfCancels)
assert scheduler.isScheduled(other)
scheduler.run()
assert hasOtherRun == True
def test_cancelFromEnd(scheduler: commands2.CommandScheduler):
counter = OOInteger()
selfCancels = commands2.Command()
@patch_via_decorator(selfCancels)
def end(self, interrupted):
counter.incrementAndGet()
scheduler.cancel(self)
scheduler.schedule(selfCancels)
scheduler.cancel(selfCancels)
assert counter == 1
assert not scheduler.isScheduled(selfCancels)
def test_scheduleFromEnd(scheduler: commands2.CommandScheduler):
counter = OOInteger()
requirement = commands2.Subsystem()
other = commands2.InstantCommand(lambda: None, requirement)
selfCancels = commands2.Command()
selfCancels.addRequirements(requirement)
@patch_via_decorator(selfCancels)
def end(self, interrupted):
counter.incrementAndGet()
scheduler.schedule(other)
scheduler.schedule(selfCancels)
scheduler.cancel(selfCancels)
assert counter == 1
assert not scheduler.isScheduled(selfCancels)
def test_scheduleFromEndInterrupt(scheduler: commands2.CommandScheduler):
counter = OOInteger()
requirement = commands2.Subsystem()
other = commands2.InstantCommand(lambda: None, requirement)
selfCancels = commands2.Command()
selfCancels.addRequirements(requirement)
@patch_via_decorator(selfCancels)
def end(self, interrupted):
counter.incrementAndGet()
scheduler.schedule(other)
scheduler.schedule(selfCancels)
scheduler.schedule(other)
assert counter == 1
assert not scheduler.isScheduled(selfCancels)
assert scheduler.isScheduled(other)
@pytest.mark.parametrize(
"interruptionBehavior",
[
commands2.InterruptionBehavior.kCancelIncoming,
commands2.InterruptionBehavior.kCancelSelf,
],
)
def test_scheduleInitializeFromDefaultCommand(
interruptionBehavior: commands2.InterruptionBehavior,
scheduler: commands2.CommandScheduler,
):
counter = OOInteger()
requirement = commands2.Subsystem()
other = commands2.InstantCommand(lambda: None, requirement).withInterruptBehavior(
interruptionBehavior
)
defaultCommand = commands2.Command()
defaultCommand.addRequirements(requirement)
@patch_via_decorator(defaultCommand)
def initialize(self):
counter.incrementAndGet()
scheduler.schedule(other)
scheduler.setDefaultCommand(requirement, defaultCommand)
scheduler.run()
scheduler.run()
scheduler.run()
assert counter == 3
assert not scheduler.isScheduled(defaultCommand)
assert scheduler.isScheduled(other)