diff --git a/ttl/ttlworker/BUILD.bazel b/ttl/ttlworker/BUILD.bazel index 347bb566834f2..bebd3995bf055 100644 --- a/ttl/ttlworker/BUILD.bazel +++ b/ttl/ttlworker/BUILD.bazel @@ -43,6 +43,7 @@ go_test( name = "ttlworker_test", srcs = [ "del_test.go", + "job_integration_test.go", "job_manager_integration_test.go", "job_manager_test.go", "job_test.go", diff --git a/ttl/ttlworker/job.go b/ttl/ttlworker/job.go index 8628e9a6c9746..02f402f90888d 100644 --- a/ttl/ttlworker/job.go +++ b/ttl/ttlworker/job.go @@ -47,7 +47,7 @@ const finishJobTemplate = `UPDATE mysql.tidb_ttl_table_status const updateJobStateTemplate = "UPDATE mysql.tidb_ttl_table_status SET current_job_state = %? WHERE table_id = %? AND current_job_id = %? AND current_job_owner_id = %?" func updateJobCurrentStatusSQL(tableID int64, oldStatus cache.JobStatus, newStatus cache.JobStatus, jobID string) (string, []interface{}) { - return updateJobCurrentStatusTemplate, []interface{}{newStatus, tableID, oldStatus, jobID} + return updateJobCurrentStatusTemplate, []interface{}{string(newStatus), tableID, string(oldStatus), jobID} } func finishJobSQL(tableID int64, finishTime time.Time, summary string, jobID string) (string, []interface{}) { diff --git a/ttl/ttlworker/job_integration_test.go b/ttl/ttlworker/job_integration_test.go new file mode 100644 index 0000000000000..b90c4a550d858 --- /dev/null +++ b/ttl/ttlworker/job_integration_test.go @@ -0,0 +1,41 @@ +// Copyright 2022 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ttlworker_test + +import ( + "context" + "testing" + + dbsession "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/ttl/cache" + "github.com/pingcap/tidb/ttl/session" + "github.com/pingcap/tidb/ttl/ttlworker" + "github.com/stretchr/testify/require" +) + +func TestChangeStatus(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + dbSession, err := dbsession.CreateSession4Test(store) + require.NoError(t, err) + se := session.NewSession(dbSession, dbSession, nil) + + job := ttlworker.NewTTLJob(&cache.PhysicalTable{ID: 0}, "0", cache.JobStatusWaiting) + tk.MustExec("insert into mysql.tidb_ttl_table_status(table_id,current_job_id,current_job_status) VALUES(0, '0', 'waiting')") + require.NoError(t, job.ChangeStatus(context.Background(), se, cache.JobStatusRunning)) + tk.MustQuery("select current_job_status from mysql.tidb_ttl_table_status").Check(testkit.Rows("running")) +} diff --git a/ttl/ttlworker/job_test.go b/ttl/ttlworker/job_test.go index 7645777327225..19075b905e22d 100644 --- a/ttl/ttlworker/job_test.go +++ b/ttl/ttlworker/job_test.go @@ -15,12 +15,27 @@ package ttlworker import ( + "context" "testing" "github.com/pingcap/errors" + "github.com/pingcap/tidb/ttl/cache" + "github.com/pingcap/tidb/ttl/session" "github.com/stretchr/testify/assert" ) +func NewTTLJob(tbl *cache.PhysicalTable, id string, status cache.JobStatus) *ttlJob { + return &ttlJob{ + tbl: tbl, + id: id, + status: status, + } +} + +func (j *ttlJob) ChangeStatus(ctx context.Context, se session.Session, status cache.JobStatus) error { + return j.changeStatus(ctx, se, status) +} + func TestIterScanTask(t *testing.T) { tbl := newMockTTLTbl(t, "t1")