本文整理匯總了Golang中github.com/youtube/vitess/go/vt/topo.Impl.NewMasterParticipation方法的典型用法代碼示例。如果您正苦於以下問題:Golang Impl.NewMasterParticipation方法的具體用法?Golang Impl.NewMasterParticipation怎麽用?Golang Impl.NewMasterParticipation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/youtube/vitess/go/vt/topo.Impl
的用法示例。
在下文中一共展示了Impl.NewMasterParticipation方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: checkElection
// checkElection runs the tests on the MasterParticipation part of the
// topo.Impl API.
func checkElection(t *testing.T, ts topo.Impl) {
name := "testmp"
// create a new MasterParticipation
id1 := "id1"
mp1, err := ts.NewMasterParticipation(name, id1)
if err != nil {
t.Fatalf("cannot create mp1: %v", err)
}
// no master yet, check name
waitForMasterID(t, mp1, "")
// wait for id1 to be the master
ctx1, err := mp1.WaitForMastership()
if err != nil {
t.Fatalf("mp1 cannot become master: %v", err)
}
// get the current master name, better be id1
waitForMasterID(t, mp1, id1)
// create a second MasterParticipation on same name
id2 := "id2"
mp2, err := ts.NewMasterParticipation(name, id2)
if err != nil {
t.Fatalf("cannot create mp2: %v", err)
}
// wait until mp2 gets to be the master in the background
mp2IsMaster := make(chan error)
go func() {
var err error
_, err = mp2.WaitForMastership()
mp2IsMaster <- err
}()
// ask mp2 for master name, should get id1
waitForMasterID(t, mp2, id1)
// stop mp1
mp1.Stop()
// this should have closed ctx1 as soon as possible,
// so 5s should be enough in tests. This will be used during lameduck
// when the server exits, so we can't wait too long anyway.
select {
case <-ctx1.Done():
case <-time.After(5 * time.Second):
t.Fatalf("shutting down mp1 didn't close ctx1 in time")
}
// now mp2 should be master
err = <-mp2IsMaster
if err != nil {
t.Fatalf("mp2 awoke with error: %v", err)
}
// ask mp2 for master name, should get id2
waitForMasterID(t, mp2, id2)
// stop mp2, we're done
mp2.Stop()
}