Skip to content

Commit 609ecbd

Browse files
committed
add continuous mode test
1 parent 0503522 commit 609ecbd

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

robot/impl/jobmanager_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,106 @@ func TestJobManagerHistory(t *testing.T) {
133133
// TODO: test flaky job with both successes and panics
134134
}
135135

136+
// Test continuous mode, include switching to and from.
137+
func TestJobContinuousSchedule(t *testing.T) {
138+
logger := logging.NewTestLogger(t)
139+
140+
fakeSensorComponent := []resource.Config{
141+
{
142+
Model: resource.DefaultModelFamily.WithModel("fake"),
143+
Name: "sensor",
144+
API: sensor.API,
145+
},
146+
}
147+
148+
cfg := &config.Config{
149+
Components: fakeSensorComponent,
150+
Jobs: []config.JobConfig{
151+
{
152+
config.JobConfigData{
153+
Name: "fake sensor",
154+
Schedule: "2s",
155+
Resource: "sensor",
156+
Method: "GetReadings",
157+
},
158+
},
159+
},
160+
}
161+
cfgCron := &config.Config{
162+
Components: fakeSensorComponent,
163+
Jobs: []config.JobConfig{
164+
{
165+
config.JobConfigData{
166+
Name: "fake sensor",
167+
Schedule: "*/5 * * * * *",
168+
Resource: "sensor",
169+
Method: "GetReadings",
170+
},
171+
},
172+
},
173+
}
174+
cfgContinuous := &config.Config{
175+
Components: fakeSensorComponent,
176+
Jobs: []config.JobConfig{
177+
{
178+
config.JobConfigData{
179+
Name: "fake sensor",
180+
Schedule: "continuous",
181+
Resource: "sensor",
182+
Method: "GetReadings",
183+
},
184+
},
185+
},
186+
}
187+
ctx, ctxCancelFunc := context.WithCancel(context.Background())
188+
defer ctxCancelFunc()
189+
lr := setupLocalRobot(t, ctx, cfg, logger)
190+
o, _, addr := robottestutils.CreateBaseOptionsAndListener(t)
191+
err := lr.StartWeb(ctx, o)
192+
test.That(t, err, test.ShouldBeNil)
193+
robotClient, err := rclient.New(ctx, addr, logger)
194+
test.That(t, err, test.ShouldBeNil)
195+
defer robotClient.Close(ctx)
196+
197+
// Start running in 2s duration mode. Expect latest success timestamp - earliest > 1s
198+
time.Sleep(10 * time.Second)
199+
ms, err := robotClient.MachineStatus(ctx)
200+
test.That(t, err, test.ShouldBeNil)
201+
test.That(t, len(ms.JobStatuses), test.ShouldEqual, 1)
202+
jh, ok := ms.JobStatuses["fake sensor"]
203+
test.That(t, ok, test.ShouldBeTrue)
204+
successes := jh.RecentSuccessfulRuns
205+
test.That(t, len(successes), test.ShouldBeLessThan, 10)
206+
test.That(t, successes[len(successes)-1].Sub(successes[0]), test.ShouldBeGreaterThan, time.Second)
207+
208+
// Switch from 2s duration to continuous. Expect latest success timestamp - earliest < 1s
209+
lr.Reconfigure(ctx, cfgContinuous)
210+
time.Sleep(10 * time.Second)
211+
ms, err = robotClient.MachineStatus(ctx)
212+
test.That(t, err, test.ShouldBeNil)
213+
test.That(t, len(ms.JobStatuses), test.ShouldEqual, 1)
214+
jh, ok = ms.JobStatuses["fake sensor"]
215+
test.That(t, ok, test.ShouldBeTrue)
216+
successes = jh.RecentSuccessfulRuns
217+
test.That(t, ok, test.ShouldBeTrue)
218+
test.That(t, len(successes), test.ShouldBeGreaterThanOrEqualTo, 10)
219+
test.That(t, successes[len(successes)-1].Sub(successes[0]), test.ShouldBeLessThan, time.Second)
220+
221+
// Swtich from continuous to 2s cron. Expect latest success timestamp - earliest > 1s
222+
lr.Reconfigure(ctx, cfgCron)
223+
time.Sleep(10 * time.Second)
224+
ms, err = robotClient.MachineStatus(ctx)
225+
test.That(t, err, test.ShouldBeNil)
226+
test.That(t, len(ms.JobStatuses), test.ShouldEqual, 1)
227+
jh, ok = ms.JobStatuses["fake sensor"]
228+
test.That(t, ok, test.ShouldBeTrue)
229+
successes = jh.RecentSuccessfulRuns
230+
test.That(t, ok, test.ShouldBeTrue)
231+
// History still contains runs from prev. If stored size is 10, we still expect 10 here.
232+
test.That(t, len(successes), test.ShouldBeGreaterThanOrEqualTo, 10)
233+
test.That(t, successes[len(successes)-1].Sub(successes[0]), test.ShouldBeGreaterThan, time.Second)
234+
}
235+
136236
func TestJobManagerConfigChanges(t *testing.T) {
137237
logger := logging.NewTestLogger(t)
138238
model := resource.DefaultModelFamily.WithModel(utils.RandomAlphaString(8))

0 commit comments

Comments
 (0)