package copr_paging

import (
	"flag"
	"math/rand"
	"time"

	_ "github.com/go-sql-driver/mysql"
	. "github.com/onsi/ginkgo"
	"github.com/pingcap/test-infra/sdk/resource"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s"

	. "github.com/pingcap/endless/pkg/util"
)

const (
	prepareConc  = 128
	testConc     = 8
	stateCnt     = 10
	prepareCnt   = 25600
	checkCnt     = 20
	testDuration = 1 * time.Hour
)

const (
	pagingNowriteNoErr    = "paging-nowrite-noerr"
	pagingWriteNoErr      = "paging-write-noerr"
	pagingWriteRegionErr  = "paging-write-regionerr"
	pagingWriteNetworkErr = "paging-write-networkerr"
	pagingWriteLockedErr  = "paging-write-lockederr"
	pagingWritePodFailure = "paging-write-podfailure"
	pagingWriteAllErr     = "paging-write-allerr"
)

var _ = ParameterizedDescribe("Paging-Coprocessor", func(flagSet *flag.FlagSet) {
	ctx := suiteTestCtx

	Context("IndexLookUp", func() {
		tc := ctx.Resource("tc").(resource.TiDBCluster)
		table := "indexlookup_test"

		BeforeEach(func() {
			rand.Seed(time.Now().UnixNano())
			prepareIndexLookUp(tc, table)
		})

		TestCase("Run the paging case, no write, no error inject", pagingNowriteNoErr, func() {
			testIndexLookUpPaging(pagingNowriteNoErr, tc, table, false, false, false, false, false)
		})

		TestCase("Run the paging case, with write, no error inject", pagingWriteNoErr, func() {
			testIndexLookUpPaging(pagingWriteNoErr, tc, table, true, false, false, false, false)
		})

		TestCase("Run the paging case, with write, region error inject", pagingWriteRegionErr, func() {
			testIndexLookUpPaging(pagingWriteRegionErr, tc, table, true, true, false, false, false)
		})

		TestCase("Run the paging case, with write, network error inject", pagingWriteNetworkErr, func() {
			testIndexLookUpPaging(pagingWriteNetworkErr, tc, table, true, false, true, false, false)
		})

		TestCase("Run the paging case, with write, locked error inject", pagingWriteLockedErr, func() {
			testIndexLookUpPaging(pagingWriteLockedErr, tc, table, true, false, false, true, false)
		})

		TestCase("Run the paging case, with write, pod failure", pagingWritePodFailure, func() {
			testIndexLookUpPaging(pagingWritePodFailure, tc, table, true, false, false, false, true)
		})

		TestCase("Run the paging case, with write, all errors inject", pagingWriteAllErr, func() {
			testIndexLookUpPaging(pagingWriteAllErr, tc, table, true, true, true, true, false)
		})
	})
})
