Skip to content

Commit

Permalink
chore(kv): Test the updating of latest success/failure fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettbuddin committed Aug 25, 2020
1 parent 515b0a6 commit d3632f8
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions kv/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,85 @@ func TestTaskRunCancellation(t *testing.T) {
}
}

func TestService_UpdateTask_RecordLatestSuccessAndFailure(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

c := clock.NewMock()
c.Set(time.Unix(1000, 0))

ts := newService(t, ctx, c)
defer ts.Close()

ctx = icontext.SetAuthorizer(ctx, &ts.Auth)

originalTask, err := ts.Service.CreateTask(ctx, influxdb.TaskCreate{
Flux: `option task = {name: "a task",every: 1h} from(bucket:"test") |> range(start:-1h)`,
OrganizationID: ts.Org.ID,
OwnerID: ts.User.ID,
Status: string(influxdb.TaskActive),
})
if err != nil {
t.Fatal("CreateTask", err)
}

c.Add(1 * time.Second)
exp := c.Now()
updatedTask, err := ts.Service.UpdateTask(ctx, originalTask.ID, influxdb.TaskUpdate{
LatestCompleted: &exp,
LatestScheduled: &exp,

// These would be updated in a mutually exclusive manner, but we'll set
// them both to demonstrate that they do change.
LatestSuccess: &exp,
LatestFailure: &exp,
})
if err != nil {
t.Fatal("UpdateTask", err)
}

if got := updatedTask.LatestScheduled; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestCompleted; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestSuccess; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestFailure; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}

c.Add(5 * time.Second)
exp = c.Now()
updatedTask, err = ts.Service.UpdateTask(ctx, originalTask.ID, influxdb.TaskUpdate{
LatestCompleted: &exp,
LatestScheduled: &exp,

// These would be updated in a mutually exclusive manner, but we'll set
// them both to demonstrate that they do change.
LatestSuccess: &exp,
LatestFailure: &exp,
})
if err != nil {
t.Fatal("UpdateTask", err)
}

if got := updatedTask.LatestScheduled; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestCompleted; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestSuccess; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
if got := updatedTask.LatestFailure; !got.Equal(exp) {
t.Fatalf("unexpected -got/+exp\n%s", cmp.Diff(got.String(), exp.String()))
}
}

type taskOptions struct {
name string
every string
Expand Down

0 comments on commit d3632f8

Please sign in to comment.