-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: fix nil pointer in vtgate on topo connection error (#13010)
* fix nil pointer in vtgate on topo connection error Signed-off-by: Olga Shestopalova <[email protected]> * add test Signed-off-by: Olga Shestopalova <[email protected]> * fix year Signed-off-by: Olga Shestopalova <[email protected]> --------- Signed-off-by: Olga Shestopalova <[email protected]> Co-authored-by: Olga Shestopalova <[email protected]>
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package discovery | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
vschemapb "vitess.io/vitess/go/vt/proto/vschema" | ||
"vitess.io/vitess/go/vt/topo" | ||
"vitess.io/vitess/go/vt/topo/faketopo" | ||
) | ||
|
||
func TestSrvKeyspaceWithNilNewKeyspace(t *testing.T) { | ||
cell := "cell" | ||
keyspace := "testks" | ||
factory := faketopo.NewFakeTopoFactory() | ||
factory.AddCell(cell) | ||
ts := faketopo.NewFakeTopoServer(factory) | ||
ts2 := &fakeTopoServer{} | ||
hc := NewHealthCheck(context.Background(), 1*time.Millisecond, time.Hour, ts, cell, "") | ||
kew := NewKeyspaceEventWatcher(context.Background(), ts2, hc, cell) | ||
kss := &keyspaceState{ | ||
kew: kew, | ||
keyspace: keyspace, | ||
shards: make(map[string]*shardState), | ||
} | ||
kss.lastKeyspace = &topodatapb.SrvKeyspace{ | ||
ServedFrom: []*topodatapb.SrvKeyspace_ServedFrom{ | ||
{ | ||
TabletType: topodatapb.TabletType_PRIMARY, | ||
Keyspace: keyspace, | ||
}, | ||
}, | ||
} | ||
require.True(t, kss.onSrvKeyspace(nil, nil)) | ||
} | ||
|
||
type fakeTopoServer struct { | ||
} | ||
|
||
// GetTopoServer returns the full topo.Server instance. | ||
func (f *fakeTopoServer) GetTopoServer() (*topo.Server, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetSrvKeyspaceNames returns the list of keyspaces served in | ||
// the provided cell. | ||
func (f *fakeTopoServer) GetSrvKeyspaceNames(ctx context.Context, cell string, staleOK bool) ([]string, error) { | ||
return []string{"ks1"}, nil | ||
} | ||
|
||
// GetSrvKeyspace returns the SrvKeyspace for a cell/keyspace. | ||
func (f *fakeTopoServer) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) { | ||
zeroHexBytes, _ := hex.DecodeString("") | ||
eightyHexBytes, _ := hex.DecodeString("80") | ||
ks := &topodatapb.SrvKeyspace{ | ||
Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{ | ||
{ | ||
ServedType: topodatapb.TabletType_PRIMARY, | ||
ShardReferences: []*topodatapb.ShardReference{ | ||
{Name: "-80", KeyRange: &topodatapb.KeyRange{Start: zeroHexBytes, End: eightyHexBytes}}, | ||
{Name: "80-", KeyRange: &topodatapb.KeyRange{Start: eightyHexBytes, End: zeroHexBytes}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return ks, nil | ||
} | ||
|
||
func (f *fakeTopoServer) WatchSrvKeyspace(ctx context.Context, cell, keyspace string, callback func(*topodatapb.SrvKeyspace, error) bool) { | ||
ks, err := f.GetSrvKeyspace(ctx, cell, keyspace) | ||
callback(ks, err) | ||
} | ||
|
||
// WatchSrvVSchema starts watching the SrvVSchema object for | ||
// the provided cell. It will call the callback when | ||
// a new value or an error occurs. | ||
func (f *fakeTopoServer) WatchSrvVSchema(ctx context.Context, cell string, callback func(*vschemapb.SrvVSchema, error) bool) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters