package upsert_ap_test

import (
	"context"
	"time"

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

	"github.com/pingcap/endless/testcase/tiflash/pkg"

	"github.com/pingcap/endless/pkg/matcher"
	"github.com/pingcap/endless/pkg/util"
	"github.com/pingcap/endless/testcase/tiflash/pkg/workload/ch"
)

var _ = Describe("tiflash-upsert-ap", func() {
	var chWorkload *ch.ChWorkload
	var duration time.Duration
	BeforeEach(func() {
		chWorkload = ch.NewWorkload(suiteTestCtx).(*ch.ChWorkload)
		duration = matcher.Try(time.ParseDuration(suiteTestCtx.Param("duration", "5h"))).(time.Duration)
		chWorkload.Prepare(suiteTestCtx)
	})
	Context("TiFlash ch benchmark workload", func() {
		It("Ap query should not fail", func() {
			ctx, _ := context.WithTimeout(suiteTestCtx, duration)
			g, _ := errgroup.WithContext(ctx)
			g.Go(util.WithGinkgoRecover(func() {
				matcher.Try(chWorkload.RunTP(ctx, pkg.ErrorHandler(
					pkg.TiDBStaleSchemaHandler,
					pkg.DeadlockFoundHandler,
					pkg.LockWaitTimeoutHandler)))
			}))
			g.Go(util.WithGinkgoRecover(func() {
				matcher.Try(chWorkload.RunAP(ctx, pkg.ErrorHandler(
					pkg.CtxDoneHandler,
					pkg.InvalidConnectionHandler)))
			}))
			matcher.Try(g.Wait())
		})
	})
})
