Skip to content

Commit 2c3223d

Browse files
committed
add continuous mode test
1 parent 6722282 commit 2c3223d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

robot/impl/jobmanager_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,90 @@ func TestJobManagerHistory(t *testing.T) {
122122
// TODO: test flaky job with both successes and panics
123123
}
124124

125+
// Test continuous mode, include switching to and from.
126+
func TestJobContinuousSchedule(t *testing.T) {
127+
logger := logging.NewTestLogger(t)
128+
129+
fakeSensorComponent := []resource.Config{
130+
{
131+
Model: resource.DefaultModelFamily.WithModel("fake"),
132+
Name: "sensor",
133+
API: sensor.API,
134+
},
135+
}
136+
137+
cfg := &config.Config{
138+
Components: fakeSensorComponent,
139+
Jobs: []config.JobConfig{
140+
{
141+
config.JobConfigData{
142+
Name: "fake sensor",
143+
Schedule: "2s",
144+
Resource: "sensor",
145+
Method: "GetReadings",
146+
},
147+
},
148+
},
149+
}
150+
cfgCron := &config.Config{
151+
Components: fakeSensorComponent,
152+
Jobs: []config.JobConfig{
153+
{
154+
config.JobConfigData{
155+
Name: "fake sensor",
156+
Schedule: "*/5 * * * * *",
157+
Resource: "sensor",
158+
Method: "GetReadings",
159+
},
160+
},
161+
},
162+
}
163+
cfgContinuous := &config.Config{
164+
Components: fakeSensorComponent,
165+
Jobs: []config.JobConfig{
166+
{
167+
config.JobConfigData{
168+
Name: "fake sensor",
169+
Schedule: "continuous",
170+
Resource: "sensor",
171+
Method: "GetReadings",
172+
},
173+
},
174+
},
175+
}
176+
ctx, ctxCancelFunc := context.WithCancel(context.Background())
177+
defer ctxCancelFunc()
178+
lr := setupLocalRobot(t, ctx, cfg, logger)
179+
180+
test.That(t, lr.JobManager().NumJobHistories.Load(), test.ShouldEqual, 1)
181+
182+
time.Sleep(10 * time.Second)
183+
jh, ok := lr.JobManager().JobHistories.Load("fake sensor")
184+
successes := jh.Successes()
185+
test.That(t, ok, test.ShouldBeTrue)
186+
test.That(t, len(successes), test.ShouldBeLessThan, 10)
187+
test.That(t, successes[len(successes)-1].AsTime().Sub(successes[0].AsTime()), test.ShouldBeGreaterThan, time.Second)
188+
189+
// Switch from duration to continuous
190+
lr.Reconfigure(ctx, cfgContinuous)
191+
time.Sleep(10 * time.Second)
192+
jh, ok = lr.JobManager().JobHistories.Load("fake sensor")
193+
successes = jh.Successes()
194+
test.That(t, ok, test.ShouldBeTrue)
195+
test.That(t, len(successes), test.ShouldBeGreaterThanOrEqualTo, 10)
196+
test.That(t, successes[len(successes)-1].AsTime().Sub(successes[0].AsTime()), test.ShouldBeLessThan, time.Second)
197+
198+
// Swtich from continuous to cron
199+
lr.Reconfigure(ctx, cfgCron)
200+
time.Sleep(10 * time.Second)
201+
jh, ok = lr.JobManager().JobHistories.Load("fake sensor")
202+
successes = jh.Successes()
203+
test.That(t, ok, test.ShouldBeTrue)
204+
// History still contains runs from prev
205+
test.That(t, len(successes), test.ShouldBeGreaterThanOrEqualTo, 10)
206+
test.That(t, successes[len(successes)-1].AsTime().Sub(successes[0].AsTime()), test.ShouldBeGreaterThan, time.Second)
207+
}
208+
125209
func TestJobManagerConfigChanges(t *testing.T) {
126210
logger := logging.NewTestLogger(t)
127211
model := resource.DefaultModelFamily.WithModel(utils.RandomAlphaString(8))

0 commit comments

Comments
 (0)