github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/shardddl/pessimism/ops_test.go (about)

     1  // Copyright 2020 PingCAP, 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 pessimism
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  )
    19  
    20  func (t *testForEtcd) TestPutOperationDeleteInfo(c *C) {
    21  	defer clearTestInfoOperation(c)
    22  
    23  	var (
    24  		task   = "test"
    25  		source = "mysql-replica-1"
    26  		DDLs   = []string{"ALTER TABLE bar ADD COLUMN c1 INT"}
    27  		info   = NewInfo(task, source, "foo", "bar", DDLs)
    28  		op     = NewOperation("test-ID", task, source, DDLs, true, false)
    29  	)
    30  
    31  	// put info.
    32  	_, err := PutInfo(etcdTestCli, info)
    33  	c.Assert(err, IsNil)
    34  
    35  	// verify the info exists.
    36  	ifm, _, err := GetAllInfo(etcdTestCli)
    37  	c.Assert(err, IsNil)
    38  	c.Assert(ifm, HasLen, 1)
    39  	c.Assert(ifm, HasKey, task)
    40  	c.Assert(ifm[task][source], DeepEquals, info)
    41  
    42  	// verify no operations exist.
    43  	opm, _, err := GetAllOperations(etcdTestCli)
    44  	c.Assert(err, IsNil)
    45  	c.Assert(opm, HasLen, 0)
    46  
    47  	// put operation & delete info.
    48  	done, _, err := PutOperationDeleteExistInfo(etcdTestCli, op, info)
    49  	c.Assert(err, IsNil)
    50  	c.Assert(done, IsTrue)
    51  
    52  	// verify no info exit.
    53  	ifm, _, err = GetAllInfo(etcdTestCli)
    54  	c.Assert(err, IsNil)
    55  	c.Assert(ifm, HasLen, 0)
    56  
    57  	// verify the operation exists.
    58  	opm, _, err = GetAllOperations(etcdTestCli)
    59  	c.Assert(err, IsNil)
    60  	c.Assert(opm, HasLen, 1)
    61  	c.Assert(opm, HasKey, task)
    62  	c.Assert(opm[task][source], DeepEquals, op)
    63  
    64  	// try to put operation & delete info again, succeed(to support reentrant).
    65  	done, _, err = PutOperationDeleteExistInfo(etcdTestCli, op, info)
    66  	c.Assert(err, IsNil)
    67  	c.Assert(done, IsTrue)
    68  
    69  	// PUT info and operation.
    70  	_, err = PutInfo(etcdTestCli, info)
    71  	c.Assert(err, IsNil)
    72  	_, _, err = PutOperations(etcdTestCli, true, op)
    73  	c.Assert(err, IsNil)
    74  
    75  	// verify the info exists.
    76  	ifm, _, err = GetAllInfo(etcdTestCli)
    77  	c.Assert(err, IsNil)
    78  	c.Assert(ifm, HasLen, 1)
    79  	c.Assert(ifm, HasKey, task)
    80  	c.Assert(ifm[task][source], DeepEquals, info)
    81  
    82  	// verify the operation exists.
    83  	opm, _, err = GetAllOperations(etcdTestCli)
    84  	c.Assert(err, IsNil)
    85  	c.Assert(opm, HasLen, 1)
    86  	c.Assert(opm, HasKey, task)
    87  	c.Assert(opm[task][source], DeepEquals, op)
    88  
    89  	// DELETE info and operation.
    90  	_, err = DeleteInfosOperations(etcdTestCli, []Info{info}, []Operation{op})
    91  	c.Assert(err, IsNil)
    92  
    93  	// verify no info exit.
    94  	ifm, _, err = GetAllInfo(etcdTestCli)
    95  	c.Assert(err, IsNil)
    96  	c.Assert(ifm, HasLen, 0)
    97  
    98  	// verify no operations exist.
    99  	opm, _, err = GetAllOperations(etcdTestCli)
   100  	c.Assert(err, IsNil)
   101  	c.Assert(opm, HasLen, 0)
   102  
   103  	// put a done operation into etcd and try to delete operation again.
   104  	op.Done = true
   105  	_, _, err = PutOperations(etcdTestCli, true, op)
   106  	op.Done = false
   107  	c.Assert(err, IsNil)
   108  
   109  	// try to put operation & delete info again, fail(operation not equal).
   110  	done, _, err = PutOperationDeleteExistInfo(etcdTestCli, op, info)
   111  	c.Assert(err, IsNil)
   112  	c.Assert(done, IsFalse)
   113  
   114  	// DELETE info and operation.
   115  	_, err = DeleteInfosOperations(etcdTestCli, []Info{info}, []Operation{op})
   116  	c.Assert(err, IsNil)
   117  }