Example #1
0
func newTestAppFromMap(t testing.TB, fsmap map[string]string) *testApp {
	up := mock.NewRequestHandler(fsMapHandler(fsmap))
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)
	loc := &types.Location{}
	var err error
	loc.Logger = newStdLogger()
	loc.CacheKey = "test"
	loc.CacheKeyIncludesQuery = false

	path, cleanup := testutils.GetTestFolder(t)

	cz := &config.CacheZone{
		ID:             "1",
		Type:           "disk",
		Path:           path,
		StorageObjects: 200,
		Algorithm:      "lru",
		PartSize:       5,
	}

	st, err := storage.New(cz, loc.Logger)
	if err != nil {
		panic(err)
	}
	ca, err := cache.New(cz, st.DiscardPart, loc.Logger)
	if err != nil {
		panic(err)
	}
	loc.Cache = &types.CacheZone{
		ID:        cz.ID,
		PartSize:  cz.PartSize,
		Algorithm: ca,
		Scheduler: storage.NewScheduler(),
		Storage:   st,
	}

	cacheHandler, err := New(nil, loc, up)
	if err != nil {
		t.Fatal(err)
	}
	app := &testApp{
		TB:           t,
		up:           up,
		ctx:          context.Background(),
		cacheHandler: cacheHandler,
		fsmap:        fsmap,
		cleanup:      cleanup,
	}
	return app
}
Example #2
0
func (a *Application) initCacheZone(cfgCz *config.CacheZone) (err error) {
	cz := &types.CacheZone{
		ID:        cfgCz.ID,
		PartSize:  cfgCz.PartSize,
		Scheduler: storage.NewScheduler(),
	}
	// Initialize the storage
	if cz.Storage, err = storage.New(cfgCz, a.logger); err != nil {
		return fmt.Errorf("Could not initialize storage '%s' for cache zone '%s': %s",
			cfgCz.Type, cfgCz.ID, err)
	}

	// Initialize the cache algorithm
	if cz.Algorithm, err = cache.New(cfgCz, cz.Storage.DiscardPart, a.logger); err != nil {
		return fmt.Errorf("Could not initialize algorithm '%s' for cache zone '%s': %s",
			cfgCz.Algorithm, cfgCz.ID, err)
	}

	a.reloadCache(cz)
	a.cacheZones[cfgCz.ID] = cz

	return nil
}