github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/store_rpc_handler_test.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tnservice 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 "github.com/matrixorigin/matrixone/pkg/pb/txn" 24 "github.com/matrixorigin/matrixone/pkg/txn/service" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestHandleRead(t *testing.T) { 29 runTNStoreTest(t, func(s *store) { 30 shard := newTestTNShard(1, 2, 3) 31 assert.NoError(t, s.StartTNReplica(shard)) 32 33 req := service.NewTestReadRequest(1, service.NewTestTxn(1, 1, 1), 1) 34 req.CNRequest.Target.ReplicaID = 2 35 assert.NoError(t, s.handleRead(context.Background(), &req, &txn.TxnResponse{})) 36 }) 37 } 38 39 func TestHandleReadWithRetry(t *testing.T) { 40 runTNStoreTest(t, func(s *store) { 41 req := service.NewTestReadRequest(1, service.NewTestTxn(1, 1, 1), 1) 42 req.CNRequest.Target.ReplicaID = 2 43 req.Options = &txn.TxnRequestOptions{ 44 RetryCodes: []int32{int32(moerr.ErrTNShardNotFound)}, 45 } 46 go func() { 47 time.Sleep(time.Second) 48 shard := newTestTNShard(1, 2, 3) 49 assert.NoError(t, s.StartTNReplica(shard)) 50 }() 51 assert.NoError(t, s.handleRead(context.Background(), &req, &txn.TxnResponse{})) 52 }) 53 } 54 55 func TestHandleReadWithRetryWithTimeout(t *testing.T) { 56 runTNStoreTest(t, func(s *store) { 57 req := service.NewTestReadRequest(1, service.NewTestTxn(1, 1, 1), 1) 58 req.CNRequest.Target.ReplicaID = 2 59 req.Options = &txn.TxnRequestOptions{ 60 RetryCodes: []int32{int32(moerr.ErrTNShardNotFound)}, 61 } 62 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 63 defer cancel() 64 resp := &txn.TxnResponse{} 65 assert.NoError(t, s.handleRead(ctx, &req, resp)) 66 assert.Equal(t, uint32(moerr.ErrTNShardNotFound), resp.TxnError.Code) 67 }) 68 } 69 70 func TestHandleWrite(t *testing.T) { 71 runTNStoreTest(t, func(s *store) { 72 shard := newTestTNShard(1, 2, 3) 73 assert.NoError(t, s.StartTNReplica(shard)) 74 75 req := service.NewTestWriteRequest(1, service.NewTestTxn(1, 1, 1), 1) 76 req.CNRequest.Target.ReplicaID = 2 77 assert.NoError(t, s.handleWrite(context.Background(), &req, &txn.TxnResponse{})) 78 }) 79 } 80 81 func TestHandleCommit(t *testing.T) { 82 runTNStoreTest(t, func(s *store) { 83 shard := newTestTNShard(1, 2, 3) 84 assert.NoError(t, s.StartTNReplica(shard)) 85 86 req := service.NewTestCommitRequest(service.NewTestTxn(1, 1, 1)) 87 req.Txn.TNShards[0].ReplicaID = 2 88 assert.NoError(t, s.handleCommit(context.Background(), &req, &txn.TxnResponse{})) 89 }) 90 } 91 92 func TestHandleRollback(t *testing.T) { 93 runTNStoreTest(t, func(s *store) { 94 shard := newTestTNShard(1, 2, 3) 95 assert.NoError(t, s.StartTNReplica(shard)) 96 97 req := service.NewTestRollbackRequest(service.NewTestTxn(1, 1, 1)) 98 req.Txn.TNShards[0].ReplicaID = 2 99 assert.NoError(t, s.handleRollback(context.Background(), &req, &txn.TxnResponse{})) 100 }) 101 } 102 103 func TestHandlePrepare(t *testing.T) { 104 runTNStoreTest(t, func(s *store) { 105 shard := newTestTNShard(1, 2, 3) 106 assert.NoError(t, s.StartTNReplica(shard)) 107 108 req := service.NewTestPrepareRequest(service.NewTestTxn(1, 1, 1), 1) 109 req.PrepareRequest.TNShard.ReplicaID = 2 110 assert.NoError(t, s.handlePrepare(context.Background(), &req, &txn.TxnResponse{})) 111 112 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 113 defer cancel() 114 _, err := s.sender.Send(ctx, []txn.TxnRequest{req}) 115 assert.NoError(t, err) 116 }) 117 } 118 119 func TestHandleGetStatus(t *testing.T) { 120 runTNStoreTest(t, func(s *store) { 121 shard := newTestTNShard(1, 2, 3) 122 assert.NoError(t, s.StartTNReplica(shard)) 123 124 req := service.NewTestGetStatusRequest(service.NewTestTxn(1, 1, 1), 1) 125 req.GetStatusRequest.TNShard.ReplicaID = 2 126 assert.NoError(t, s.handleGetStatus(context.Background(), &req, &txn.TxnResponse{})) 127 128 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 129 defer cancel() 130 _, err := s.sender.Send(ctx, []txn.TxnRequest{req}) 131 assert.NoError(t, err) 132 }) 133 } 134 135 func TestHandleCommitTNShard(t *testing.T) { 136 runTNStoreTest(t, func(s *store) { 137 shard := newTestTNShard(1, 2, 3) 138 assert.NoError(t, s.StartTNReplica(shard)) 139 140 req := service.NewTestCommitShardRequest(service.NewTestTxn(1, 1, 1)) 141 req.CommitTNShardRequest.TNShard.ReplicaID = 2 142 assert.NoError(t, s.handleCommitTNShard(context.Background(), &req, &txn.TxnResponse{})) 143 144 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 145 defer cancel() 146 _, err := s.sender.Send(ctx, []txn.TxnRequest{req}) 147 assert.NoError(t, err) 148 }) 149 } 150 151 func TestHandleRollbackTNShard(t *testing.T) { 152 runTNStoreTest(t, func(s *store) { 153 shard := newTestTNShard(1, 2, 3) 154 assert.NoError(t, s.StartTNReplica(shard)) 155 156 req := service.NewTestRollbackShardRequest(service.NewTestTxn(1, 1, 1)) 157 req.RollbackTNShardRequest.TNShard.ReplicaID = 2 158 assert.NoError(t, s.handleRollbackTNShard(context.Background(), &req, &txn.TxnResponse{})) 159 160 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 161 defer cancel() 162 _, err := s.sender.Send(ctx, []txn.TxnRequest{req}) 163 assert.NoError(t, err) 164 }) 165 } 166 167 func TestHandleTNShartnotFound(t *testing.T) { 168 runTNStoreTest(t, func(s *store) { 169 req := service.NewTestRollbackShardRequest(service.NewTestTxn(1, 1, 1)) 170 resp := &txn.TxnResponse{} 171 assert.NoError(t, s.handleRollbackTNShard(context.Background(), &req, resp)) 172 assert.Equal(t, uint32(moerr.ErrTNShardNotFound), resp.TxnError.Code) 173 }) 174 } 175 176 func TestHandleDebug(t *testing.T) { 177 runTNStoreTest(t, func(s *store) { 178 shard := newTestTNShard(1, 2, 3) 179 assert.NoError(t, s.StartTNReplica(shard)) 180 181 req := service.NewTestReadRequest(1, service.NewTestTxn(1, 1, 1), 1) 182 req.Method = txn.TxnMethod_DEBUG 183 req.CNRequest.Target.ReplicaID = 2 184 assert.NoError(t, s.handleDebug(context.Background(), &req, &txn.TxnResponse{})) 185 }) 186 }