Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Dec 17, 2024
1 parent 03d774e commit 7c88a97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions go/vt/vtorc/inst/shard_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func SaveShard(shard *topo.ShardInfo) error {
}

// DeleteStaleKeyspaceShards deletes shard records that have not been updated since a provided time.
func DeleteStaleKeyspaceShards(keyspace string, staleTime time.Time) error {
func DeleteStaleKeyspaceShards(keyspaceName string, staleTime time.Time) error {
_, err := db.ExecVTOrc(`DELETE FROM vitess_shard
WHERE
keyspace = ?
AND
updated_timestamp < DATETIME(?, 'unixepoch')
updated_timestamp <= DATETIME(?, 'unixepoch')
`,
keyspace,
keyspaceName,
staleTime.Unix(),
)
return err
Expand Down
26 changes: 26 additions & 0 deletions go/vt/vtorc/inst/shard_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,29 @@ func TestGetKeyspaceShardNames(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{"-80", "80-"}, shardNames)
}

func TestDeleteStaleKeyspaceShards(t *testing.T) {
// Clear the database after the test. The easiest way to do that is to run all the initialization commands again.
defer func() {
db.ClearVTOrcDatabase()
}()

shardInfo := topo.NewShardInfo("ks1", "-80", &topodatapb.Shard{}, nil)
err := SaveShard(shardInfo)
require.NoError(t, err)
shards, err := GetKeyspaceShardNames("ks1")
require.NoError(t, err)
require.Len(t, shards, 1)

// test a staletime before save causes no delete
require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now().Add(-time.Hour)))
shards, err = GetKeyspaceShardNames("ks1")
require.NoError(t, err)
require.Len(t, shards, 1)

// test statetime of now deletes everything
require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now()))
shards, err = GetKeyspaceShardNames("ks1")
require.NoError(t, err)
require.Len(t, shards, 0)
}

0 comments on commit 7c88a97

Please sign in to comment.