Repository: pingcap/tidb
File: pkg/sessionctx/stmtctx/stmtctx_test.go (Go)

Code before:
	require.True(t, ok)
	require.Equal(t, int64(15), id)
	// exhausted
	require.True(t, reserved.Exhausted())
	id, ok = reserved.Consume()
	require.False(t, ok)
	require.Equal(t, int64(0), id)
}

func TestUsedStatsInfoForTableWriteToSlowLog(t *testing.T) {
	// pseudo stats: Version=0
	sPseudo := &stmtctx.UsedStatsInfoForTable{
		Name:          "t1",
		Version:       0,
		RealtimeCount: 1000,
		ModifyCount:   100,
	}
	var buf bytes.Buffer
	sPseudo.WriteToSlowLog(&buf)
	out := buf.String()
	require.Contains(t, out, "t1:")
	require.Contains(t, out, "stats_meta_version=pseudo")
	require.Contains(t, out, "realtime_count=1000")
	require.Contains(t, out, "modify_count=100")
	// pseudo returns early, so no "[index][column]" part
	require.NotContains(t, out, "][")
	require.Equal(t, "t1:stats_meta_version=pseudo[realtime_count=1000;modify_count=100]", out)

	// real stats: Version != 0
	buf.Reset()
	sReal := &stmtctx.UsedStatsInfoForTable{
		Name:          "orders",
		Version:       5,
		RealtimeCount: 1000000,
		ModifyCount:   500,
	}
	sReal.WriteToSlowLog(&buf)
	out = buf.String()
	require.Contains(t, out, "orders:")
	require.Contains(t, out, "stats_meta_version=5")
	require.Contains(t, out, "realtime_count=1000000")
	require.Contains(t, out, "modify_count=500")
	require.Equal(t, "orders:stats_meta_version=5[realtime_count=1000000;modify_count=500]", out)

	// real stats with column/index load status
	buf.Reset()
	sWithStatus := &stmtctx.UsedStatsInfoForTable{
		Name:                  "t2",
		Version:               10,
		RealtimeCount:         2000,
		ModifyCount:           0,

Review comment (suggestion):
```suggestion
	// pseudo returns early, so no "[index][column]" part
	require.NotContains(t, out, "][")
```

Code after:
	require.Equal(t, int64(15), id)
	// exhausted
	require.True(t, reserved.Exhausted())
	id, ok = reserved.Consume()
	require.False(t, ok)
	require.Equal(t, int64(0), id)
}

func TestUsedStatsInfoForTableWriteToSlowLog(t *testing.T) {
	// pseudo stats: Version=0
	sPseudo := &stmtctx.UsedStatsInfoForTable{
		Name:          "t1",
		Version:       0,
		RealtimeCount: 1000,
		ModifyCount:   100,
	}
	var buf bytes.Buffer
	sPseudo.WriteToSlowLog(&buf)
	out := buf.String()
	// pseudo returns early, so no "[index][column]" part
	require.NotContains(t, out, "][")
	require.Equal(t, "t1:stats_meta_version=pseudo[realtime_count=1000;modify_count=100]", out)

	// real stats: Version != 0
	buf.Reset()
	sReal := &stmtctx.UsedStatsInfoForTable{
		Name:          "orders",
		Version:       5,
		RealtimeCount: 1000000,
		ModifyCount:   500,
	}
	sReal.WriteToSlowLog(&buf)
	out = buf.String()
	require.Equal(t, "orders:stats_meta_version=5[realtime_count=1000000;modify_count=500]", out)

	// real stats with column/index load status
	buf.Reset()
	sWithStatus := &stmtctx.UsedStatsInfoForTable{
		Name:                  "t2",
		Version:               10,
		RealtimeCount:         2000,
		ModifyCount:           0,
		IndexStatsLoadStatus:  map[int64]string{1: "allLoaded"},
		ColumnStatsLoadStatus: map[int64]string{2: "onlyCmsEvicted"},
	}
	sWithStatus.WriteToSlowLog(&buf)
	out = buf.String()
	// TblInfo is nil so column/index names fall back to "ID <id>"; order: [index][column]
	require.Equal(t, "t2:stats_meta_version=10[realtime_count=2000;modify_count=0][ID 1:allLoaded][ID 2:onlyCmsEvicted]", out)
}