Repository: pingcap/tidb
File: pkg/statistics/handle/autoanalyze/refresher/refresher_test.go (Go)

Code before:
	handle := dom.StatsHandle()
	now := time.Now()
	// Set the auto-analyze time window to a future time.
	tk.MustExec("set @@global.tidb_auto_analyze_start_time = ?", now.Add(time.Hour).Format("15:04"))
	tk.MustExec("set @@global.tidb_auto_analyze_end_time = ?", now.Add(2*time.Hour).Format("15:04"))

	tk.MustExec("use test")
	tk.MustExec("create table t (a int, b int, index idx(a))")
	tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))
	tk.MustExec("analyze table t")

	r := refresher.NewRefresher(context.Background(), handle, dom.SysProcTracker(), dom.DDLNotifier())
	defer r.Close()

	tk.MustExec("insert into t values (5, 4), (5, 5), (6, 6)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))

	require.NoError(t, util.CallWithSCtx(handle.SPool(), func(sctx sessionctx.Context) error {
		require.False(t, r.AnalyzeHighestPriorityTables(sctx), "Because it's out of time window, no jobs should be executed")
		return nil
	}))
	r.WaitAutoAnalyzeFinishedForTest()
	require.Equal(t, 1, r.Len(), "The job queue should be initialized even if it's out of time window")
}

func TestChangePruneMode(t *testing.T) {
	statistics.AutoAnalyzeMinCnt = 0
	defer func() {
		statistics.AutoAnalyzeMinCnt = 1000
	}()

	store, dom := testkit.CreateMockStoreAndDomain(t)
	handle := dom.StatsHandle()
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")
	tk.MustExec("create table t1 (a int, b int, index idx(a)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (140))")
	tk.MustExec("insert into t1 values (0, 0)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))
	tk.MustExec("analyze table t1")
	r := refresher.NewRefresher(context.Background(), handle, dom.SysProcTracker(), dom.DDLNotifier())
	defer r.Close()

	// Insert more data to each partition.
	tk.MustExec("insert into t1 values (1, 1), (11, 11)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))


Review comment (suggestion):
The comment refers to "Because it's out of time window" but uses a contraction. For consistency with the codebase style and professional documentation, consider rephrasing to "Because it is out of the time window, no jobs should be executed".
```suggestion
		require.False(t, r.AnalyzeHighestPriorityTables(sctx), "Because it is out of the time window, no jobs should be executed")
		return nil
	}))
	r.WaitAutoAnalyzeFinishedForTest()
	require.Equal(t, 1, r.Len(), "The job queue should be initialized even if it is out of the time window")
```

Code after:
	handle := dom.StatsHandle()
	now := time.Now()
	// Set the auto-analyze time window to a future time.
	tk.MustExec("set @@global.tidb_auto_analyze_start_time = ?", now.Add(time.Hour).Format("15:04"))
	tk.MustExec("set @@global.tidb_auto_analyze_end_time = ?", now.Add(2*time.Hour).Format("15:04"))

	tk.MustExec("use test")
	tk.MustExec("create table t (a int, b int, index idx(a))")
	tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))
	tk.MustExec("analyze table t")

	r := refresher.NewRefresher(context.Background(), handle, dom.SysProcTracker(), dom.DDLNotifier())
	defer r.Close()

	tk.MustExec("insert into t values (5, 4), (5, 5), (6, 6)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))

	require.NoError(t, util.CallWithSCtx(handle.SPool(), func(sctx sessionctx.Context) error {
		require.False(t, r.AnalyzeHighestPriorityTables(sctx), "Because it's out of time window, no jobs should be executed")
		return nil
	}))
	r.WaitAutoAnalyzeFinishedForTest()
	require.Equal(t, 1, r.Len(), "The job queue should be initialized even if it is out of the time window")
}

func TestChangePruneMode(t *testing.T) {
	statistics.AutoAnalyzeMinCnt = 0
	defer func() {
		statistics.AutoAnalyzeMinCnt = 1000
	}()

	store, dom := testkit.CreateMockStoreAndDomain(t)
	handle := dom.StatsHandle()
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")
	tk.MustExec("create table t1 (a int, b int, index idx(a)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (140))")
	tk.MustExec("insert into t1 values (0, 0)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))
	tk.MustExec("analyze table t1")
	r := refresher.NewRefresher(context.Background(), handle, dom.SysProcTracker(), dom.DDLNotifier())
	defer r.Close()

	// Insert more data to each partition.
	tk.MustExec("insert into t1 values (1, 1), (11, 11)")
	require.NoError(t, handle.DumpStatsDeltaToKV(true))
	require.NoError(t, handle.Update(context.Background(), dom.InfoSchema()))