Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for function (#746) #756

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/runtime/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ func (ns *Namespace) DB(ctx context.Context, group string) proto.DB {
if len(wrList) != 0 {
target = selector.NewWeightRandomSelector(wrList).GetDataSourceNo()
}

return exist[target]
if len(exist) > 0 {
return exist[target]
}
return nil
}

// DBMaster returns a master DB, returns nil if nothing selected.
Expand Down Expand Up @@ -243,8 +245,9 @@ func (ns *Namespace) DBSlave(_ context.Context, group string) proto.DB {
}
if len(wrList) != 0 {
target = selector.NewWeightRandomSelector(wrList).GetDataSourceNo()
return readDBList[target]
}
return readDBList[target]
return nil
}

// SysDB returns SysDB
Expand Down
41 changes: 40 additions & 1 deletion pkg/runtime/namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func TestRegister(t *testing.T) {
db := testdata.NewMockDB(ctrl)
db.EXPECT().ID().Return(fmt.Sprintf("the-mysql-instance-%d", i)).AnyTimes()
db.EXPECT().Weight().Return(proto.Weight{R: 10, W: 10}).AnyTimes()
db.EXPECT().Close().Times(1)
db.EXPECT().SetWeight(proto.Weight{R: 9, W: 1}).Return(nil).AnyTimes()
db.EXPECT().Close().AnyTimes()
return db
}

Expand Down Expand Up @@ -84,6 +85,41 @@ func TestRegister(t *testing.T) {
assert.NotNil(t, db)

assert.Equal(t, []string{getGroup(0), getGroup(1)}, ns.DBGroups())

err = ns.EnqueueCommand(UpdateWeight(getGroup(1), getDB(2).ID(), proto.Weight{R: 9, W: 1}))
assert.NoError(t, err)
time.Sleep(5 * time.Millisecond)

dbs := ns.DBs(getGroup(0))
assert.NotNil(t, dbs)
db0 := ns.DB0(context.Background())
assert.NotNil(t, db0)
assert.Equal(t, dbs[0].ID(), db0.ID())

dbm := ns.DBMaster(context.Background(), getGroup(0))
assert.NotNil(t, dbm)
dbl := ns.DBSlave(context.Background(), getGroup(0))
assert.Nil(t, dbl)

sys := ns.SysDB()
assert.Nil(t, sys)
rule := ns.Rule()
assert.NotNil(t, rule)

err = ns.EnqueueCommand(RemoveNode(getGroup(0), getDB(1).ID()))
assert.Nil(t, err)
time.Sleep(5 * time.Millisecond)

err = ns.EnqueueCommand(RemoveDB(getGroup(1), getDB(2).ID()))
assert.Nil(t, err)
time.Sleep(5 * time.Millisecond)
db = ns.DB(context.Background(), getGroup(1))
assert.Nil(t, db)
err = ns.EnqueueCommand(RemoveGroup(getGroup(1)))
assert.Nil(t, err)
time.Sleep(5 * time.Millisecond)
gp := ns.DBGroups()
assert.Equal(t, 1, len(gp))
}

func TestGetDBByWeight(t *testing.T) {
Expand Down Expand Up @@ -117,6 +153,9 @@ func TestGetDBByWeight(t *testing.T) {
assert.NoError(t, err, "should unregister ok")
}()
time.Sleep(5 * time.Millisecond)
nsList := List()
assert.NotNil(t, nsList, "should list namespace")
assert.Equal(t, 1, len(nsList))
ns = Load(tenant, name)
assert.NotNil(t, ns, "should load namespace")
ctx := rcontext.WithRead(context.Background())
Expand Down
Loading