本文整理匯總了Golang中github.com/eleme/banshee/util.Must函數的典型用法代碼示例。如果您正苦於以下問題:Golang Must函數的具體用法?Golang Must怎麽用?Golang Must使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Must函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPut
func TestPut(t *testing.T) {
// Open db.
fileName := "db-testing"
opts := &Options{Period: 86400, Expiration: 86400 * 7}
db, _ := Open(fileName, opts)
defer os.RemoveAll(fileName)
defer db.Close()
// Put.
m := &models.Metric{
Name: "foo",
Link: 1,
Stamp: 1452758773,
Value: 3.14,
Score: 0.1892,
Average: 3.133,
}
err := db.Put(m)
util.Must(t, err == nil)
// Must in db
key := encodeKey(m)
value, err := db.pool[0].db.Get(key, nil)
util.Must(t, err == nil)
m1 := &models.Metric{
Name: m.Name,
Stamp: m.Stamp,
Link: 1,
}
err = decodeValue(value, m1)
util.Must(t, err == nil)
util.Must(t, reflect.DeepEqual(m, m1))
}
示例2: TestStorageExpire
func TestStorageExpire(t *testing.T) {
// Open db.
fileName := "db-testing"
opts := &Options{Period: 86400, Expiration: 86400 * 7}
db, _ := Open(fileName, opts)
defer os.RemoveAll(fileName)
defer db.Close()
// Force creating 7+1 storages.
base := uint32(time.Now().Unix())
db.Put(&EventWrapper{ID: "20160525155730.1", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base}) // 0
id := db.pool[0].id // record the id to be deleted
db.Put(&EventWrapper{ID: "20160525155730.2", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*1}) // 1
db.Put(&EventWrapper{ID: "20160525155730.3", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*2}) // 2
db.Put(&EventWrapper{ID: "20160525155730.4", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*3}) // 3
db.Put(&EventWrapper{ID: "20160525155730.5", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*4}) // 4
db.Put(&EventWrapper{ID: "20160525155730.6", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*5}) // 5
db.Put(&EventWrapper{ID: "20160525155730.7", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*6}) // 6
util.Must(t, len(db.pool) == 7)
db.Put(&EventWrapper{ID: "20160525155730.8", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*7}) // 7
util.Must(t, len(db.pool) == 8)
db.Put(&EventWrapper{ID: "20160525155730.9", RuleID: 1, ProjectID: 1, Level: 2, Name: "foo", Stamp: base + db.opts.Period*8}) // 8
util.Must(t, len(db.pool) == 8) // Full storages: 1,2,3,4,5,6,7
// Files must be deleted.
deleteFileName := path.Join(fileName, strconv.FormatUint(uint64(id), 10))
util.Must(t, !util.IsFileExist(deleteFileName))
}
示例3: TestExampleConfigParsing
func TestExampleConfigParsing(t *testing.T) {
c := New()
err := c.UpdateWithYamlFile("./exampleConfig.yaml")
util.Must(t, err == nil)
defaults := New()
util.Must(t, reflect.DeepEqual(c, defaults))
}
示例4: TestOpen
func TestOpen(t *testing.T) {
fileName := "db-testing"
db, err := Open(fileName, nil)
util.Must(t, err == nil)
util.Must(t, util.IsFileExist(fileName))
db.Close()
os.RemoveAll(fileName)
}
示例5: TestOpenOptionsNil
func TestOpenOptionsNil(t *testing.T) {
fileName := "db-testing"
db, err := Open(fileName, nil)
util.Must(t, err == nil)
util.Must(t, util.IsFileExist(fileName))
defer os.RemoveAll(fileName)
defer db.Close()
util.Must(t, len(db.pool) == 0) // should have nothing in pool..
}
示例6: TestLoad
func TestLoad(t *testing.T) {
fileName := "db-testing"
db, _ := Open(fileName, nil)
defer os.RemoveAll(fileName)
defer db.Close()
idx := &models.Index{Name: "foo", Stamp: 1450430839, Score: 0.7, Average: 78.5}
// Add one
db.Put(idx)
util.Must(t, db.tr.Has(idx.Name))
util.Must(t, idx.Link == 1) // the first link
// Clear cache
db.tr.Clear()
db.idp.Clear()
util.Must(t, db.tr.Len() == 0)
util.Must(t, !db.tr.Has(idx.Name))
// Reload
db.load()
// Must not empty and idx in cache
util.Must(t, db.tr.Len() == 1)
util.Must(t, db.tr.Has(idx.Name))
t.Logf("idp len: %v", db.idp.Len())
util.Must(t, db.idp.Len() == 1)
// Get again.
i, err := db.Get(idx.Name)
util.Must(t, err == nil && i.Equal(idx))
util.Must(t, i.Link == 1) // link should be correct
}
示例7: TestEventGenerateID
func TestEventGenerateID(t *testing.T) {
rule := &Rule{ID: 1}
// Metric with the same name but different stamps.
ev1 := NewEvent(&Metric{Name: "foo", Stamp: 1456815973}, nil, rule)
ev2 := NewEvent(&Metric{Name: "foo", Stamp: 1456815974}, nil, rule)
util.Must(t, ev1.ID != ev2.ID)
// Metric with the same stamp but different names.
ev1 = NewEvent(&Metric{Name: "foo", Stamp: 1456815973}, nil, rule)
ev2 = NewEvent(&Metric{Name: "bar", Stamp: 1456815973}, nil, rule)
util.Must(t, ev1.ID != ev2.ID)
}
示例8: TestClear
func TestClear(t *testing.T) {
tr := New()
// Case simple.
tr.Put("a.b.c.d", 4)
tr.Put("a.b.c.d.e", 5)
tr.Put("a.b.c.d.e.f", 6)
util.Must(t, tr.Len() == 3)
tr.Clear()
util.Must(t, tr.Len() == 0)
util.Must(t, !tr.Has("a.b.c.d"))
}
示例9: TestPickTrendingFactor
func TestPickTrendingFactor(t *testing.T) {
cfg := config.New()
d := &Detector{cfg: cfg}
rules := []*models.Rule{
&models.Rule{Level: models.RuleLevelLow},
}
util.Must(t, d.pickTrendingFactor(rules) == cfg.Detector.TrendingFactorLowLevel)
rules = append(rules, &models.Rule{Level: models.RuleLevelMiddle})
util.Must(t, d.pickTrendingFactor(rules) == cfg.Detector.TrendingFactorMiddleLevel)
rules = append(rules, &models.Rule{Level: models.RuleLevelHigh})
util.Must(t, d.pickTrendingFactor(rules) == cfg.Detector.TrendingFactorHighLevel)
}
示例10: TestDelete
func TestDelete(t *testing.T) {
// Open db.
fileName := "db-testing"
db, _ := Open(fileName, nil)
defer os.RemoveAll(fileName)
defer db.Close()
// Add one.
idx := &models.Index{Name: "foo", Stamp: 1450430837, Score: 0.3, Average: 100}
db.Put(idx)
// Must in cache.
util.Must(t, db.tr.Has(idx.Name))
util.Must(t, db.idp.Len() == 1)
util.Must(t, idx.Link == 1)
// Delete it.
err := db.Delete(idx.Name)
util.Must(t, err == nil)
// Must not exist in cache
util.Must(t, !db.tr.Has(idx.Name))
// Must not in db.
_, err = db.db.Get([]byte(idx.Name), nil)
util.Must(t, err == leveldb.ErrNotFound)
// Must not in link pool.
util.Must(t, db.idp.Len() == 0)
// Cant get again.
_, err = db.Get(idx.Name)
util.Must(t, ErrNotFound == err)
}
示例11: TestMap
func TestMap(t *testing.T) {
tr := New()
// Case empty.
util.Must(t, len(tr.Map()) == 0)
// Case simple.
tr.Put("a.b.c.d", 41)
tr.Put("a.b.c.d.e", 51)
tr.Put("a.b.c.d.e.f", 61)
m := tr.Map()
util.Must(t, len(m) == 3)
util.Must(t, m["a.b.c.d"].(int) == 41)
util.Must(t, m["a.b.c.d.e"].(int) == 51)
util.Must(t, m["a.b.c.d.e.f"].(int) == 61)
}
示例12: TestOpenInit
func TestOpenInit(t *testing.T) {
fileName := "db-testing"
opts := &Options{Period: 86400, Expiration: 86400 * 7}
db, err := Open(fileName, opts)
util.Must(t, err == nil)
defer os.RemoveAll(fileName)
stamp := uint32(time.Now().Unix())
db.Put(&EventWrapper{Stamp: stamp})
db.Close()
// Reopen.
db, err = Open(fileName, opts)
util.Must(t, err == nil)
util.Must(t, len(db.pool) == 1) // should have one storage
util.Must(t, db.pool[0].id*db.opts.Period <= stamp)
}
示例13: TestLoadExpired
func TestLoadExpired(t *testing.T) {
fileName := "db-testing"
opts := &Options{86400 * 7}
db, _ := Open(fileName, opts)
defer os.RemoveAll(fileName)
defer db.Close()
idx := &models.Index{Name: "foo", Stamp: 1450430839, Score: 0.7, Average: 78.5}
db.Put(idx)
// Clear cache
db.tr.Clear()
db.idp.Clear()
db.load()
util.Must(t, db.tr.Len() == 0)
util.Must(t, !db.tr.Has(idx.Name))
}
示例14: TestFilter
func TestFilter(t *testing.T) {
// Open db.
fileName := "db-testing"
db, _ := Open(fileName, nil)
defer os.RemoveAll(fileName)
defer db.Close()
// Add indexes.
excludeName := "a.b.c.d.e.f"
db.Put(&models.Index{Name: "a.b.c.e.f.g"})
db.Put(&models.Index{Name: "a.b.c.d.f.g"})
db.Put(&models.Index{Name: excludeName})
// Filter
l := db.Filter("a.b.c.*.f.*")
util.Must(t, len(l) == 2)
util.Must(t, l[0].Name != excludeName && l[1].Name != excludeName)
}
示例15: TestEventTranslateRuleComment
func TestEventTranslateRuleComment(t *testing.T) {
m := &Metric{Name: "timer.count_ps.foo.bar"}
r := &Rule{Pattern: "timer.count_ps.*.*", Comment: "$1 and $2 timing"}
ev := &Event{Metric: m, Rule: r}
excepted := "foo and bar timing"
util.Must(t, ev.TranslateRuleComment() == excepted)
}