Exemplo n.º 1
0
func TestOpen(t *testing.T) {
	// Open db.
	fileName := "storage_test"
	db, err := Open(fileName)
	assert.Ok(t, err == nil)
	assert.Ok(t, db != nil)
	// Defer close and remove files.
	defer db.Close()
	defer os.RemoveAll(fileName)
	// Check if child db file exist
	assert.Ok(t, util.IsFileExist(path.Join(fileName, admindbFileName)))
	assert.Ok(t, util.IsFileExist(path.Join(fileName, indexdbFileName)))
	assert.Ok(t, util.IsFileExist(path.Join(fileName, metricdbFileName)))
}
Exemplo n.º 2
0
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))
}
Exemplo n.º 3
0
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(&models.Metric{Link: 1, Stamp: base})                    // 0
	id := db.pool[0].id                                             // record the id to be deleted
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*1}) // 1
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*2}) // 2
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*3}) // 3
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*4}) // 4
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*5}) // 5
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*6}) // 6
	util.Must(t, len(db.pool) == 7)
	db.Put(&models.Metric{Link: 1, Stamp: base + db.opts.Period*7}) // 7
	util.Must(t, len(db.pool) == 8)
	db.Put(&models.Metric{Link: 1, 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))
}
Exemplo n.º 4
0
func TestOpen(t *testing.T) {
	fileName := "db-testing"
	db, err := Open(fileName)
	assert.Ok(t, err == nil)
	assert.Ok(t, util.IsFileExist(fileName))
	db.Close()
	os.RemoveAll(fileName)
}
Exemplo n.º 5
0
func TestOpen(t *testing.T) {
	// Open db.
	fileName := "storage_test"
	options := &Options{NumGrid: 288, GridLen: 300}
	db, err := Open(fileName, options)
	assert.Ok(t, err == nil)
	assert.Ok(t, db != nil)
	// Defer close and remove files.
	defer db.Close()
	defer os.RemoveAll(fileName)
	// Check if child db file exist
	assert.Ok(t, util.IsFileExist(path.Join(fileName, admindbFileName)))
	assert.Ok(t, util.IsFileExist(path.Join(fileName, indexdbFileName)))
	assert.Ok(t, util.IsFileExist(path.Join(fileName, metricdbFileName)))
	sFileName := fmt.Sprintf("%s-%dx%d", statedbFileName, options.NumGrid, options.GridLen)
	assert.Ok(t, util.IsFileExist(path.Join(fileName, sFileName)))
}
Exemplo n.º 6
0
func TestOpen(t *testing.T) {
	fileName := "db-testing"
	db, err := Open(fileName, &Options{288, 300})
	defer db.Close()
	defer os.RemoveAll(fileName)
	assert.Ok(t, err == nil)
	assert.Ok(t, util.IsFileExist(fileName))
}
Exemplo n.º 7
0
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..
}
Exemplo n.º 8
0
func TestOpen(t *testing.T) {
	fileName := "db-testing"
	db, err := Open(fileName)
	assert.Ok(t, nil == err)
	assert.Ok(t, db != nil)
	assert.Ok(t, util.IsFileExist(fileName))
	defer os.RemoveAll(fileName)
	defer db.Close()
	assert.Ok(t, db.DB().HasTable(&models.User{}))
	assert.Ok(t, db.DB().HasTable(&models.Rule{}))
	assert.Ok(t, db.DB().HasTable(&models.Project{}))
}