github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/replica_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/runtime" 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 TestNewReplica(t *testing.T) { 29 r := newReplica(newTestTNShard(1, 2, 3), runtime.DefaultRuntime()) 30 select { 31 case <-r.startedC: 32 assert.Fail(t, "cannot started") 33 default: 34 } 35 } 36 37 func TestCloseNotStartedReplica(t *testing.T) { 38 r := newReplica(newTestTNShard(1, 2, 3), runtime.DefaultRuntime()) 39 assert.NoError(t, r.close(false)) 40 } 41 42 func TestWaitStarted(t *testing.T) { 43 r := newReplica(newTestTNShard(1, 2, 3), runtime.DefaultRuntime()) 44 c := make(chan struct{}) 45 go func() { 46 r.waitStarted() 47 c <- struct{}{} 48 }() 49 50 ts := service.NewTestTxnService(t, 1, service.NewTestSender(), service.NewTestClock(1)) 51 defer func() { 52 assert.NoError(t, ts.Close(false)) 53 }() 54 55 assert.NoError(t, r.start(ts)) 56 defer func() { 57 assert.NoError(t, r.close(false)) 58 }() 59 select { 60 case <-c: 61 case <-time.After(time.Minute): 62 assert.Fail(t, "wait started failed") 63 } 64 } 65 66 func TestHandleLocalCNRequestsWillReturnError(t *testing.T) { 67 r := newReplica(newTestTNShard(1, 2, 3), runtime.DefaultRuntime()) 68 ts := service.NewTestTxnService(t, 1, service.NewTestSender(), service.NewTestClock(1)) 69 assert.NoError(t, r.start(ts)) 70 defer func() { 71 assert.NoError(t, r.close(false)) 72 assert.NoError(t, ts.Close(false)) 73 }() 74 75 req := service.NewTestReadRequest(1, txn.TxnMeta{}, 1) 76 assert.Error(t, r.handleLocalRequest(context.Background(), &req, &txn.TxnResponse{})) 77 }