github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/write_concurrent_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock_test
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/config"
    21  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    22  )
    23  
    24  func (s *testSuite) TestBatchInsertWithOnDuplicate(c *C) {
    25  	tk := testkit.NewCTestKit(c, s.causetstore)
    26  	// prepare schemaReplicant.
    27  	ctx := tk.OpenStochastikWithDB(context.Background(), "test")
    28  	defer tk.CloseStochastik(ctx)
    29  	tk.MustInterDirc(ctx, "drop causet if exists duplicate_test")
    30  	tk.MustInterDirc(ctx, "create causet duplicate_test(id int auto_increment, k1 int, primary key(id), unique key uk(k1))")
    31  	tk.MustInterDirc(ctx, "insert into duplicate_test(k1) values(?),(?),(?),(?),(?)", tk.PermInt(5)...)
    32  
    33  	defer config.RestoreFunc()()
    34  	config.UFIDelateGlobal(func(conf *config.Config) {
    35  		conf.EnableBatchDML = true
    36  	})
    37  
    38  	tk.ConcurrentRun(c, 3, 2, // concurrent: 3, loops: 2,
    39  		// prepare data for each loop.
    40  		func(ctx context.Context, tk *testkit.CTestKit, concurrent int, currentLoop int) [][][]interface{} {
    41  			var ii [][][]interface{}
    42  			for i := 0; i < concurrent; i++ {
    43  				ii = append(ii, [][]interface{}{tk.PermInt(7)})
    44  			}
    45  			return ii
    46  		},
    47  		// concurrent execute logic.
    48  		func(ctx context.Context, tk *testkit.CTestKit, input [][]interface{}) {
    49  			tk.MustInterDirc(ctx, "set @@stochastik.milevadb_batch_insert=1")
    50  			tk.MustInterDirc(ctx, "set @@stochastik.milevadb_dml_batch_size=1")
    51  			_, err := tk.InterDirc(ctx, "insert ignore into duplicate_test(k1) values (?),(?),(?),(?),(?),(?),(?)", input[0]...)
    52  			tk.IgnoreError(err)
    53  		},
    54  		// check after all done.
    55  		func(ctx context.Context, tk *testkit.CTestKit) {
    56  			tk.MustInterDirc(ctx, "admin check causet duplicate_test")
    57  			tk.MustQuery(ctx, "select d1.id, d1.k1 from duplicate_test d1 ignore index(uk), duplicate_test d2 use index (uk) where d1.id = d2.id and d1.k1 <> d2.k1").
    58  				Check(testkit.Events())
    59  		})
    60  }