github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/observer/tidb_test.go (about) 1 // Copyright 2023 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 observer 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/DATA-DOG/go-sqlmock" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestTiDBObserver(t *testing.T) { 25 t.Parallel() 26 27 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) 28 require.NoError(t, err) 29 30 mock.ExpectQuery(queryConnIdleDurationStmt). 31 WillReturnRows( 32 sqlmock.NewRows( 33 []string{"time", "instance", "in_txn", "quantile", "value"}). 34 AddRow("2023-01-16 17:22:16.881000", "10.2.6.127:11080", 0, 0.9, 0.309), 35 ) 36 37 mock.ExpectQuery(queryConnCountStmt). 38 WillReturnRows( 39 sqlmock.NewRows( 40 []string{"time", "instance", "value"}). 41 AddRow("2023-01-10 16:44:39.123000", "10.2.6.127:11080", 24), 42 ) 43 44 mock.ExpectQuery(queryQueryDurationStmt). 45 WillReturnRows( 46 sqlmock.NewRows( 47 []string{"time", "instance", "sql_type", "value"}). 48 AddRow("2023-01-10 16:47:08.283000", "10.2.6.127:11080", 49 "Begin", 0.0018886375591793793). 50 AddRow("2023-01-10 16:47:08.283000", "10.2.6.127:11080", 51 "Insert", 0.014228768066070199). 52 AddRow("2023-01-10 16:47:08.283000", "10.2.6.127:11080", 53 "Delete", nil). 54 AddRow("2023-01-10 16:47:08.283000", "10.2.6.127:11080", 55 "Commit", 0.0004933262664880737), 56 ) 57 58 mock.ExpectQuery(queryTxnDurationStmt). 59 WillReturnRows( 60 sqlmock.NewRows( 61 []string{"time", "instance", "type", "value"}). 62 AddRow("2023-01-10 16:50:38.153000", "10.2.6.127:11080", 63 "abort", nil). 64 AddRow("2023-01-10 16:50:38.153000", "10.2.6.127:11080", 65 "commit", 0.06155323076923076). 66 AddRow("2023-01-10 16:50:38.153000", "10.2.6.127:11080", 67 "rollback", nil), 68 ) 69 mock.ExpectClose() 70 71 ctx := context.Background() 72 observer := NewTiDBObserver(db) 73 err = observer.Tick(ctx) 74 require.NoError(t, err) 75 err = observer.Close() 76 require.NoError(t, err) 77 }