vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/vstreamer/main_flaky_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vstreamer
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	_flag "vitess.io/vitess/go/internal/flag"
    27  	"vitess.io/vitess/go/mysql"
    28  	"vitess.io/vitess/go/vt/dbconfigs"
    29  	"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
    30  	"vitess.io/vitess/go/vt/vttablet/tabletserver/vstreamer/testenv"
    31  )
    32  
    33  var (
    34  	engine *Engine
    35  	env    *testenv.Env
    36  
    37  	ignoreKeyspaceShardInFieldAndRowEvents bool
    38  )
    39  
    40  func TestMain(m *testing.M) {
    41  	_flag.ParseFlagsForTest()
    42  	ignoreKeyspaceShardInFieldAndRowEvents = true
    43  
    44  	exitCode := func() int {
    45  		var err error
    46  		env, err = testenv.Init()
    47  		if err != nil {
    48  			fmt.Fprintf(os.Stderr, "%v", err)
    49  			return 1
    50  		}
    51  		defer env.Close()
    52  
    53  		// engine cannot be initialized in testenv because it introduces
    54  		// circular dependencies
    55  		engine = NewEngine(env.TabletEnv, env.SrvTopo, env.SchemaEngine, nil, env.Cells[0])
    56  		engine.InitDBConfig(env.KeyspaceName, env.ShardName)
    57  		engine.Open()
    58  		defer engine.Close()
    59  
    60  		return m.Run()
    61  	}()
    62  	os.Exit(exitCode)
    63  }
    64  
    65  func customEngine(t *testing.T, modifier func(mysql.ConnParams) mysql.ConnParams) *Engine {
    66  	original, err := env.Dbcfgs.AppWithDB().MysqlParams()
    67  	require.NoError(t, err)
    68  	modified := modifier(*original)
    69  	config := env.TabletEnv.Config().Clone()
    70  	config.DB = dbconfigs.NewTestDBConfigs(modified, modified, modified.DbName)
    71  
    72  	engine := NewEngine(tabletenv.NewEnv(config, "VStreamerTest"), env.SrvTopo, env.SchemaEngine, nil, env.Cells[0])
    73  	engine.InitDBConfig(env.KeyspaceName, env.ShardName)
    74  	engine.Open()
    75  	return engine
    76  }