當前位置: 首頁>>代碼示例>>Golang>>正文


Golang QueryRules.Find方法代碼示例

本文整理匯總了Golang中github.com/youtube/vitess/go/vt/tabletserver.QueryRules.Find方法的典型用法代碼示例。如果您正苦於以下問題:Golang QueryRules.Find方法的具體用法?Golang QueryRules.Find怎麽用?Golang QueryRules.Find使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/youtube/vitess/go/vt/tabletserver.QueryRules的用法示例。


在下文中一共展示了QueryRules.Find方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestFileCustomRule

func TestFileCustomRule(t *testing.T) {
	tqsc := tabletservermock.NewController()

	var qrs *tabletserver.QueryRules
	rulepath := path.Join(os.TempDir(), ".customrule.json")
	// Set r1 and try to get it back
	err := ioutil.WriteFile(rulepath, []byte(customRule1), os.FileMode(0644))
	if err != nil {
		t.Fatalf("Cannot write r1 to rule file %s, err=%v", rulepath, err)
	}

	fcr := NewFileCustomRule()
	// Let FileCustomRule to build rule from the local file
	err = fcr.Open(tqsc, rulepath)
	if err != nil {
		t.Fatalf("Cannot open file custom rule service, err=%v", err)
	}
	// Fetch query rules we built to verify correctness
	qrs, _, err = fcr.GetRules()
	if err != nil {
		t.Fatalf("GetRules returns error: %v", err)
	}
	qr := qrs.Find("r1")
	if qr == nil {
		t.Fatalf("Expect custom rule r1 to be found, but got nothing, qrs=%v", qrs)
	}
}
開發者ID:littleyang,項目名稱:vitess,代碼行數:27,代碼來源:filecustomrule_test.go

示例2: TestZkCustomRule

func TestZkCustomRule(t *testing.T) {
	tqsc := tabletservermock.NewController()

	setUpFakeZk(t)
	zkcr := NewZkCustomRule(conn)
	err := zkcr.Open(tqsc, "/zk/fake/customrules/testrules")
	if err != nil {
		t.Fatalf("Cannot open zookeeper custom rule service, err=%v", err)
	}

	var qrs *tabletserver.QueryRules
	// Test if we can successfully fetch the original rule (test GetRules)
	qrs, _, err = zkcr.GetRules()
	if err != nil {
		t.Fatalf("GetRules of ZkCustomRule should always return nil error, but we receive %v", err)
	}
	qr := qrs.Find("r1")
	if qr == nil {
		t.Fatalf("Expect custom rule r1 to be found, but got nothing, qrs=%v", qrs)
	}

	// Test updating rules
	conn.Set("/zk/fake/customrules/testrules", customRule2, -1)
	<-time.After(time.Second) //Wait for the polling thread to respond
	qrs, _, err = zkcr.GetRules()
	if err != nil {
		t.Fatalf("GetRules of ZkCustomRule should always return nil error, but we receive %v", err)
	}
	qr = qrs.Find("r2")
	if qr == nil {
		t.Fatalf("Expect custom rule r2 to be found, but got nothing, qrs=%v", qrs)
	}
	qr = qrs.Find("r1")
	if qr != nil {
		t.Fatalf("Custom rule r1 should not be found after r2 is set")
	}

	// Test rule path removal
	conn.Delete("/zk/fake/customrules/testrules", -1)
	<-time.After(time.Second)
	qrs, _, err = zkcr.GetRules()
	if err != nil {
		t.Fatalf("GetRules of ZkCustomRule should always return nil error, but we receive %v", err)
	}
	if reflect.DeepEqual(qrs, tabletserver.NewQueryRules()) {
		t.Fatalf("Expect empty rule at this point")
	}

	// Test rule path revival
	conn.Create("/zk/fake/customrules/testrules", "customrule2", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	conn.Set("/zk/fake/customrules/testrules", customRule2, -1)
	<-time.After(time.Second) //Wait for the polling thread to respond
	qrs, _, err = zkcr.GetRules()
	if err != nil {
		t.Fatalf("GetRules of ZkCustomRule should always return nil error, but we receive %v", err)
	}
	qr = qrs.Find("r2")
	if qr == nil {
		t.Fatalf("Expect custom rule r2 to be found, but got nothing, qrs=%v", qrs)
	}

	zkcr.Close()
}
開發者ID:hadoop835,項目名稱:vitess,代碼行數:63,代碼來源:zkcustomrule_test.go


注:本文中的github.com/youtube/vitess/go/vt/tabletserver.QueryRules.Find方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。