vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/repltracker/writer_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package repltracker 18 19 import ( 20 "fmt" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 26 "vitess.io/vitess/go/mysql/fakesqldb" 27 "vitess.io/vitess/go/sqltypes" 28 "vitess.io/vitess/go/vt/dbconfigs" 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" 31 ) 32 33 func TestWriteHeartbeat(t *testing.T) { 34 db := fakesqldb.New(t) 35 defer db.Close() 36 37 now := time.Now() 38 tw := newTestWriter(db, &now) 39 upsert := fmt.Sprintf("INSERT INTO %s.heartbeat (ts, tabletUid, keyspaceShard) VALUES (%d, %d, '%s') ON DUPLICATE KEY UPDATE ts=VALUES(ts), tabletUid=VALUES(tabletUid)", 40 "_vt", now.UnixNano(), tw.tabletAlias.Uid, tw.keyspaceShard) 41 db.AddQuery(upsert, &sqltypes.Result{}) 42 43 writes.Reset() 44 writeErrors.Reset() 45 46 tw.writeHeartbeat() 47 assert.Equal(t, int64(1), writes.Get()) 48 assert.Equal(t, int64(0), writeErrors.Get()) 49 } 50 51 func TestWriteHeartbeatError(t *testing.T) { 52 db := fakesqldb.New(t) 53 defer db.Close() 54 55 now := time.Now() 56 tw := newTestWriter(db, &now) 57 58 writes.Reset() 59 writeErrors.Reset() 60 61 tw.writeHeartbeat() 62 assert.Equal(t, int64(0), writes.Get()) 63 assert.Equal(t, int64(1), writeErrors.Get()) 64 } 65 66 func newTestWriter(db *fakesqldb.DB, frozenTime *time.Time) *heartbeatWriter { 67 config := tabletenv.NewDefaultConfig() 68 config.ReplicationTracker.Mode = tabletenv.Heartbeat 69 config.ReplicationTracker.HeartbeatIntervalSeconds = 1 70 71 params, _ := db.ConnParams().MysqlParams() 72 cp := *params 73 dbc := dbconfigs.NewTestDBConfigs(cp, cp, "") 74 75 tw := newHeartbeatWriter(tabletenv.NewEnv(config, "WriterTest"), &topodatapb.TabletAlias{Cell: "test", Uid: 1111}) 76 tw.keyspaceShard = "test:0" 77 78 if frozenTime != nil { 79 tw.now = func() time.Time { 80 return *frozenTime 81 } 82 } 83 84 tw.appPool.Open(dbc.AppWithDB()) 85 tw.allPrivsPool.Open(dbc.AllPrivsWithDB()) 86 87 return tw 88 }